Skip to content

Instantly share code, notes, and snippets.

@panzi
Created November 10, 2013 17:24
Show Gist options
  • Save panzi/7401121 to your computer and use it in GitHub Desktop.
Save panzi/7401121 to your computer and use it in GitHub Desktop.
Usage: python visprim.py <width> <height> <output-filename>
#!/usr/bin/env python
import sys
import Image
from itertools import islice
def primes():
yield False # 0
yield False # 1
yield True # 2
ps = [2]
half = 1
n = 3
while 1:
isprime = True
for p in ps:
if p > half:
break
if n % p == 0:
isprime = False
break
if isprime:
yield True
ps.append(n)
else:
yield False
yield False
half += 1
n += 2
def makePrimeImage(width,height):
ps = primes()
img = Image.new('RGB',(width,height))
n = width * height
data = [0x0000ff if p else 0xffffff for p in islice(primes(),n)]
# append = data.append
# write = sys.stdout.write
# flush = sys.stdout.flush
# delta = 100. / n
# percent = 0
# write('%6.2f%%' % percent)
# flush()
# for p in islice(primes(),n):
# append(0x0000ff if p else 0xffffff)
# percent += delta
# write('\r%6.2f%%' % percent)
# flush()
# print '\r%6.2f%%' % 100
img.putdata(data)
return img
def main():
width = int(sys.argv[1],0)
heigth = int(sys.argv[2],0)
out = sys.argv[3]
img = makePrimeImage(width,heigth)
img.save(out)
if __name__ == '__main__':
try:
import psyco
except ImoportError:
print "it might run faster with psyco"
else:
psyco.full()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment