39 lines
883 B
Swift
39 lines
883 B
Swift
import Foundation
|
|
|
|
func solution(_ nums:[Int]) -> Int {
|
|
guard let MAX = nums.max() else { return -1 }
|
|
|
|
let maxLimit: Int = 3 * MAX - 3
|
|
|
|
var isPrime: [Bool] = Array(repeating: true, count: maxLimit + 1)
|
|
isPrime[0] = false
|
|
isPrime[1] = false
|
|
|
|
for n in 2...Int(sqrt(Double(maxLimit))) {
|
|
if isPrime[n] {
|
|
for i in stride(from: n * 2, through: maxLimit, by: n) {
|
|
isPrime[i] = false
|
|
}
|
|
}
|
|
}
|
|
|
|
var ans: Int = 0
|
|
let count: Int = nums.count
|
|
|
|
for i in 0 ..< count - 2 {
|
|
for j in i+1 ..< count - 1 {
|
|
for k in j+1 ..< count {
|
|
let sum: Int = nums[i] + nums[j] + nums[k]
|
|
if isPrime[sum] {
|
|
ans += 1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return ans
|
|
}
|
|
|
|
let nums: [Int] = [1,2,7,6,4]
|
|
print(solution(nums))
|