Sieve of Eratosthenes is an algorithm to find all prime numbers up to a given limit. Learn how to implement it in JavaScript.
The Sieve of Eratosthenes is an algorithm for finding all prime numbers up to some limit n.
It is attributed to Eratosthenes of Cyrene, an ancient Greek mathematician.
When the algorithm terminates, the numbers remaining true in the array are all the primes below n.
An improvement of this algorithm is, in step 4, start marking multiples of p from p * p, and not from 2 * p. The reason why this works is because, at that point, smaller multiples of p will have already been marked false.
The algorithm has a complexity of O(n log(log n)).
The Original Article can be found on
#javascript #algorithms #datastructures #math