Created
July 1, 2020 12:53
-
-
Save 73nko/a7f4fe75c7ddcf3b4152c8999cb30c63 to your computer and use it in GitHub Desktop.
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
/** | |
* @param {number} maxNumber | |
* @return {number[]} | |
*/ | |
export default function sieveOfEratosthenes( | |
maxNumber | |
) { | |
const isPrime = new Array(maxNumber + 1) | |
.fill(true); | |
isPrime[0] = false; | |
isPrime[1] = false; | |
const primes = []; | |
for ( | |
let number = 2; | |
number <= maxNumber; | |
number += 1 | |
) { | |
if (isPrime[number] === true) { | |
primes.push(number); | |
let nextNumber = number * number; | |
while (nextNumber <= maxNumber) { | |
isPrime[nextNumber] = false; | |
nextNumber += number; | |
} | |
} | |
} | |
return primes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment