Created
April 23, 2020 17:33
-
-
Save gkucmierz/97751f7f13088434531ca1f06465d8ad to your computer and use it in GitHub Desktop.
count primes - erastotenes sieve
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
const sieve = []; | |
const MAX = 10008; | |
for (let i = 2; i < MAX; ++i) { | |
for (let j = i * 2; j < MAX; j += i) { | |
sieve[j] = 1; | |
} | |
} | |
const primes = []; | |
for (let [p, i] = [0, 2]; i < sieve.length; ++i) { | |
if (!sieve[i]) { | |
primes[p++] = i; | |
} | |
} | |
console.log( | |
primes | |
.slice(-30) | |
// .slice(0, 10) | |
.join`\n` | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment