brriney
01-13-2008, 02:00 AM
I am beginning computer science major and I am completely lost with this. Can anyone help me figure these directions because they are very confusing at some parts. Any suggestions?
Write a program that calculates all prime numbers less than 10,000. The program works as follows:
1. Create a main method, and a method called ArrayList< Integer > sieve( int n ), which returns a list with all the prime numbers less than n. The main method calls sieve( 10,000 ), and prints out the results.
2. Inside of sieve(), do the following:
a. Call method createTrueArray( n ) to create a boolean array of size n.
b. Set cells 0 and 1 of the array to false. Each cell in the array represents a number, the boolean value represents whether it is prime or not. We start by assuming that all numbers greater or equal 2 are prime, and then we remove the ones that are not.
c. Loop over the array, starting at index 2.
d. First, remove all multiples of 2, by setting the values for 4, 6, 8, ... to false.
e. Then, look for the next number still marked true (in this case 3), and set all its multiples to false.
f. Repeat with all numbers that are marked true. When you are done, only prime numbers will be marked true.
g. Call method booleanArrayToIntList() to return a list of integers that contains the primes.
3. Method boolean[] createTrueArray( int n ) creates a boolean array of size n and initializes all its values to true.
4. Method ArrayList< Integer > booleanArrayToIntList( boolean[] booleans ) loops through booleans, and for each true value, it adds the corresponding number to a list. Finally, it returns the list.
Write a program that calculates all prime numbers less than 10,000. The program works as follows:
1. Create a main method, and a method called ArrayList< Integer > sieve( int n ), which returns a list with all the prime numbers less than n. The main method calls sieve( 10,000 ), and prints out the results.
2. Inside of sieve(), do the following:
a. Call method createTrueArray( n ) to create a boolean array of size n.
b. Set cells 0 and 1 of the array to false. Each cell in the array represents a number, the boolean value represents whether it is prime or not. We start by assuming that all numbers greater or equal 2 are prime, and then we remove the ones that are not.
c. Loop over the array, starting at index 2.
d. First, remove all multiples of 2, by setting the values for 4, 6, 8, ... to false.
e. Then, look for the next number still marked true (in this case 3), and set all its multiples to false.
f. Repeat with all numbers that are marked true. When you are done, only prime numbers will be marked true.
g. Call method booleanArrayToIntList() to return a list of integers that contains the primes.
3. Method boolean[] createTrueArray( int n ) creates a boolean array of size n and initializes all its values to true.
4. Method ArrayList< Integer > booleanArrayToIntList( boolean[] booleans ) loops through booleans, and for each true value, it adds the corresponding number to a list. Finally, it returns the list.