Skip to content

Instantly share code, notes, and snippets.

@fhinkel
Last active December 3, 2017 19:49
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 fhinkel/2876ea53ab539c8d7c8e7026d5cdec72 to your computer and use it in GitHub Desktop.
Save fhinkel/2876ea53ab539c8d7c8e7026d5cdec72 to your computer and use it in GitHub Desktop.
function isPrime(p) {
const upper = Math.sqrt(p);
for(let i = 2; i <= upper; i++) {
if (p % i === 0 ) {
return false;
}
}
return true;
}
// Return n-th prime
function prime(n) {
if (n < 1) {
throw Error("n too small: " + n);
}
let count = 0;
let result = 1;
while(count < n) {
result++;
if (isPrime(result)) {
count++;
}
}
return result;
}
module.exports = {
prime
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment