Created
September 16, 2019 16:05
-
-
Save NSEGeorge/ee27723528a8b669f87457219c76022e to your computer and use it in GitHub Desktop.
This file contains 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
typealias A = ArraySlice<Bool> | |
func sieveOfEratosthenes(limit: Int) -> [Int] { | |
var possibles = A(repeating: true, count: limit + 1) | |
var result = [Int]() | |
for i in 2 ... limit { | |
guard possibles[i] else { continue } | |
for j in stride(from: i * i, through: limit, by: i) { | |
possibles[j] = false | |
} | |
result.append(i) | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment