The Prime Number Seive

Due Wednesday of 3rd Week (day of the Midterm), at 11:59pm.

Please turn in a printed out copy of your code in addition to the electronic version. This way, the grader can give you comments.

In this assignment, you will use a specific algorithm to determine all of the prime numbers from 0 to some given number n.

  1. Create an array that will hold the information about whether or not each of the n numbers is prime (think carefully about the type for this array)
  2. For each number, determine if it is prime. A number is prime if the only two numbers that divide it are 1 and itself. For this step you may notjust go through a the numbers and get rid of all the multiples (ie you can't just get rid of all the multiples of 2, then the multiples of three and so on). You need to take each number individually and check it's primality. If you get this working, it is worth 10 points.
  3. The above process takes n2 steps: for each of the n elements, you check to see if it is divisible by any of the other elements. You can cut down on this a bit if you only check to see if a number is divisible by any number smaller than it (ie 24 will never be divisible by 25 or anything larger). You can actually get more efficient than that. For more points
    1. Use a more limited range of numbers (ie stop before you get to the actual number) (5 points)
    2. Use only a subset of the numbers in that range. (5 points)
    There are very specific solutions to the two above ideas. In your code, please write a comment that indicates what the range of numbers is and what the subset of the numbers is.
To do square root, include java.lang.Math. the square root function is
sqrt(double)
. here is a link to more info.

Reading

Savitch: all of chapter 3