Lab #11: Eratosthenes Sieve

The goal of this lab is to write a class Eratosthenes to execute the algorithm called Eratosthenes Sieve to find all prime numbers less than an integer specified by the user and then save the thus-obtained prime numbers in a file, which is also specified by the user. These two parameters, the limite and the file name, should be given as the arguments to the program:

% java Eratosthenes limit

In the main method of the program, limit can be obtained by executing Integer.parseInt( args[ 0 ] ) and is args[ 1 ].

The Eratosthenes algorithm can be explained as follows:

  1. Write on a piece of paper the numbers 2 through limit-1
  2. For p = 2, ..., limit-1, if p is not crossed out, then cross out all the multiples of p that are greater than p.

For example, if the limit is 16, we start with the numbers

2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

For p = 2, we cross out all the even numbers other than 2, and so the list of remaining numbers becomes:

2, 3, 5, 7, 9, 11, 13, 15

The next we do p = 3. We cross out 6, 9, 12, and 15. The list becomes:

2, 3, 5, 7, 11, 13

We skip p = 4 because p is already crossed out.

The next comes p = 5. We cross out 10 and 15, but since they are already crossed out, the list does not change. The same deal for the remaining values of p.

The final list is thus 2, 3, 5, 7, 11, 13. The numbers on this last list are the prime numbers below 16.

This process can be carried out using a boolean array as follows:

  1. Given limit, declare a boolean array of length limit. For each p = 2, ..., limit-1, the array has true at position p if and only if p has not been crossed out.
  2. Fill the array with true. This is the initial status of the list.
  3. For each p between 2 and limit-1, at each position that is a multiple of p, excluding p, set the value of the array at the position to false (corssing out).
  4. For each p between 2 and limit-1, if the array holds true at the position, print p to the output file with a newline.

Write your code in such a way that it prints out the value of p and each number that is crossed out for the first time.

Here is an example of executing the code:

% java Eratosthenes 50 primes.txt
p = 2
.. crossing out 4
.. crossing out 6
.. crossing out 8
.. crossing out 10
.. crossing out 12
.. crossing out 14
.. crossing out 16
.. crossing out 18
.. crossing out 20
.. crossing out 22
.. crossing out 24
.. crossing out 26
.. crossing out 28
.. crossing out 30
.. crossing out 32
.. crossing out 34
.. crossing out 36
.. crossing out 38
.. crossing out 40
.. crossing out 42
.. crossing out 44
.. crossing out 46
.. crossing out 48
p = 3
.. crossing out 9
.. crossing out 15
.. crossing out 21
.. crossing out 27
.. crossing out 33
.. crossing out 39
.. crossing out 45
p = 5
.. crossing out 25
.. crossing out 35
p = 7
.. crossing out 49
p = 11
p = 13
p = 17
p = 19
p = 23
p = 29
p = 31
p = 37
p = 41
p = 43
p = 47

Using the cat comand the output file generated by the execution can be seen:

% cat primes.txt
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47

Go back to the lab main page

Go back to the home page