Last active
November 10, 2018 16:25
-
-
Save sunjayaali/172a8a03921018b332faf5c8a9b2ffba to your computer and use it in GitHub Desktop.
Prime Numbers
This file contains hidden or 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
function isPrime(n) { | |
for (let i = 2; i <= Math.sqrt(n); i++) { | |
if (n % i === 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
function nextPrime(n) { | |
if (n > 2) { | |
for (let i = n + 1; i < 2 * n; i++) { | |
if (isPrime(i)) return i; | |
} | |
} | |
return n === 2 ? 3 : 2; | |
} | |
const result = []; | |
let t; | |
console.time(`log`); | |
for (let i = 0; i < 100000; i++) { | |
t = nextPrime(t); | |
result.push(t); | |
} | |
console.timeEnd(`log`); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment