Skip to content

Commit 7839bf3

Browse files
committed
illustration of sieve of Eratosthenes
1 parent 67dd87d commit 7839bf3

File tree

7 files changed

+60
-0
lines changed

7 files changed

+60
-0
lines changed

images/eratosthenes_1.pdf

12.3 KB
Binary file not shown.

images/eratosthenes_2.pdf

20.1 KB
Binary file not shown.

images/eratosthenes_3.pdf

20.5 KB
Binary file not shown.

images/eratosthenes_4.pdf

20.8 KB
Binary file not shown.

images/eratosthenes_5.pdf

20.9 KB
Binary file not shown.

images/eratosthenes_6.pdf

21.6 KB
Binary file not shown.

images/src/eratosthenes.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import os
2+
import sys
3+
4+
import numpy as np
5+
from pyx import canvas, color, path, text, unit
6+
7+
def draw_grid():
8+
c.stroke(path.rect(0, 0, 25, 2))
9+
for n in range(24):
10+
c.stroke(path.line(n+1, 0, n+1, 2))
11+
c.stroke(path.line(0, 1, 25, 1))
12+
13+
text.set(text.LatexRunner)
14+
text.preamble(r'\usepackage{arev}\usepackage[T1]{fontenc}')
15+
unit.set(xscale=1.2, wscale=2.5)
16+
17+
c = canvas.canvas()
18+
c.fill(path.rect(0, 1, 2, 1), [color.grey(0.7)])
19+
c.text(0.5, 1.5, '0', [text.halign.center, text.valign.middle])
20+
c.text(1.5, 1.5, '1', [text.halign.center, text.valign.middle])
21+
basename = os.path.splitext(sys.argv[0])[0]
22+
baseprimes = [0, 2, 3, 5, 7]
23+
ncolor = len(baseprimes)-1
24+
cancelled = set([0, 1])
25+
for nr, baseprime in enumerate(baseprimes):
26+
if nr == 0:
27+
for n in range(2, 50):
28+
x = n % 25
29+
y = 2-(n//25)
30+
c.text(x+0.5, y-0.5, str(n), [text.halign.center, text.valign.middle])
31+
else:
32+
cancelled.add(baseprime)
33+
hvalue = 1.1*(nr-1)/(ncolor-1)
34+
hvalue = hvalue-int(hvalue)
35+
primecolor = color.hsb(hvalue, 1, 0.8)
36+
x = baseprime % 25
37+
y = 2-(baseprime//25)
38+
c.fill(path.rect(x, y, 1, -1), [primecolor])
39+
c.text(x+0.5, y-0.5, r'\textbf{%s}' % baseprime,
40+
[text.halign.center, text.valign.middle, color.grey(1)])
41+
for n in range(baseprime**2, 50, baseprime):
42+
if not n in cancelled:
43+
cancelled.add(n)
44+
x = n % 25
45+
y = 2-(n//25)
46+
c.stroke(path.line(x, y-1, x+1, y), [primecolor])
47+
c.stroke(path.line(x, y, x+1, y-1), [primecolor])
48+
draw_grid()
49+
c.writePDFfile('%s_%s' % (basename, nr+1))
50+
51+
for n in range(50):
52+
if not n in cancelled:
53+
x = n % 25
54+
y = 2-(n//25)
55+
c.fill(path.rect(x, y, 1, -1), [color.hsb(0.15, 1, 0.8)])
56+
c.text(x+0.5, y-0.5, r'\textbf{%s}' % n,
57+
[text.halign.center, text.valign.middle, color.grey(1)])
58+
draw_grid()
59+
c.writePDFfile('%s_%s' % (basename, nr+2))
60+

0 commit comments

Comments
 (0)