Credit: M.qrius
The sieve of Eratosthenes is a simple algorithm for finding all prime numbers up to a specified maximum value. It works by identifying the prime numbers in increasing order while removing from consideration composite numbers that are multiples of each prime. This animation shows the process of finding all primes no greater than 120. The algorithm begins by identifying 2 as the first prime number and then crossing out every multiple of 2 up to 120. The next available number, 3, is the next prime number, so then every multiple of 3 is crossed out. (In this version of the algorithm, 6 is not crossed out again since it was just identified as a multiple of 2. The same optimization is used for all subsequent steps of the process: given a prime p, only multiples no less than p2 are considered for crossing out, since any lower multiples must already have been identified as multiples of smaller primes. Larger multiples that just happen to already be crossed out—like 12 when considering multiples of 3—are crossed out again, because checking for such duplicates would impose an unnecessary speed penalty on any real-world implementation of the algorithm.) The next remaining number, 5, is the next prime, so its multiples get crossed out (starting with 25); and so on. The process continues until no more composite numbers could possibly be left in the list (i.e., when the square of the next prime exceeds the specified maximum). The remaining numbers (here starting with 11) are all prime. Note that this procedure is easily extended to find primes in any given arithmetic progression. One of several prime number sieves, this ancient algorithm was attributed to the Greek mathematician Eratosthenes (d. c. 194 BCE) by Nicomachus in his first-century (CE) work Introduction to Arithmetic. Other more modern sieves include the sieve of Sundaram (1934) and the sieve of Atkin (2003). The main benefit of sieve methods is the avoidance of costly primality tests (or, conversely, divisibility tests). Their main drawback is their restriction to specific ranges of numbers, which makes this type of method inappropriate for applications requiring very large prime numbers, such as public-key cryptography.