Skip to content

Instantly share code, notes, and snippets.

@vasco3
Created September 19, 2013 06:42
Show Gist options
  • Save vasco3/6619818 to your computer and use it in GitHub Desktop.
Save vasco3/6619818 to your computer and use it in GitHub Desktop.
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number?
/*
By listing the first six prime numbers:
2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
*/
function findPrimeNumber(n){
//declare a counter and a variable of the latest prime number
var c = 1, nPrime = 2, i = 3;
console.log('c %s nPrime %s',c,nPrime);
function isPrime(k){
for( var j = 2; j < k; j++){
if( k % j === 0 ){ return false; }
}
return true;
}
//loop to infinity until find the nth prime number
while(c < n){
//test each number to see if it's prime
//if prime, then increase counter and update prime number
if( isPrime(i) ) {
c++;
nPrime = i;
}
i++;
}
return nPrime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment