Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

The Sieve of Eratosthenes finds all prime numbers up to a chosen limit by marking the multiples of each prime as composite. The numbers left unmarked are the primes. It is useful when you need a list of primes in a range, rather than a yes-or-no primality test for just one number.

What is the Sieve of Eratosthenes?

The Sieve of Eratosthenes is an algorithm for finding every prime number up to a specified upper bound. NIST defines it as “An algorithm to find all prime numbers up to a certain N.” NIST Dictionary of Algorithms and Data Structures

A prime number is an integer greater than 1 with no positive divisors other than 1 and itself. The sieve starts with the integers from 2 through the chosen limit, then eliminates numbers that are multiples of smaller primes. The unmarked numbers are prime.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How does the sieve work?

  1. Make a list: Write down the integers from 2 through the inclusive limit, N. Treat every number as unmarked at first.
  2. Start with 2: It is the smallest unmarked number, so it is prime. Mark its multiples greater than 2 as composite.
  3. Move to the next unmarked number: That number is prime. Mark its multiples greater than itself.
  4. Stop when the candidate exceeds √N: At that point, every composite number through N has already been marked.
  5. Read the result: All remaining unmarked numbers are primes.

For example, to sieve through 30, mark multiples of 2, then multiples of 3, then multiples of 5. Since 5² is at most 30 but 7² is greater than 30, there is no need to process 7 as a candidate. The unmarked numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.

Marking can begin at the square of each prime instead of at twice that prime: any smaller multiple has a smaller prime factor and will already have been marked. This optimization is described in the Carnegie Mellon University explanation.

Why is it enough to check candidates through the square root?

Every composite number can be written as a product of two smaller positive integers. At least one of those factors must be no larger than the number’s square root; otherwise, their product would exceed the number. That factor has a prime divisor no larger than itself. Therefore, every composite number at most N has a prime factor at most √N, and the sieve will mark it while processing that prime.

Conversely, an unmarked number has not been identified as a multiple of any smaller prime. It cannot be composite, so it is prime. This square-root bound is what lets the algorithm stop processing candidates before it reaches the end of the list. UNC Greensboro’s explanation also describes the procedure and its bound.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What are the edge cases and complexity?

  • The limit is inclusive: If N is prime, include N in the result.
  • Limits below 2: There are no primes in the requested range, because 1 is not prime.
  • Time: The ordinary array-based sieve runs in O(n log log n) time for a limit n.
  • Space: It uses O(n) space to track which numbers are marked. The Prime Pages gives the same asymptotic complexity for the standard sieve.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When should you use it?

Use the basic sieve when you need all primes within a bounded range and the range can fit comfortably in memory. Its marking process is straightforward to understand and implement. If you only need to determine whether one number is prime, generating every prime up to that number may do more work than necessary. For a very large interval where storing a full range is impractical, a segmented sieve can reduce working memory by processing portions of the range; the basic sieve remains the simpler teaching example.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.