Skip to content

Instantly share code, notes, and snippets.

@sunjayaali
Last active November 10, 2018 16:25
Show Gist options
  • Save sunjayaali/172a8a03921018b332faf5c8a9b2ffba to your computer and use it in GitHub Desktop.
Save sunjayaali/172a8a03921018b332faf5c8a9b2ffba to your computer and use it in GitHub Desktop.
Prime Numbers
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