Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static func getPrimes(to n: Int) -> [Int] {
- let xmody = (1...n)
- .map { x in (1...n).map { y in x % y } }
- let primes = xmody
- .map { mods in
- mods.enumerate()
- .filter { y, mod in mod == 0 }
- .map { y, mod in y + 1 } // divisors for x
- }
- .enumerate()
- .filter { x, zs in
- guard let z0 = zs.first, z1 = zs.last where zs.count <= 2 else {
- return false
- }
- return z0 == 1 && z1 == x + 1
- }
- .map { x, _ in x + 1 }
- return primes
- }
Add Comment
Please, Sign In to add comment