A student marks a number grid while using the Sieve of Eratosthenes to find prime numbers.

How the Sieve of Eratosthenes Finds Prime Numbers

The Sieve of Eratosthenes finds every prime up to a limit by crossing out composite-number patterns instead of testing numbers one by one.

Prime numbers are easy to define and surprisingly awkward to find one at a time. A prime is a whole number greater than 1 with exactly two positive factors: 1 and itself. That definition makes checking 7 simple, but a list of hundreds or thousands of numbers quickly turns repeated division into slow, error-prone work.

The Sieve of Eratosthenes solves a different problem. Instead of asking whether each number is prime, it writes down an entire range and removes numbers that must be composite. What remains is the complete list of primes up to the chosen limit. The method is more than two thousand years old, yet the U.S. National Institute of Standards and Technology still defines it as an algorithm for finding every prime up to a given number. Its staying power comes from a beautiful idea: it is often easier to eliminate what cannot qualify than to test every candidate from scratch.

Prime numbers hide among their multiples

Every whole number greater than 1 is either prime or composite. A composite number can be written as a product of smaller whole numbers. For example, 21 is composite because 3 Γ— 7 = 21, while 23 is prime because no whole number other than 1 and 23 divides it evenly.

The sieve turns that distinction into a pattern. If 2 is prime, then every later multiple of 2 must be composite. The same is true for 3, 5, 7, and every other prime. Crossing out those multiples removes whole families of composite numbers in a few organized passes.

The number 1 needs special treatment. It is neither prime nor composite because it has only one positive factor, not two. Excluding 1 also keeps prime factorization unique. For instance, 30 can be written as 2 Γ— 3 Γ— 5; allowing endless extra factors of 1 would make that representation needlessly ambiguous.

This already reveals two useful facts. First, 2 is the only even prime, since every larger even number is a multiple of 2. Second, a number that survives one pass is not automatically prime. After removing multiples of 2, the number 9 remains, but the pass for 3 will remove it. The sieve earns its answer gradually.

Work the sieve from 1 to 50

Start with a grid containing the whole numbers from 1 through 50. Cross out 1, then move to 2, the first number still available. Keep 2 and cross out every greater multiple of 2: 4, 6, 8, 10, and so on through 50.

  1. Use 2: Cross out all larger even numbers.
  2. Move to 3: It has not been crossed out, so it is prime. Cross out 6, 9, 12, 15, and the other multiples of 3.
  3. Skip 4: It is already crossed out, so it cannot begin a new prime pass.
  4. Use 5: Cross out its larger multiples. Several, such as 10, 15, and 20, are already gone; that repetition is harmless.
  5. Use 7: Cross out 14, 21, 28, 35, 42, and 49.

After the 7-pass, the uncrossed numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, and 47. Those are all the primes up to 50. Notice that the method found them as a set. It never had to ask separately whether 37, 41, 43, or 47 was prime.

A student uses handwritten math notes to track multiples and prime numbers.
Systematic marking is faster than testing every number from scratch.

A cleaner version of the same process begins crossing out at the square of each prime. When the 5-pass begins, for example, 10, 15, and 20 have already been removed as multiples of 2 or 3. The first new composite that 5 can reveal is 25, or 5 Γ— 5. For 7, the first useful starting point is 49. This small adjustment avoids repeating work without changing the result.

Why the square-root stopping point works

The sieve does not need a crossing-out pass for every prime below the limit. To find primes through 50, it is enough to process primes no larger than the square root of 50, which is a little more than 7. That is why the work stops after 7 rather than continuing with 11, 13, and 17.

The reason comes from factor pairs. Suppose a composite number at or below 50 had no factor at or below √50. Both factors would then be greater than √50, so their product would be greater than 50. That contradicts the starting condition. Every composite at or below 50 must therefore have at least one factor no greater than √50.

Consider 46. Its factor pair 2 Γ— 23 includes a small factor, so the 2-pass removes it. The number 49 is 7 Γ— 7, so the 7-pass catches it at the edge of the stopping rule. A number such as 47 has no smaller prime factor and survives. Once all primes through 7 have been processed, every composite through 50 has already had a chance to be crossed out.

This square-root idea appears elsewhere in number theory. When testing a single number for primality by trial division, there is no need to try possible factors beyond its square root. The sieve applies the same logic across a whole interval, sharing the work among many numbers.

Handwritten number patterns show how an algorithm removes composite numbers and leaves primes.
Each surviving number begins a new round of crossing out its multiples.

From a paper grid to a computer algorithm

A computer follows the same logic with a list of markers. It begins by treating the numbers from 2 to the limit as possible primes. Starting with 2, it marks that prime’s multiples as composite, moves to the next unmarked number, and repeats until it passes the square root of the limit. The unmarked positions then identify the primes.

That makes the sieve a useful bridge between mathematics and computing. It has a clear input, a repeated procedure, a stopping condition, and a definite output. It also shows why algorithm design is not only about getting the right answer. Starting each pass at the prime’s square reduces duplicate work, and skipping already marked numbers avoids treating composites as new sieving values.

For a basic implementation, the amount of memory grows with the chosen limit because the program stores a marker for each number. The classic sieve is very fast for generating all primes across a moderate range, with running time commonly described as proportional to n log log n. For an enormous interval, programmers may use a segmented sieve, processing smaller blocks instead of storing the entire range at once.

The method has limits. It is not primarily a tool for factoring one large number, and building a grid up to a huge candidate may waste memory if the only question is whether that one number is prime. Different primality tests are better suited to that task. The right method depends on whether the goal is one verdict, a factorization, or a complete prime list.

Common mistakes that change the result

Most sieve errors come from losing track of what the marks mean. Crossing out the starting prime itself removes a correct answer. Treating 1 as prime begins the process with the wrong definition. Stopping before the square root can leave a square such as 49 uncrossed, while continuing far beyond it adds work without finding any new composites.

  • Keep the prime that starts each pass; cross out only its larger multiples.
  • Move to the next uncrossed number, not simply the next number.
  • Begin at the prime’s square when earlier multiples have already been handled.
  • Process every prime up to and including the square root of the limit.
  • Read the remaining uncrossed numbers as the final prime list.

The Sieve of Eratosthenes makes prime numbers visible as survivors of a carefully designed elimination process. It replaces many isolated division problems with one connected pattern, explains why composite numbers disappear, and shows how a mathematical proof can guide an efficient algorithm. On paper, it turns a number grid into a map of primes. In code, the same idea becomes a compact example of how good algorithms save work by recognizing structure.

Have any questions or need more information on the topics covered? Get quick answers, further details, or clarifications by chatting with our AI assistant, Novo, at the bottom right corner of the page.

Akshay Dinesh

As a student, I am dedicated to writing articles that educate and inspire others. My interests span a wide range of topics, and I strive to provide valuable insights through my work. If you have any questions or would like to reach out, feel free to contact me at akshay[at]novolearner.com

πŸ“˜ Free Tutoring – By Students, For Students

πŸŽ“ Get completely free, personalized tutoring from high school and college students who understand what it’s like to be a learner today.

Just tell us your grade and subject(s) - we’ll follow up within 24 hours with your class info.

πŸ‘‰ Book your free class here

Like what we do?

Consider donating to us. Running a free educational website has its costs. We never charge our users a fee to access our content. However, we still have to foot our bills. Please help us do more. Any amount is appreciated.

Your Support Matters

We noticed you're using an ad blocker. Our website depends on ad revenue to keep our content free and accessible to everyone. Please consider disabling your ad blocker to support us and help us continue providing valuable content.

Advertisement

Advertisement

Advertisement

Advertisement

Advertisement

Advertisement