QuataMagazine

Recamán’s Sequence Simulation in C++ ImGui

Markus Buchholz
3 min readMar 29, 2023

--

Recamán’s sequence is a sequence of numbers related to each other by a recurrence relation.
The relation is straightforward and can be formulated as follows.
There exists a subset of real numbers from 0 to n. In the case of article implementation, the set can be considered as an array/vector of length n. We run an external counter which counts from 1. In each step of sequence
generation, the counter updates the current value by one. The sequence generator keeps the current position of the index value of the array (vector).
The index is updated depending on the algorithm condition. As a rule, the algorithm in each step tries to reduce the current index by the current value of the counter.
If the reduction (index) is lower than 0 or the vector position of the reduced index was visited so the reduction can not take place. In this case, the index is increased by the value of the current counter. The value of the index is pushed to the container which holds the value of Recamán’s sequence
The algorithm pioneered by mathematician Bernardo Recamán Santosby can be expressed as follows.

where a is the number in the sequence, n is the current state of the counter

In this article, I implemented the Recamán sequence generator. The beauty of the Recamán sequence can be displayed by running simulations
(drawing arcs between the numbers in the sequence).
The radius of each arc is the distance between two consecutive numbers, divided by 2. For example, for numbers 20, 12, 21, and 11, in sequence, we construct the arc between position 20 and 12 (on the screen I keep the Y screen coordinate constant). The radius of that arc is 4.0. Next, we draw the arc for the numbers 12–21 with a radius 4.5. Then we construct the arc between position 21 and 11 with the radius 5, etc. Please note the position of the arc (above and below the Y-axis) varies for each arc in the sequence.
It means we draw the arc above and below along the line in an exchangeable way. One arc is above the line and the next is below the line, etc. Process repeats.

Here you can see the final result for the sequence consisting of 50 numbers.

The final results for the sequence of 50 numbers (by author),

And simulation for the same number length of the sequence.

Simulation (by author)

The implementation of the solution I deployed in C++ with the visualization in ImGui. The introduction to the C++ ImGui and information on how to compile and build programs you will find in one of my previous articles.

The source code you will find on my GitHub.

Thank you for reading.

--

--

Markus Buchholz
Markus Buchholz

Written by Markus Buchholz

Researcher in underwater robotics

No responses yet