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… Recamán’s sequence has the code A005132 in the OEIS (The On-Line Encyclopedia of Integer Sequences) database. Niel Solane put forward the as-yet-unproven conjecture 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 trajectory,
- start drawing a trajectory
- …
The Recaman sequence is often visualized using arcs on the axis of non-negative integers by 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 value of the next term in the sequence, i.e., at the point a(n+1). Next, an arc is drawn starting at the point a(n+2) and ending at the point a(n+4). This process is repeated until the figure is large enough. The arc is drawn from the bottom if the index of the starting point is even, and from the top if it is odd. In other words, every even step draws the arc from the bottom, and every odd step from the top. This procedure is explained in more detail in this video.
I find the image above extremely 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 can be seen 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 intrigued me so much that I decided to let the program run for 3 days. After this time, the program 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).