Created
November 6, 2016 20:42
-
-
Save nerdybeast/ff336518a8d875cce1a3c5661d9a0648 to your computer and use it in GitHub Desktop.
Euler 7 - 10001'st Prime Number
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
'use strict'; | |
const MAX = 10001; | |
let primes = []; | |
let n = 2; | |
while(primes.length < MAX) { | |
if(isPrime(n)) { primes.push(n); } | |
if(n === 2) { n++; } else { n += 2; } | |
} | |
function isPrime(n) { | |
let start = 2; | |
while(start <= Math.sqrt(n)) { | |
if(n % start++ === 0) return false; | |
} | |
return n > 1; | |
} | |
console.log('10001\'st prime number =>', primes.pop()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment