-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolored_spiral.py
More file actions
67 lines (53 loc) · 1.36 KB
/
colored_spiral.py
File metadata and controls
67 lines (53 loc) · 1.36 KB
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
57
58
59
60
61
62
63
64
65
66
67
from __future__ import division
from ulam import ulam_spiral, factors
# Create the Ulam spiral.
grid_size = 11
grid = ulam_spiral(grid_size)
# Set up the canvas size.
square_size = 50
size(grid_size*square_size, grid_size*square_size)
# Black lines.
stroke(0)
# Set the font properties.
font("Helvetica", 20)
align(align=CENTER)
# x = [[1,2,3], [4,5,6], [7,8,9]]
# n = 3
# for i in range(2*n-1):
# if i < n:
# z = 0
# else:
# z = i-n+1
# tmp = []
# for j in range(z, i-z+1):
# tmp.append(x[j][i-j])
# print "Slice %s: %s" % (i, tmp)
def count_primes(x):
count = 0
for e in x:
if len(factors(e)) == 2:
count += 1
return count
density = {}
for i in range(0, 2 * grid_size - 1):
if i < grid_size:
z = 0
else:
z = i - grid_size + 1
diag = []
for j in range(z, i - z + 1):
diag.append(grid[j][i-j])
density[i] = count_primes(diag) / len(diag)
for i in range(0, 2 * grid_size - 1):
if i < grid_size:
z = 0
else:
z = i - grid_size + 1
for j in range(z, i - z + 1):
x = (i-j)*square_size
y = j*square_size
c = color(density[i]+0.1)
rect(x, y, square_size, square_size, fill=c)
text(str(grid[j][i-j]), x, y+square_size/2+8, width=square_size)
print "done."
canvas.save('colored_spiral.png')