Skip to content

Instantly share code, notes, and snippets.

@jremmen
Created June 22, 2013 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jremmen/5842804 to your computer and use it in GitHub Desktop.
Save jremmen/5842804 to your computer and use it in GitHub Desktop.
js: returns a list of n primes
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