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.
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".
Enter a positive integer: 20 Primes less than 20: [2, 3, 5, 7, 11, 13, 17, 19] # of marks made: 16 |
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 | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
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:
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) |
The following section includes hyperlinks to the Java infrastructure needed to support the use of positional lists with cursors.
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.
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.