Homework # 6

Due: Oct. 22, 2004 (F)

Your assignment is to write a program to find all the prime numbers between 1 and 999. A prime number is any integer that can be divided evenly only by itself and 1. Use the Sieve of Eratosthenes method to find the prime numbers. The Sieve of Eratosthenes works as follows:

  1. Create an array with all elements initialized to 1 (true). Array elements with prime subscripts will remain 1. All other array elements will eventually be set to zero.
  2. Starting with array subscript 2 (subscript 1 is prime), every time an array element is found whose value is 1, loop through the remainder of the array and set to zero every element whose subscript is a multiple of the subscript for the element with value 1. For array subscript 2, all elements beyond 2 in the array that are multiples of 2 will be set to zero (subscripts 4, 6, 8, etc.). For array subscript 3, all elements beyond 3 in the array that are multiples of 3 will be set to zero (subscripts 6, 9, 12, etc.).
When this process is complete, the array elements that are still set to one indicate that the subscript is a prime number. These subscripts can then be printed. Write a program that uses an array of 1000 elements to determine and print the prime numbers between 1 and 999. Ignore element 0 of the array.

Again, use the Linux system to edit, compile, execute, and debug your programs.

To submit your assignment, click on the link at the bottom of the homework page for the course: http://www.cs.uni.edu/~fienup/cs036f04/homework/index.htm

You will need to

  1. log on to the submission system using your CNS account information
  2. select the "810:036, C++ Fienup" course
  3. select the radio button corresponding to lab being submitted
  4. enter the name of the file you wish to submit which needs to be accessable on the computer from which you are submitting