Guest User

Untitled

a guest
Apr 30th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. static func getPrimes(to n: Int) -> [Int] {
  2. let xmody = (1...n)
  3. .map { x in (1...n).map { y in x % y } }
  4.  
  5. let primes = xmody
  6. .map { mods in
  7. mods.enumerate()
  8. .filter { y, mod in mod == 0 }
  9. .map { y, mod in y + 1 } // divisors for x
  10. }
  11. .enumerate()
  12. .filter { x, zs in
  13. guard let z0 = zs.first, z1 = zs.last where zs.count <= 2 else {
  14. return false
  15. }
  16. return z0 == 1 && z1 == x + 1
  17. }
  18. .map { x, _ in x + 1 }
  19.  
  20. return primes
  21. }
Add Comment
Please, Sign In to add comment