Created
August 29, 2014 22:24
-
-
Save abijlani/ab29b0d33ad5126d707c to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
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
// 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