Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- def is_prime(n):
- if n <= 2:
- return True
- else:
- x = n - 1
- while x > 1:
- if n % x == 0:
- return False
- else:
- x -= 1
- return True
- def bin_prime(n, length):
- return format(n, 'b').zfill(length)
- def turtlefy(limit, step_size):
- #turtle setup
- turtle.setup(1500, 1000, None, None)
- turtle.bgcolor('black')
- turtle.speed(0)
- turtle.pen(fillcolor="black", pencolor="white", pensize=1)
- turtle_turn = 1 #1 is turn right, -1 is turn left
- #get maximum number of bits based on chosen limit
- length = len(format(limit, 'b'))
- #start iteration from 0 to the chosen limit
- for i in range(limit):
- if is_prime(i):
- binrep = bin_prime(i, length) #binary representation of the current prime
- title = 'Prime: {} - Binary: {}'.format(i, binrep)
- turtle.title(title)
- #send orders to the turtle based on the binary representation of the prime
- for x, order in enumerate(binrep):
- if order != '0':
- if turtle_turn == 1:
- turtle.right(90)
- elif turtle_turn == -1:
- turtle.left(90)
- turtle.forward(step_size)
- turtle_turn *= -1
- turtlefy(999999, step_size=0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement