Skip to content

Instantly share code, notes, and snippets.

@dandevac
Last active February 16, 2018 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dandevac/f5225f8bdcc4c3671f5f88ad03a4eeef to your computer and use it in GitHub Desktop.
Save dandevac/f5225f8bdcc4c3671f5f88ad03a4eeef to your computer and use it in GitHub Desktop.
a LOGO-style collatz path visualization
#!/bin/env python3
import turtle
from sys import argv
from random import randint
def collatz_path(n):
path = [n]
while n != 1:
if n % 2 == 0:
n = n // 2
else:
n = 3 * n + 1
path.append(n)
return path
def main():
turtle.color('yellow', 'blue')
turtle.begin_fill()
path = None
if len(argv) == 1:
path = collatz_path(100) # if no command line argument is provided, use 100 as default n
else:
path = collatz_path(int(argv[1]))
for step in path:
turtle.forward(step % 120) # this is for scaling
if randint(1, 10) % 2 == 0:
turtle.left(step % 360)
else:
turtle.right(step % 360)
turtle.end_fill()
turtle.done()
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment