Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Generate "Ulam spiral (prime spiral)"
# -*- encoding: utf-8 -*-
from PIL import Image
from math import sqrt
Max = 333
pix = 3
filename = 'output.png'
f1 = lambda x,y: sorted(range(x-(y-1)*2+1-y, x-(y-1)*2+1),reverse=True)
isprime = lambda x: x != 1 and (x == 2 or not 0 in [x%y for y in range(2,int(sqrt(x))+1)])
ulam = []
for i in range(Max/2+1):
for i in range(1,Max/2+1):
for i in range(1,len(ulam)-1):
m = i if i < Max/2+1 else Max - i - 1
for j in range(m):
ulam[i] += reversed([ulam[i-1][Max-1-x]-1 for x in range(m)])
for i in range(len(ulam)):
ulam[i] = map(isprime,ulam[i])
img ='RGB',(Max*pix,Max*pix),(255,255,255))
for y in range(len(ulam)):
for x in range(len(ulam[i])):
if ulam[y][x]: color = (0,0,0)
else: color = (255,255,255)
for a in range(pix):
for b in range(pix):
img.putpixel((x*pix+b,y*pix+a),color), 'PNG')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment