Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
46
Task/Additive-primes/Swift/additive-primes.swift
Normal file
46
Task/Additive-primes/Swift/additive-primes.swift
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import Foundation
|
||||
|
||||
func isPrime(_ n: Int) -> Bool {
|
||||
if n < 2 {
|
||||
return false
|
||||
}
|
||||
if n % 2 == 0 {
|
||||
return n == 2
|
||||
}
|
||||
if n % 3 == 0 {
|
||||
return n == 3
|
||||
}
|
||||
var p = 5
|
||||
while p * p <= n {
|
||||
if n % p == 0 {
|
||||
return false
|
||||
}
|
||||
p += 2
|
||||
if n % p == 0 {
|
||||
return false
|
||||
}
|
||||
p += 4
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func digitSum(_ num: Int) -> Int {
|
||||
var sum = 0
|
||||
var n = num
|
||||
while n > 0 {
|
||||
sum += n % 10
|
||||
n /= 10
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
let limit = 500
|
||||
print("Additive primes less than \(limit):")
|
||||
var count = 0
|
||||
for n in 1..<limit {
|
||||
if isPrime(digitSum(n)) && isPrime(n) {
|
||||
count += 1
|
||||
print(String(format: "%3d", n), terminator: count % 10 == 0 ? "\n" : " ")
|
||||
}
|
||||
}
|
||||
print("\n\(count) additive primes found.")
|
||||
Loading…
Add table
Add a link
Reference in a new issue