V22.0101 ..... Homework 6 ..... Fall 2006
Primes

Assigned: Wed Nov 1
Due: Thurs Nov 9 at midnight.

The Sieve of Eratosthenes is an algorithm for finding all prime numbers less than or equal to a given number N. The idea is to test N only against smaller prime numbers, instead of all smaller numbers. We need an array from 2 to N, but arrays must start at 0 in Java so we start the array at 0 and just ignore or "cross out" the first two slots; thus the size of the array is N+1. Repeatedly do the following: find the smallest integer, I, that is not crossed out, and "cross out" all multiples of I (2I, 3I, etc), continuing until I > sqrt(N). For a reason that will be explained below, use an int array instead of a Boolean array, initialized to contain zeros, and when "crossing out" position K because K is a multiple of I, SAVE I in position K.

Write a program which implements the following, either as static methods or instance methods, as you prefer, and organized into classes as you think best.

Make a conjecture, based on the output of your program, about when prime numbers can be expressed as sums of squares, and include it in your program comments. Hint: it has something to do with K mod 4 (the remainder after dividing K by 4).

Finally, how big N can your program handle in a reasonable amount of time? How big does this compare with the "brain-dead" methods for computing primes?