-
-
Save nrafter/10438840 to your computer and use it in GitHub Desktop.
Project Euler problem 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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