Skip to content

Instantly share code, notes, and snippets.

@73nko
Created July 1, 2020 12:53
Show Gist options
  • Save 73nko/a7f4fe75c7ddcf3b4152c8999cb30c63 to your computer and use it in GitHub Desktop.
Save 73nko/a7f4fe75c7ddcf3b4152c8999cb30c63 to your computer and use it in GitHub Desktop.
/**
* @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