Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
FREEDOM251

Freedom251 › Blog › Algorithms

Sieve of Eratosthenes: Find All Primes Up to a Limit

By Freedom251 Team · · 3 min read

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

The Sieve of Eratosthenes finds every prime number up to a chosen limit by repeatedly crossing out multiples of smaller primes. The numbers left unmarked are prime. It is useful when you need a complete list of primes in a range, rather than a yes-or-no test for just one number.

What is the Sieve of Eratosthenes?

It is a method for finding all prime numbers up to a specified number. NIST’s Dictionary of Algorithms and Data Structures defines it as “An algorithm to find all prime numbers up to a certain N.” NIST’s entry describes the basic purpose; the method is named for the ancient Greek mathematician Eratosthenes.

As an Amazon Associate I earn from qualifying purchases.

A prime number is an integer greater than 1 whose only positive divisors are 1 and itself. The sieve begins with the integers from 2 through the chosen limit. It identifies the smallest number not yet crossed out as prime, then crosses out that number’s larger multiples as composite.

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

How does the sieve work?

  1. List the candidates. Write down every integer from 2 through the inclusive upper limit, N. Treat them all as unmarked initially.
  2. Start with 2. Since it is the first unmarked candidate, it is prime. Cross out its multiples greater than itself: 4, 6, 8, and so on, through N.
  3. Move to the next unmarked integer. That candidate is prime. Cross out its larger multiples.
  4. Repeat through candidates whose squares are no greater than N. After that, the remaining unmarked numbers are prime.

For example, to sieve through 30, first cross out multiples of 2, then multiples of 3. The next unmarked candidate is 5; cross out its larger multiples as well. Continue with the next eligible unmarked candidate, 7. Since 7² is greater than 30, no further candidate needs to be processed. The unmarked numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.

Why is it enough to stop at the square root?

Every composite number at most N has a prime factor no greater than √N. When the sieve processes that prime factor, it crosses out the composite number as one of its multiples. Therefore, once the next candidate is greater than √N, any composite in the list has already been marked. Carnegie Mellon University’s explanation discusses this stopping rule and the algorithm’s correctness: Primes.

The same reasoning explains why unmarked numbers are prime: if one were composite, it would have a prime factor no greater than its square root, and that factor would already have caused it to be crossed out.

What happens at the boundaries?

  • 2 is included: it is the smallest prime and the sieve starts there, not at 1.
  • The upper limit is included: if N is prime, it remains in the result.
  • If N is less than 2: there are no primes in the requested range.

The University of North Carolina at Greensboro’s instructional explanation likewise treats the sieve as finding primes through a chosen bound: Sieve of Eratosthenes, Section 10.2.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How much time and memory does it use?

The ordinary array-based sieve takes O(n log log n) time and O(n) space for an upper limit n, according to The Prime Pages’ glossary entry. In practical terms, its simplicity makes it a standard way to generate all primes in a bounded range, but its array grows with the range. A segmented sieve can reduce working memory when processing a large interval; the appropriate approach depends on the interval size, available memory, and whether you need all primes or only want to test one number. The cited material does not establish a universal performance crossover between versions.

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.

Algorithmsmathematicsprime numbersSieve of Eratosthenes

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.