ISC Practicals and Theory - Sieve of Eratosthenes
Updated 21 Oct 2026
The Sieve of Eratosthenes is a method of finding every prime number up to a limit $n$ without testing each number separately. We write down all the numbers from 2 to $n$, then cross out the multiples of each prime in turn. The numbers that are never crossed out are the primes. This program, written in the main method alone, reads $n$, crosses out composites in a boolean array and prints all the primes up to $n$ together with their count.
Definition
A prime number is a number greater than 1 whose only factors are 1 and itself. A composite number is a number greater than 1 that has some other factor.
The sieve works because every composite number is a multiple of a smaller prime. If we cross out the multiples of 2, then of 3, then of 5 and so on, only primes remain.
For $n = 30$:
- Cross out the multiples of 2: 4, 6, 8, ..., 30.
- The next number not crossed out is 3. Cross out its multiples: 9, 15, 21, 27 are new (the rest are already gone).
- The next is 5. Cross out 25 (30 is already gone).
- The next is 7, but $7 \times 7 = 49$ is more than 30, so nothing is left to cross out.
- Primes left: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, so there are 10 primes.
Why start from $p \times p$: a multiple $p \times k$ with $k < p$ has the smaller factor $k$, so it was already crossed out when we handled the primes below $p$. The first multiple of $p$ that can be new is $p \times p$. For the same reason we only need to take $p$ while $p \times p \le n$.
Range: the program accepts $n$ from 2 to 1000, and otherwise prints an error message.
Algorithm
- Read
n. If it is not from 2 to 1000, print an error message and stop. - Create the
booleanarraycompositeof sizen + 1. All elements arefalseat the start: nothing is crossed out. - For
pfrom 2 whilep * p <= n: ifcomposite[p]is stillfalse, thenpis prime. Cross out its multiples, starting fromp * p(the smaller multiples were crossed out by smaller primes), by settingcomposite[m] = trueform = p * p, p * p + p, ...up ton. - Go through
ifrom 2 tonand print everyiwithcomposite[i]stillfalse; these are the primes. Count them incount. - Print the count of primes.
Program
In main, the nested for loops cross out the multiples of each prime and the last for loop prints the numbers that were not crossed out.
import java.util.Scanner;
public class Sieve
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); // reads the input from the keyboard
System.out.print("Enter n (2 to 1000): ");
int n = sc.nextInt(); // upper limit up to which primes are to be found
if (n < 2 || n > 1000)
{
System.out.println("Invalid input. n must be from 2 to 1000.");
}
else
{
boolean[] composite = new boolean[n + 1]; // composite[i] is true once i has been crossed out; all false at the start
for (int p = 2; p * p <= n; p++)
{
if (!composite[p]) // p is still not crossed out, so p is prime
{
for (int m = p * p; m <= n; m = m + p)
{
composite[m] = true;
}
}
}
int count = 0; // number of primes found
System.out.println("Prime numbers up to " + n + ":");
for (int i = 2; i <= n; i++)
{
if (!composite[i])
{
System.out.print(i + " ");
count++;
}
}
System.out.println();
System.out.println("Count of primes = " + count);
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the numbers typed by the user |
n | int | Upper limit up to which primes are to be found |
composite | boolean[] | composite[i] is true once i has been crossed out |
p | int | The number being tested as a prime, from 2 while p * p <= n |
m | int | A multiple of p that is being crossed out |
count | int | Number of primes found |
i | int | Loop counter used to print the primes |
Sample input and output
Sample 1
Enter n (2 to 1000): 30
Prime numbers up to 30:
2 3 5 7 11 13 17 19 23 29
Count of primes = 10Sample 2
Enter n (2 to 1000): 2
Prime numbers up to 2:
2
Count of primes = 1Sample 3
Enter n (2 to 1000): 97
Prime numbers up to 97:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Count of primes = 25Sample 4
Enter n (2 to 1000): 1
Invalid input. n must be from 2 to 1000.Dry run
Input: n = 30. The array composite has indexes 0 to 30, all false at the start.
p | p * p <= 30 | composite[p] before | Action | Numbers newly crossed out |
|---|---|---|---|---|
| 2 | 4 <= 30, true | false | m = 4, 6, 8, ..., 30 | 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 |
| 3 | 9 <= 30, true | false | m = 9, 12, 15, 18, 21, 24, 27, 30 | 9, 15, 21, 27 |
| 4 | 16 <= 30, true | true | p is crossed out, skipped | none |
| 5 | 25 <= 30, true | false | m = 25, 30 | 25 |
| 6 | 36 <= 30, false | loop ends |
The printing loop now goes through i = 2 to 30 and prints those not crossed out: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29. Here count goes from 0 to 10, so the last line is Count of primes = 10. This matches the output of the first sample run.
Variations
- Range "Print all prime numbers between m and n using the Sieve of Eratosthenes."
- Twin primes "Using the sieve, print all pairs of twin primes (primes that differ by 2) up to n."
- Sum of primes "Find the sum of all primes up to n using the sieve."
- Largest prime "Find the largest prime number not more than n using the sieve."