In mathematics, the sieve of eratosthenes is an algorithm for finding all prime numbers up to any given limit.
It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the first prime number, 2. The multiples of a given prime are generated as a sequence of numbers starting from that prime, with constant difference between them that is equal to that prime. This is the sieve's key distinction from using trial division to sequentially test each candidate number for divisibility by each prime. Once all the multiples of each discovered prime have been marked as composites, the remaining unmarked numbers are primes.
Using such algorithm to find primes consumes memory and operations
This is an animation to demonstrate how sieve works
Use the slider to set array size and the speed of animation
All prime numbers found are marked with green color