CMPS 144 Spring 2019
Prog. Assg. #5: Eratosthenes' Sieve and Lists
Due: 11:59pm, April 29, 2019

Background

A well known algorithm for identifying the prime numbers, up to some ceiling, was devised by Eratosthenes of Cyrene, a Greek genius who lived during the 3rd century BC. His algorithm is known as the Sieve of Eratosthenes. To learn how it works, read Sections 1 (Overview) and 2.1 (Algorithm Pseudocode) of the Wikipedia entry.


Your Tasks

Task #1: Provided to you is the Java class EratosthenesSieve. It is intended mostly to serve as a model to help you in completing Task #2, but there is one broken method that you are to fix, the second version of primes(). (The comment "STUB!!" appears there.)

Extra Credit: In addition to that correction, two modest performance improvements can be made. Each one involves either increasing the lower bound or decreasing the upper bound of the range of values assumed by a for-loop control variable. (In both cases, the "avoided" loop iterations have no real effect, due to properties of numbers.) Consider these improvements to be worth a few points of "extra credit".

Sample Execution of EratSieveTester
Enter a positive integer: 20
Primes less than 20:
[2, 3, 5, 7, 11, 13, 17, 19]

# of marks made: 16
For the purpose of testing your work, provided is the EratSieveTester application. As you can see to the right, it prompts the user to enter a positive integer N and responds by listing the prime numbers up to, but not including, N. It also reports the number of times that numbers were "marked" as being non-prime, which is a good measure of running time. Thus, it can help in determining whether your extra-credit attempts were successful. (A number is marked as being non-prime once for each of its prime factors, which explains why the number of times that marks are made exceeds the number of non-prime numbers in the range [2..N).)

The EratosthenesSieve class is a bit unusual in that the bulk of its work is carried out by the constructor, which employs the Sieve Of Eratosthenes algorithm to fill the elements of the boolean array instance variable isPrime[] so that isPrime[k] is true iff k is a prime number.

Referring back to the sample execution of EratSieveTester, it would have created an instance of EratosthenesSieve whose isPrime[] array, upon completion of the constructor, looked like this (where f and t stand for false and true, respectively):

  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| f | f | t | t | f | t | f | t | f | f | f | t | f | t | f | f | f | t | f | t |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

Task #2: Where the purpose of an instance of the EratosthenesSieve class is to identify which integers in the range [2..N) are prime (for some N), the purpose of an instance of the (unfinished) EratosthenesLists class is to identify, for each k in the range [2..N), the prime factors of k. Thus, in analogy to an instance of EratosthenesSieve filling the elements of a boolean array, where the k-th element of the array indicates whether or not k is a prime number, an instance of EratosthenesLists fills an array of lists, where the k-th element of the array is a list containing the prime factors of k. The following shows what such a structure would look like, conceptually. Notice that each array element is a reference to a list of nodes, where each node contains a prime number. The element at location 30, for example, is a reference to a list containing the prime numbers 2, 3, and 5, as those are the prime factors of 30.

  0   1   2   3   4   5   6   7   8   9  10        30
+---+---+---+---+---+---+---+---+---+---+---+     +---+
|   |   | * | * | * | * | * | * | * | * | * | ... | * | ...
+---+---+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+     +-+-+
          |   |   |   |   |   |   |   |   |         |
          |   |   |   |   |   |   |   |   |         |
          V   V   V   V   V   V   V   V   V         V
         +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+       +-+
         |2| |3| |2| |5| |2| |7| |2| |3| |2|  ...  |2|  ...
         +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+       +-+
                          |               |         |
                          |               |         |
                         +-+             +-+       +-+
                         |3|             |5|       |3|
                         +-+             +-+       +-+
                                                    |
                                                    |
                                                   +-+
                                                   |5|
                                                   +-+

For the purposes of this assignment, your instructor has deemed that the lists be instances of a class that implements the PositionalListWithCursors interface. (Hyperlinks to that interface and other needed interfaces/classes are found below.)

You will notice that the EratosthenesLists class, as given (see hyperlink above), is unfinished in three ways:

Sample EratListsTester Execution
Enter positive integer: 16
2: (2)
3: (3)
4: (2)
5: (5)
6: (2, 3)
7: (7)
8: (2)
9: (3)
10: (2, 5)
11: (11)
12: (2, 3)
13: (13)
14: (2, 7)
15: (3, 5)
For the purpose of testing your EratosthenesLists class, provided is the Java application EratListsTester. It prompts the user to enter a positive integer N and responds by showing the prime factors of each positive integer up to, but not including, N. See sample execution to the right. (To test the toArray() method, you will have to "uncomment" two relevant lines of code.)

The following section includes hyperlinks to the Java infrastructure needed to support the use of positional lists with cursors.


Java Infrastructure for Task #2

Here are links to Java interfaces and classes that serve as "infrastructure" for Task #2 of this assignment. You are not to modify any of them.


Program Submission

Use the file submission system (see the link near the top of the course web page) to submit into the prog5_dir folder the source code of the Java classes that you were responsible for completing, namely the files EratosthenesSieve.java and EratosthenesLists.java.

Make sure to insert the standard comments near the top of each class. These should include your name, a list of names of persons with whom you collaborated, and a description of any defects. If there are defects of which you are unaware, it probably means that you did a poor job of testing your work. Thus, of two submissions having similar defects, one in which those defects are acknowledged deserves a better grade than one in which they are not.