-
Notifications
You must be signed in to change notification settings - Fork 0
/
ulams_spiral.py
59 lines (43 loc) · 1.31 KB
/
ulams_spiral.py
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
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sympy import isprime as is_prime
class Spiral_Display():
def __init__(self, cols = 9):
self.cols = cols
self.matrix = np.zeros((cols,cols))
self.ptr = [round(cols/2), round(cols/2)]
self.entrie = 1
def append_point(self, ptr, mov, num, direction, relation):
for i in range(mov):
if(relation(self.entrie)):
try:
self.matrix[ptr[0], ptr[1]] = 1
except IndexError:
continue
self.entrie += 1
ptr[direction] += num
def set_point(self, mov, relation = is_prime):
if(mov % 2 == 0):
self.append_point(self.ptr, mov, -1, 1, relation)
self.append_point(self.ptr, mov, +1, 0, relation)
elif(mov % 2 != 0):
self.append_point(self.ptr, mov, +1, 1, relation)
self.append_point(self.ptr, mov, -1, 0, relation)
def fixed_display(self):
print(self.matrix[1:,1:])
def normal_display(self):
print('\n', self.matrix)
def main():
M = Spiral_Display(300)
for i in range(M.cols)[1:]:
#M.set_point(i, lambda x: x % 5 == 0)
M.set_point(i)
M.fixed_display()
M.normal_display()
sns.heatmap(M.matrix[1:, 1:], cmap='magma',yticklabels= False,
xticklabels = False, cbar = False)
#plt.show()
plt.savefig(f'figures/{M.cols}Spiral.png', dpi= 128)
if __name__ == '__main__':
main()