Skip to content

Instantly share code, notes, and snippets.

@NSEGeorge
Created September 16, 2019 16:05
Show Gist options
  • Save NSEGeorge/ee27723528a8b669f87457219c76022e to your computer and use it in GitHub Desktop.
Save NSEGeorge/ee27723528a8b669f87457219c76022e to your computer and use it in GitHub Desktop.
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