Code bij Recamán

Je kunt de code ook als Python Notebook downloaden.


import turtle
turtle.TurtleScreen._RUNNING=True

A = [0]
maxA = 0
Afmeting = 65
for i in range(1,Afmeting):
    a = A[i-1] - i
    if (a < 0) | (a in A):
        a = A[i-1] + i
    if a > maxA:
        maxA = a
    A.append(a)

turtle.title("Recaman")
turtle.color('black', 'cornsilk')
turtle.speed(0)
turtle.right(180)
turtle.penup()
turtle.forward(460)
turtle.left(-90)
turtle.pendown()
turtle.begin_fill()
la = len(A)
rold = 1
for i in range(la-1):
    r = (A[i+1]-A[i])/2
    if r * rold < 0:
        turtle.right(180)
    rold = r
    Hoek = 180 * r // abs(r)
    r = abs(r)
    if i % 2 == 1:
        turtle.circle(8*r,Hoek)
    else:
        turtle.circle(-8*r,Hoek)
turtle.end_fill()
turtle.done()
print("Turtle done")