Download ppt

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Prime Numbers
Lecture L4.4
Sieve of Eratosthenes
Find prime numbers using
Sieve of Eratosthenes
// first create an array of flags, where
// each member of the array stands for a odd number
// starting with 3.
for (j = 0; j < size; j++)
{
flags[j] = 1;
}
size = 1024
j
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
Find prime numbers using
Sieve of Eratosthenes
primeCount = 0;
for (j = 0; j < size; j++)
{
if (flags[j] == 1)
{
// found a prime, count it
primeCount++;
// now set all of the multiples of
// the current prime number to false
// because they couldn't possibly be
a = 2*j + 3; // odd numbers starting
b = a + j;
while(b < size)
{
flags[b] = 0;
b = b + a;
}
}
}
j
0
1
2
3
4
5
6
7
8
prime
with 3 9
10
11
12
13
14
15
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
a
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
b
3
6
9
12
15
18
21
24
27
30
33
36
39
42
45
48
Find prime numbers using
Sieve of Eratosthenes
primeCount = 0;
for (j = 0; j < size; j++)
{
if (flags[j] == 1)
{
// found a prime, count it
primeCount++;
// now set all of the multiples of
// the current prime number to false
// because they couldn't possibly be
a = 2*j + 3; // odd numbers starting
b = a + j;
while(b < size)
{
flags[b] = 0;
b = b + a;
}
}
}
j
0
1
2
3
4
5
6
7
8
prime
with 3 9
10
11
12
13
14
15
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
a
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
b
3
6
9
12
15
18
21
24
27
30
33
36
39
42
45
48
Find prime numbers using
Sieve of Eratosthenes
primeCount = 0;
for (j = 0; j < size; j++)
{
if (flags[j] == 1)
{
// found a prime, count it
primeCount++;
// now set all of the multiples of
// the current prime number to false
// because they couldn't possibly be
a = 2*j + 3; // odd numbers starting
b = a + j;
while(b < size)
{
flags[b] = 0;
b = b + a;
}
}
}
j
0
1
2
3
4
5
6
7
8
prime
with 3 9
10
11
12
13
14
15
1
1
1
0
1
1
0
1
1
1
1
1
1
1
1
1
a
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
b
3
6
9
12
15
18
21
24
27
30
33
36
39
42
45
48
Find prime numbers using
Sieve of Eratosthenes
primeCount = 0;
for (j = 0; j < size; j++)
{
if (flags[j] == 1)
{
// found a prime, count it
primeCount++;
// now set all of the multiples of
// the current prime number to false
// because they couldn't possibly be
a = 2*j + 3; // odd numbers starting
b = a + j;
while(b < size)
{
flags[b] = 0;
b = b + a;
}
}
}
j
0
1
2
3
4
5
6
7
8
prime
with 3 9
10
11
12
13
14
15
1
1
1
0
1
1
0
1
1
0
1
1
1
1
1
1
a
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
b
3
6
9
12
15
18
21
24
27
30
33
36
39
42
45
48
Find prime numbers using
Sieve of Eratosthenes
primeCount = 0;
for (j = 0; j < size; j++)
{
if (flags[j] == 1)
{
// found a prime, count it
primeCount++;
// now set all of the multiples of
// the current prime number to false
// because they couldn't possibly be
a = 2*j + 3; // odd numbers starting
b = a + j;
while(b < size)
{
flags[b] = 0;
b = b + a;
}
}
}
j
0
1
2
3
4
5
6
7
8
prime
with 3 9
10
11
12
13
14
15
1
1
1
0
1
1
0
1
1
0
1
1
0
1
1
1
a
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
b
3
6
9
12
15
18
21
24
27
30
33
36
39
42
45
48
Find prime numbers using
Sieve of Eratosthenes
primeCount = 0;
for (j = 0; j < size; j++)
{
if (flags[j] == 1)
{
// found a prime, count it
primeCount++;
// now set all of the multiples of
// the current prime number to false
// because they couldn't possibly be
a = 2*j + 3; // odd numbers starting
b = a + j;
while(b < size)
{
flags[b] = 0;
b = b + a;
}
}
}
j
0
1
2
3
4
5
6
7
8
prime
with 3 9
10
11
12
13
14
15
1
1
1
0
1
1
0
1
1
0
1
1
0
1
1
0
a
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
b
3
6
9
12
15
18
21
24
27
30
33
36
39
42
45
48
Find prime numbers using
Sieve of Eratosthenes
primeCount = 0;
for (j = 0; j < size; j++)
{
if (flags[j] == 1)
{
// found a prime, count it
primeCount++;
// now set all of the multiples of
// the current prime number to false
// because they couldn't possibly be
a = 2*j + 3; // odd numbers starting
b = a + j;
while(b < size)
{
flags[b] = 0;
b = b + a;
}
}
}
j
0
1
2
3
4
5
6
7
8
prime
with 3 9
10
11
12
13
14
15
1
1
1
0
1
1
0
1
1
0
1
0
0
1
1
0
a
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
b
3
6
9
12
15
18
21
24
27
30
33
36
39
42
45
48
Related documents