Skip to content

Instantly share code, notes, and snippets.

@abijlani
Created August 29, 2014 22:24
Show Gist options
  • Save abijlani/ab29b0d33ad5126d707c to your computer and use it in GitHub Desktop.
Save abijlani/ab29b0d33ad5126d707c to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
// Sieve of Eratosthenes
let numbers : Array<Int> = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
func sieve(array: [Int], startIndex: Int) -> [Int] {
let filteredArray = array.filter{(x) -> Bool in
if x == array[startIndex] {
return true
} else {
return x % array[startIndex] != 0
}
}
if array == filteredArray {
return filteredArray
} else {
return sieve(filteredArray,startIndex + 1)
}
}
var primes : [Int] = sieve(numbers,numbers.startIndex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment