1

So I wanted to create a graph that plots a Collatz Conjecture for a given number (n), but for some reason my turtle will only travel to the desired position and then not move any further, even though i have clearly specified what rotation and distance it should travel in. here is the code, the bit where i'm struggling is under the second function "graphCollatz(t)":

import time
import turtle
import math

#simple collatz conjecture
def simpleCollatz(t):
    n = int(input("input value for n"))
    while True:  
        if n % 2 == 0:               #check if n is even
            n = n / 2                #divide by two if even
            print(n)
            time.sleep(t)
        else:
            n = n * 3 + 1            #multiply by 3 and add 1 if odd
            print(n)
            time.sleep(t)

#collatz conjecture as a graph using turtle
def graphCollatz(t):
    newN = int(input("input value for n"))
    lastN = newN                    #store a value for our last number n to help us calculate trigonometry later
    plotter = turtle.Turtle()
    plotter.screen.bgcolor("black")
    plotter.pencolor("white")
    plotter.right(90)
    plotter.forward(300)
    plotter.left(90)
    plotter.backward(250)
    if newN % 2 == 0:               #same as last code
        newN = newN / 2
    else:
        newN = newN * 3 + 1
    while True:
        if newN % 2 == 0:
            lastN = newN
            newN = newN / 2                      #the triangle has one point at lastN, one point at newN, and the right-angle point where they intersect
            trigOpposite = newN - lastN          #find the value for the opposite (angle theta is the angle we want our turtle to change in)
            trigAdjacent = 1                     #the x axis of the graph is incremental so adjacent will always be 1
            trigHypotenuse = math.sqrt(trigOpposite ** 2 + trigAdjacent ** 2)        #find the hypotenuse by doing pythagoras (this is the distance our turtle will travel)
            theta = math.asin(trigOpposite / trigHypotenuse)                    #find theta by doing sin^-1(opposite / hypotenuse)
            turtle.left(theta)                                                  #rotate turtle by theta
            turtle.forward(trigHypotenuse)                                      #move turtle forward by the Hypotenuse distance
        else:
            lastN = newN
            newN = newN * 3 + 1
            trigOpposite = newN - lastN
            trigAdjacent = 1
            trigHypotenuse = math.sqrt(trigOpposite ** 2 + trigAdjacent ** 2)
            theta = math.asin(trigOpposite / trigHypotenuse)
            turtle.left(theta)
            turtle.forward(trigHypotenuse)
            


print("code for calculating the trajectory of any given number (n) following the rules of the collatz conjecture (if odd, 3n+1, if even, n/2)")
time.sleep(1)
choice = input("which feature to run? 1 = simple collatz conjexture, 2 = graph collatz conjecture")
if choice == "1":
    t = float(input("how long should the delay be between numbers?"))
    simpleCollatz(t)
elif choice == "2":
    t = float(input("how long should the delay be between steps?"))
    graphCollatz(t)



Moss
  • 11
  • 2
  • Some loose remarks: `newN = newN / 2` results in a float; you could use `newN = newN // 2` to keep it integer. To calculate a full 360º angle, `theta = math.atan2(trigOpposite, trigHypotenuse)` might be useful. Maybe you need to convert from radians to degrees? `... * 180 / pi` ? – JohanC Feb 23 '22 at 14:25
  • Thanks a lot, I didn't realize it was measured in radians. – Moss Feb 23 '22 at 15:08

0 Answers0