Created
May 28, 2018 20:25
-
-
Save BrianLitwin/0eb41bfc36c394256ad0424cb8d3fdf7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func sieve(numbers: [Int]) -> [Int] { | |
if numbers.isEmpty { return [] } | |
let p = numbers[0] | |
// assert(p > 1, "numbers must start at 2 or higher") | |
let rest = numbers[1..<numbers.count] | |
return [p] + sieve(rest.filter { $0 % p > 0 }) | |
} | |
func primesUpTo(max: Int) -> [Int] { | |
return [1] + sieve(Array(2...max)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment