-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathulam2.py
executable file
·66 lines (55 loc) · 1.89 KB
/
ulam2.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
60
61
62
63
64
65
66
#!/usr/bin/env python
# coding: utf-8
# draw an ulam spiral
# Author : Martin Piffault
# Spiral path algorithm from :
# http://stackoverflow.com/questions/398299/looping-in-a-spiral
# First parameter gives output file name, default: ulam.png
# Second parameter gives external side of the spiral in pixels, default: 100
import sys
from PIL import Image
import math
WHITE = (0xff, 0xff, 0xff, 0xff)
BLACK = (0x00, 0x00, 0x00, 0xff)
# Test primarity of number
def prime(number):
if number <= 3:
return number >= 2
if number % 2 == 0 or number % 3 == 0:
return False
for i in range(5, int(number ** 0.5) + 1, 6):
if number % i == 0 or number % (i + 2) == 0:
return False
return True
def spiral(X, Y, img):
nb_num = X*Y
nb_tenth = nb_num // 10
x = y = 0
dx = 0
dy = -1
number = 0
ok = 0
for i in range(max(X, Y)**2):
if (-X//2 < x <= X//2) and (-Y//2 < y <= Y//2):
number += 1
cx = (X//2)-x
cy = (Y//2)-y
# if number % nb_tenth == 0:
# print (n/nb_tenth)*10, '%'
# TODO : fill image with white pixels for even numbers,
# so we can iterate only over odds
if not prime(number):
img.putpixel((cy,cx), tuple(WHITE))
if x == y or (x < 0 and x == -y) or (x > 0 and x == 1-y):
dx, dy = -dy, dx
x, y = x+dx, y+dy
if __name__ == '__main__':
filename = sys.argv[1] if len(sys.argv) > 1 else 'ulam.png'
width = int(sys.argv[2]) if len(sys.argv) > 2 else 100
height = int(sys.argv[3]) if len(sys.argv) > 3 else width
dimension = (width,height)
filename = '.'.join([filename,'png']) if '.' not in filename else filename
# We start with a black image
img = Image.new('RGBA',dimension,color=(BLACK))
spiral(width, height, img)
img.save(filename)