Handout #28
CS 106A
July 21, 2009
Section Handout #4—Arrays & Memory
Thanks to Eric Roberts for some of these problems, and shouts out to Brandon Burr for some of them too
1.
Simple arrays
In the third century B.C., the Greek astronomer Eratosthenes developed an algorithm for
finding all the prime numbers up to some upper limit
N
.
To apply the algorithm, you start
by writing down a list of the integers between 2 and
N
.
For example, if
N
were 20, you
would begin by writing down the following list:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
You then begin by circling the first number in the list, indicating that you have found a
prime.
You then go through the rest of the list and cross off every multiple of the value
you have just circled, since none of those multiples can be prime.
Thus, after executing
the first step of the algorithm, you will have circled the number 2 and crossed off every
multiple of two, as follows:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
From here, you simply repeat the process by circling the first number in the list that is
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 SAHAMI,M
 Prime Numbers, Prime number, Greek astronomer Eratosthenes, upper limit N.

Click to edit the document details