Top Banner
Lists Samuel Marateck © 2010
30

Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Lists

Samuel Marateck

© 2010

Page 2: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

• The Sieve of Eratosthenes

Page 3: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Place a 0 in all slots

• X = 26*[0]

Page 4: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Place 0’s in all slots.

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 5: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Start with 2

for j in range(2, √25):

Page 6: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Start with 2

for j in range(2, √25):

if x[j] == 0:

Page 7: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Start with 2

for j in range(2, √25):

if x[j] == 0:

index = 2*j

Page 8: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Start with the 2 slot.

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 9: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

for j in range(2, √25):

if x[j] == 0:

index = 2*j

while index < 25:

x[index] = 1

Page 10: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Place a 1 in the 4 slot.

0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 11: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

for j in range(2, √25):

if x[j] == 0:

index = 2*j

while index < 25:

x[index] = 1

index = index + j

Page 12: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Then in the 6 slot.

0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 13: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Continue.

0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 14: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Continue.

0 0 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 15: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Continue.

0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 16: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Place 1’s in all multiples of 2 slots.

0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 17: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Since there is a 0 in the 3 slot,3 is prime

0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 18: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

There is a 1 already in the 6 slot.

0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 19: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Place a 1 in the 9 slot.

0 0 0 1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 20: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

The 12 slot already has a 1.

0 0 0 1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 21: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Then a 1 in the 15 slot.

0 0 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 22: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Then a 1 in the 21 slot.

0 0 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 23: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Now there is a 1 in all multiple of 3 slots.

0 0 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 24: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Skip the 4 slot since it has a 1.

0 0 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 25: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

There is a 0 in the 5 slot,so 5 is prime.

0 0 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 26: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Place a 1 in multiple of 5 slots.

0 0 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 27: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

The only non-zero slot is 25.

0 0 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 1

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 28: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Since √25 is 5 we stop here.

0 0 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 1

1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Page 29: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.

Starting with 2, all the slots with 0’s are prime numbers.

0 0 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 1

2 3 5 7 11

13

17

19

23

Page 30: Lists Samuel Marateck © 2010. The Sieve of Eratosthenes.