Skip to content

Instantly share code, notes, and snippets.

@charliegroll
Last active August 29, 2015 13:58
Show Gist options
  • Select an option

  • Save charliegroll/9978980 to your computer and use it in GitHub Desktop.

Select an option

Save charliegroll/9978980 to your computer and use it in GitHub Desktop.
var i = 1,
curr = 2;
function isPrime(x){
var j = 2,
sq = Math.sqrt(x);
while (j <= sq) {
if (x%j == 0) return false;
j++;
}
return true;
}
function nthPrime(n, d) {
if (n == 0) return 1;
if (n == 1) return 2;
while (i<n) {
if (curr >= 3) {
curr = curr + 2;
} else {
curr++;
}
if (isPrime(curr)) {
i++;
}
if ((new Date() - d) > 10000) {
$('result').html(curr);
return;
}
}
return curr;
}
var d = new Date(),
nth = 499999;
console.log(nthPrime(nth));
console.log('time: ' + (new Date() - d));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment