Solution

advertisement
CmSc 250 Fundamentals of Computing III
Homework 02 - SOLUTION
The algorithm for generating consecutive primes not exceeding a given integer n was
probably invented in ancient Greece and is known as the sieve of Eratosthenes (ca. 200
B.C.)
1. Search for the algorithm on the Web (use the links provided on our class web
page). Find at least three descriptions of the algorithm.
Definition: An algorithm to find all prime numbers up to a certain N. Begin with an
(unmarked) array of integers from 2 to N. The first unmarked integer, 2, is the first
prime. Mark every multiple of this prime. Repeatedly take the next unmarked integer
as the next prime and mark every multiple of the prime
To optimize, when we find the prime n, we can begin marking at n ², since any composite
less than that is a multiple of a lesser prime, and so will have been marked earlier. As a
corollary, we can stop marking when n ² is greater than our range. That is, any unmarked
numbers greater than the square root of the range are primes.
2. Study the algorithm. Using the algorithm, find the primes from 1 to 20 to make
sure you have understood it.
3. Design appropriate data structures to implement the algorithm.
An array of integers A[N+1], initialized with 1. A[0] and A[1] are not considered.
At the end of the algorithm if A[j] =1, then j is a prime number, otherwise j is not a
prime number, j > 1.
4. Describe the basic operations in the algorithm. These operations will be
implemented at some later time.
Basic operation: change the value of a cell from 1 to 0.
5. Write the algorithm in pseudocode using the designed data structures and the
basic operations (of course you will use also other operations , e.g. incrementing a
variable, however the pseudocode has to be built on the basic operations that you
have described in the previous task).
1
// Finding all prime numbers in the range [2.. N]
ALGORITHM Sieve_of_ Eratosthenes(N)
1. Initialize array of size N+1 with A[0] = A[1] = 0, A[k] = 1, k > 1, k < N+1
2. for k = 2 while k*k <= N do
2.1. if A[k] = 1
2.1.1. j = 2; m = k
2.1.2. while m*j < N+1
A[m*j] = 0
j=j+1
3. for k = 2 to N do
if A[k] = 1 print k // k is a prime number
6. Trace the algorithm on paper for n = 20. Write down each step of the trace.
Step 1:
0
0
1
0
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
1
10 11 12 13 14 15 16 17 18 19 20
1 1 1 1 1 1 1 1 1 1 1
Step 2. k = 2
Step 2.1. if condition is true
Step 2.1.1
j = 2, m = 2
Step 2.1.2
Store 0 in the cell whose index is a multiple of 2
0
0
1
0
2
1
3
1
4
0
5
1
6
0
7
1
8
0
9
0
10 11 12 13 14 15 16 17 18 19 20
0 1 0 1 0 0 0 1 0 1 0
Step 2: k = 3
Step 2.1. if condition is true
Step 2.1.1
j = 2, m = 3
Step 2.1.2
Store 0 in the cells whose index is a multiple of 3
0
0
1
0
2
1
3
1
4
0
5
1
6
0
7
1
8
0
9
0
10 11 12 13 14 15 16 17 18 19 20
0 1 0 1 0 0 0 1 0 1 0
Step 2. k = 4
Step 2.1. if condition is false : A[4] is not 1
Step 2. k = 5; loop condition becomes false (5*5 is greater than 20), we exit the
loop
Step 3: print the indexes of all elements equal to 1:
2, 3, 5, 7, 11, 13 ,17, 19
2
Download