Report abuse

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm, inch, mm
c = canvas.Canvas("ulam.pdf", pagesize=(23*inch, 23*inch))
total = int(23*inch/mm * 23 * inch/mm)

# Sieve of Eratosthenes
nums = [ i for i in range(2*total) ]
primes = [ True for i in range(2*total) ]
primes[0] = False
primes[1] = False
for p in range(len(primes)):
    if primes[p]:
        i = 2
        while i*p < len(primes):
            primes[i*p] = False
            i += 1

import random
def drawDot(x, y):
    r = random.random()
    g = random.random()
    b = 1 - (r**2 + g**2)**.5
    c.setFillColorRGB(random.random()/2+.25, random.random()/2+.25, random.random()/2+.25)
    c.circle(x*mm + 23*inch/2, y*mm + 23*inch/2, .5*mm, stroke=0, fill=1)


sidelength = 1
x = 0
y = 0
n = 1
while n < total:
    for _ in range(sidelength):
        x += 1
        n += 1
        if primes[n]:
            drawDot(x,y)
    for _ in range(sidelength):
        y += 1
        n += 1
        if primes[n]:
            drawDot(x,y)
    sidelength += 1
    for _ in range(sidelength):
        x -= 1
        n += 1
        if primes[n]:
            drawDot(x,y)
    for _ in range(sidelength):
        y -= 1
        n += 1
        if primes[n]:
            drawDot(x,y)
    sidelength += 1

c.showPage()
c.save()