Skip to content

Instantly share code, notes, and snippets.

@cekici
Last active August 28, 2018 22:15
Show Gist options
  • Save cekici/21f3a9fa5cd6397a75b0df3c3df7839c to your computer and use it in GitHub Desktop.
Save cekici/21f3a9fa5cd6397a75b0df3c3df7839c to your computer and use it in GitHub Desktop.
function isPrime(x) {
for (let i = 2; i < x; i = i+1) {
if (x % i === 0) {
return false;
}
}
return true;
}
function findNthPrime(x) {
let nth;
if (x === 1) {
nth = 2;
} else {
let primeIndex = 1;
for (let i = 3; true; i = i+2) {
if (isPrime(i)) {
primeIndex = primeIndex + 1;
}
if (primeIndex === x) {
nth = i;
break;
}
}
}
return nth;
}
console.log(findNthPrime(10001));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment