HW 03

advertisement
CmSc 250 Fundamentals of Computing III
Homework 03, due 09/18
Implement the algorithm for generating consecutive primes not exceeding a given integer
n ( the sieve of Eratosthenes ) using
a) your pseudocode
b) one of the pseudocodes listed below (do not choose your pseudocode)
Students that have not turned HW2 have to choose two of the pseudocodes listed below.
1. Adam’s:
Create an array of Booleans and set each to true
Find the square root of the size and store in a variable.
Find all multiples of two
Sets a variable j at 2
Using a loop, the index starts at j+j and runs while it is less then size, the index is
then incremented at i+j, this allows us to do the multiples of two first.
Sets all multiples of 2 to false.
Uses another loop starting at j=3, and runs to the square root of the size, j is
incremented by two each time.
If the number is prime does the following
Runs a for loop i=j+j, runs while i is less than size, then increments
by j
Sets the multiples of the number to false
Displays the results
2. Chad’s
get n; //from user
set up array A[N];
for (I = 0; I < N; I++);
A[I] = I;
for ( I = 2; I < N; I++ )
{
for(int x = I; x< N; x++)
{
int m = I * x ;
A[m] = 0;
}
}
for (I = 0; I < n; I++)
if (A[I] != 0)
print A[I]
1
3. Kevin’s
Declare initial size = N;
Declare array of type Boolean of initial size [N];
Set all fields in array true;
For(int I =2; I < prime.length; i++)
Prime[i] = true;
Do the sieve
Run through loop for whole array
For(int k = 2; k < prime.length; k++)
If k is prime, true, then mark all it’s multiples false
Print the array
Run through whole array
For(int I = 2; I < N; i++)
Print only array index that contains true statement
If (prime[i] == true)
Print prime[i]
End program
4. Luc’s
Description of the data structures
To implement the sieve of Eratosthenes, we will use 2 stacks of integers with one of the arrays containing
the indices of the memory locations of the second array; the second array containing the number of integers
up to which we want to find the prime numbers.
Description of the basic operations
If we want to compute the prime numbers up to the number n, we create an array numbers[ ] of size n. We
start with the number 2 and we fill the array with integers from 2 to n. We start by removing from the list
all the multiples of two. All of the numbers that give a nil remainder for the division by 2 are replaced in
the numbers[ ] array by -1. When we are done with the multiples of 2, the next number is p = 3. We check
2
for the condition p ≤ n. As long as this condition is not satisfied we keep repeating the same operation.
Pseudocode in C++
PROCESS
1- allocate memory locations for the array:
const int n = set by the programmer
in the main part of the program:
int numbers [n];
2- fill both arrays with integers from 2 up to n:
for (int i=2; i<n; i++)
{
2
numbers[i] = i;
}
3- start with p = 2 and as long as (numbers[i]%p == 0), replace numbers[i] by -1. when this step is
complete p becomes the next first number in the array different from -1 and we check the
2
condition the condition p ≤ n. if it is not the case we keep replacing numbers that satisfy the
condition (numbers[i]%p == 0) by -1 and we repeat the aforementioned steps, this is done in the
locations that contains numbers different from -1.
at the end we have n array in which the locations contain either prime numbers or -1.
OUTPUT
4- output the numbers contained in the memory locations that have not been inserted -1.
5. Patrick’s
get data from user
ending = the number input from the user as to how far it should be computed
(declaration)
long counter = 1 (declaration)
array[ending / 2] (declaration, used for keeping track of the prime numbers)
i = 0 (declaration, used for keeping track of next available position in array)
setup the ordered array with insertion, deletion, and membership operations
ordered_array (declaration)
populate ordered_array, standard for loop, adding one each time to create all the numbers
between 1 and “ending”
if (ending != 1)
{
while (counter !>= ending)
{
counter++
array[i] = counter
i++
go through and delete all multiples of “counter” in ordered_array using
deletion and membership operations
}
}
else
{
no prime numbers…
}
display the results
3
6. Sarah’s
The program starts with an array with all of the numbers from 1-N. 1 is not considered
prime or composite, so operations are started on 2. 2 is a prime, but all of its multiples
are not, so you go through the array and mark all multiples of 2 as a composite. 3, the
next number to not be marked is then marked as a prime and its multiples marked as
composite. Go through the list marking the next non marked number as a prime and then
marking its multiples as composite. Once the current number is greater than N there
are no more composite numbers on the list to mark, leaving only prime numbers
unmarked.
Eratosthenes(n)
{
a[1] = 0
for i = 2 to n do a[i] = 1
p = 2
while p2 < n do
{
j := p2
while (j < n) do
{
a[j] = 0
j = j+p
}
repeat p = p+1 until a[p] = 1
}
return(a)
}
What to turn in:
A. Implementation of your pseudocode
When implementing your pseudocode you may find necessary to modify it. If this
is the case, write down an explanation of the modifications and the reason for the
modifications. Write your pseudocode (previous or modified) in itemized form.
Put the pseudocode and accompanying descriptions (in case of modifications) in
comments at the beginning of your program.
In the source code write comments that refer explicitly to the pseudocode.
B. Implementation of someone else’s pseudocode
Rewrite the pseudocode you have chosen in itemized form. If something is not
clear in the pseudocode, refer to its author for explanations.
4
If it turns out that the pseudocode has to be modified, do the modifications
keeping the idea of the pseudocode. Write in comments at the beginning of the
program the pseudocode(itemized) and explanation of any modifications – what
modifications were done and why.
Turn in two source code files (C++) or two zipped projects (Java). Note: zip files are
not accepted Simpson’e server. Change the extension to ‘zip’ to ‘250’ and then send
the files as attachments.
Please follow the instructions of homework submission
Don’t forget to write your name in the program!
5
Download