Created
June 22, 2013 22:06
-
-
Save jremmen/5842804 to your computer and use it in GitHub Desktop.
js: returns a list of n primes
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
function primes(n) { | |
function iter(i, acc) { | |
if(acc.length > n) return acc; | |
else if(i > 1 && divisor(2)) { | |
acc.push(i); | |
return iter(i + 1, acc); | |
} | |
else return iter(i + 1, acc); | |
function divisor(x) { | |
if(x > Math.sqrt(i)) return true; | |
else if(i % x === 0) return false; | |
else return divisor(x + 1); | |
} | |
} | |
return iter(1, []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment