Skip to content

Instantly share code, notes, and snippets.

@nrafter
Created April 11, 2014 03:08
Show Gist options
  • Save nrafter/10438840 to your computer and use it in GitHub Desktop.
Save nrafter/10438840 to your computer and use it in GitHub Desktop.
Project Euler problem 7
/*
http://projecteuler.net/problem=7
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?
*/
var findPrime = function findPrime(num) {
var t0 = performance.now();
var count = 2;
test:
for (var test = 5; count !== num; test += 2) {
var divisor = Math.ceil(Math.sqrt(test)) % 2 ? Math.ceil(Math.sqrt(test)) : Math.ceil(Math.sqrt(test)) - 1;
for (divisor; divisor > 1; divisor -= 2) {
if (!(test % divisor)) {
continue test;
}
}
count++;
}
return {prime: test - 2, time: performance.now() - t0};
}
findPrime(10001);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment