-
-
Save dandevac/f5225f8bdcc4c3671f5f88ad03a4eeef to your computer and use it in GitHub Desktop.
a LOGO-style collatz path visualization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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