homework0

advertisement
TCSS 143, Autumn 2004
Homework 0: Sieve of Eratosthenes (Review of Java Concepts)
Assigned:
Due:
Wed 2004/09/29
Tue 2004/10/05, 11:59:59pm
This assignment is designed to refresh you on Java programming, such as loops, arrays, classes, and input using
Scanner. In addition, this assignment serves to make sure that every student has his/her account set up correctly
and can complete the turnin process.
Background:
A prime number is a positive integer that has no factors other than 1 and itself. Another way to say this is that
for a prime number p, if a * b = p, then either (a = 1 and b = p) or (a = p and b = 1). For example, 12 is not a
prime number because 3 * 4 = 12 and 6 * 2 = 12. But 23 is a prime number, because there are no factors for 23
other than 1 and 23. The number 2 is considered to be the smallest prime number; 1 itself does not count.
Prime numbers are important in several areas of computer science such as cryptography.
There are several ways to determine whether a number is prime. One such algorithm is called the Sieve of
Eratosthenes. Eratosthenes was a Greek mathematician around 200 BC. In fact, Eratosthenes' algorithm is very
efficient for creating lists of prime numbers up to a given maximum. The algorithm is as follows:
Consider the list of integers between 1 and max. (For this example, we will use max = 25.)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Start out by marking all multiples of 2 (but not 2 itself):
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
x
x
x
x
x
x
x
x
x
x
x
Move to the next unmarked number, which in this case is 3, then mark all its multiples (again, but not 3 itself):
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
x
x
x x x
x
x x x
x
x x x
x
Continue in this fashion, marking all multiples of the next unmarked number until there are no new unmarked
numbers. The numbers that survive this marking process are primes.
The following is a rough pseudo-code sketch of this algorithm as a computer program. Recall that [m, n] means
the range of integers between m and n, inclusive:
algorithm sieve(max):
- create list of markers, of size max
- for each i in [2, max], do:
- for each j in [2*i, max] by increments of i, do:
- mark number j in the list
- output every number that is not marked
1/2
Program Semantics:
When your program runs, it should print the prompt, Enter a maximum integer > 0: to the user and
read an integer from the keyboard console. After the user types their maximum integer and presses Enter, your
program should print out a message for every prime number up to and including that maximum. The following
is an example of the output (user input is in bold italic):
Enter a maximum integer > 0: 25
2 is prime!
3 is prime!
5 is prime!
7 is prime!
11 is prime!
13 is prime!
17 is prime!
19 is prime!
23 is prime!
If no prime numbers are found up to the given maximum integer, your program should print the message, No
primes found to the user. The following is an example of the output:
Enter a maximum integer > 0: 1
No primes found.
Note that since prime numbers are positive, a negative maximum value should result in no primes being found.
Program Specification:
Write a class named Sieve in a file Sieve.java. Your Sieve class should contain the following methods:
public static void main(String[] args)
Runs the program as outlined previously, prompting the user for the maximum value and then invoking
printAllPrimes. The input should be read from System.in using a Scanner.
public static void printAllPrimes(int max)
Finds all primes up to the given maximum, inclusive, using the Sieve of Eratosthenes algorithm described
previously.
Grading:
The correctness of your program will be graded on whether its output matches exactly the output as specified
above, including printing the right prime numbers for the given maximum value, and printing the message in
the proper format including whitespace. Your code will be tested with a variety of input values,
The style and design of your program will be graded on whether you follow the program specification above,
whether you actually implement the Sieve of Eratosthenes algorithm as described, whether you use the Scanner
for input as described, the presence of reasonable brief comments in your source code, and general spacing,
indentation, and indentation of your code.
This program should be completed individually. It is subject to the plagiarism and conduct rules specified in
the course syllabus.
2/2
Download