# Primes

Being fascinated with how prime spirals look, I tried another layout for primes. Starting at the upper left and writing out numbers like a normal text, starting at 0 and coloring every prime number red, every other number white, this is the result.

It is interesting, that – like in prime spirals – the red squares form visible patterns. Randomly assigned squares would not as often form such patterns.

``````# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 27th of June, 2015
#         edited 28th of June, 2015``````

``````# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 27th of June, 2015
#         edited 28th of June, 2015

# importing needed modules
import pygame, sys, time, math, os, random

""" CLASSES """
# dummy class for global variables
class dummy():
pass

""" FUNCTIONS """
# checks if given number should be lit on
def lit(_num):
# prime check
if _num <= 1:
return False
for _ in range(2, _num):
if _num % _ == 0:
return False
return True

# gets the mouse position
def getMousePos():
p = pygame.mouse.get_pos()
return [p[0], p[1]]

# validates color integer
# extra feature: _min and _max implementation
def colorValid(_color, _min = 0, _max = 255):
newColor = math.fabs(_color)
n = _max - _min
if newColor > n:
if int(newColor / n) % 2 == 0:
newColor = newColor % n
else:
newColor = n - (newColor % n)

return int(newColor) + _min

# gets the position on a circle
# circle center                           : '_pos'
# angle from center to point on the circle: '_angle'
return [
]

# returns an integer version of given positon
def intpos(_pos):
return [int(_pos[0]), int(_pos[1])]

# basic vector functions
def vecConvert(p1, p2):
return [p2[0] - p1[0], p2[1] - p1[1]]
def vecLen(vec):
return math.sqrt( (vec[0]**2) + (vec[1]**2) )
def vecMultiply(vec, n):
return [vec[0] * n, vec[1] * n]
def vecGetPoint(vec, point):
return [point[0] + vec[0], point[1] + vec[1]]
return [vec1[0] + vec2[0], vec1[1] + vec2[1]]

# claculates distance between given positions
def posDistance(p1, p2):
return math.sqrt( (p2[0] - p1[0])**2 + (p2[1] - p1[1])**2 )

# quits the program
def quit():
sys.exit()

""" TICK; RENDER """
# tick function
def tick():
# handle events
for event in pygame.event.get():
# quit
if event.type == pygame.QUIT:
quit()

# keyup
if event.type == pygame.KEYUP:
# handle 'main.KEYSDOWN'
if event.key in main.KEYSDOWN:
main.KEYSDOWN.remove(event.key)

# keydown
if event.type == pygame.KEYDOWN:
# handle 'main.KEYSDOWN'
if event.key not in main.KEYSDOWN:
main.KEYSDOWN.append(event.key)

# render function
def render():
# fill
#main.SURF.fill(main.COLOR)

if True:
if main.VIS < main.WIDTH * main.HEIGHT:
if lit(main.VIS):
c = [255, 0, 0]
else:
c = [255, 255, 255]
main.SURF.set_at([main.VIS % main.WIDTH, main.VIS / main.WIDTH], c)
main.VIS += 1

# blit and flip
main.SCREEN.blit(pygame.transform.scale(main.SURF, main.SCALEDSIZE), [0, 0])
pygame.display.flip()

""" INIT """
# initialize program
def init():
main.WIDTH, main.HEIGHT = 1080/5, 720/5
main.SIZE = [main.WIDTH, main.HEIGHT]

main.SCALEDSIZE = [1080, 720]
main.SCREEN = pygame.display.set_mode(main.SCALEDSIZE)
main.SURF = pygame.Surface(main.SIZE)

main.CAPTION = "Primes"
main.COLOR = [0, 0, 0]
main.TICKS = 0
main.KEYSDOWN = []

main.VIS = 0

# functions
pygame.display.set_caption(main.CAPTION)

""" RUN """
# run function (uses tick() and render())
def run():
ticksPerSecond = 60
lastTime = time.time() * 1000000000
nsPerTick =  1000000000.0 / float(ticksPerSecond)

ticks = 0
frames = 0

lastTimer = time.time() * 1000
delta = 0.0

while True:
now = time.time() * 1000000000
delta += float(now - lastTime) / float(nsPerTick)
lastTime = now
shouldRender = False

while delta >= 1:
ticks += 1
main.TICKS += 1
tick()
delta -= 1
shouldRender = True

if shouldRender:
frames += 1
render()

if time.time() * 1000 - lastTimer >= 1000:
lastTimer += 1000

# debug
# print("Frames: " + str(frames) + ", ticks: " + str(ticks))

frames = 0
ticks = 0

# main variable
main = dummy()
init()

# start program
run()``````