In this project, I visualized Recamán’s sequence using turtle graphics.
What is Recamán’s sequence?
Recamán’s sequence is a sequence of non-negative integers. Its nth term, a(n), can be defined using the following rule:
a(0) = 0; for n > 0, a(n) = a(n-1) – n if the result is a positive number and is not already an element of the sequence, otherwise a(n) = a(n-1) + n
The first terms of this sequence are therefore the numbers 0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9, 24, 8, 25, 43, 62, 42, 63, 41, 18… The Recamán sequence has the code A005132 in the OEIS (The On-Line Encyclopedia of Integer Sequences) database. Niel Solane proposed a conjecture—which remains unproven to this day—that this infinite sequence contains all non-negative integers.
What is turtle graphics?
Turtle graphics is a form of
vector graphics drawn by a computer program. This program uses a virtual cursor and changes its position on a 2D plane according to predefined rules. The cursor leaves a trail behind it, allowing the program to draw. Such a program has simple rules, such as:
- move the cursor forward by x pixels,
- turn left/right by x°,
- turn left/right by x°,
- stop drawing the path,
- start drawing a trajectory
- …
The Recaman sequence is often visualized using arcs on the axis of non-negative integers, starting at the first number of the sequence, a(n). Then, an arc is drawn starting at this point and ending at the point corresponding to the next term in the sequence, that is, at the point a(n+1). Next, an arc is drawn starting at a(n+2) and ending at a(n+4). This process is repeated until the figure is large enough. An arc is drawn from the bottom if n is even and from the top if n is odd. In other words, an arc is drawn from the bottom every even step and from the top every odd step. This process is explained in more detail in this video.
I find the image above incredibly fascinating, so I decided to experiment further with this sequence. I therefore created a Python program that draws turtle graphics using the following rules:
- n = 0
- turn left by a(n)°
- move forward
- n = n + 1
- return to step 2
As you can see below on the right, these simple rules have created an unexpectedly complex pattern that is essentially infinite, and what the interactive demo below displays is only a tiny fraction of it.
This structure fascinated me so much that I decided to let the program run for 3 days. After that time, the program had generated over 3,000,000 terms of the Recaman sequence, and the structure was so large that I decided to create a video about it (see below).
A simple sample Python source code for this project is available below.
import turtle
t = turtle.Turtle()
ts = turtle.Screen()
while True:
t.speed(0)
t.up()
t.left(270)
t.setx(0)
t.sety(0)
t.down()
recaman = [0]
scale = 8 # set size
n = 3500 # set how many iterations
for i in range(1, n):
if recaman[-1] - i > 0 and recaman[-1] - i not in recaman:
recaman.append(recaman[-1] - i)
t.left(recaman[-1])
t.forward(scale)
else:
recaman.append(recaman[-1] + i)
t.left(recaman[-1])
t.forward(scale)
turtle.Screen().reset()
turtle.done()