# Recamán Sequence

The Recamán Sequence might look like a jumble of numbers at first glance.

But then it becomes clear.

## THE BIG PICTURE

The 0th term is 0.

To find the $$n$$th term, you usually subtract, $$a(n)-n$$, however if that results in a negative number or a number already used in the sequence, you add, $$a(n)+n$$.

So the first term will be 0$$\pm$$1, but we cannot choose subtraction because 0 – 1 = -1. So we add, which means the 1st term is 1.

The second term will be 1$$\pm$$2, but we cannot choose subtraction because 1 – 2 = -1. So we add, which means the 2nd term is 3.

The third term will be 3$$\pm$$3, but we cannot choose subtraction because 3 – 3 = 0, and it is already in the sequence. So we add, which means the 3rd term is 6.

The fourth term will be 6$$\pm$$4, but we can choose subtraction because 6 – 4 = 2, which is not in the sequence. So we subtract, which means the 4th term is 2.

The fifth term will be 2$$\pm$$5, but we cannot choose subtraction because 2 – 5 = -3. So we add, which means the 5th term is 7.

And so on.

## VISUAL REPRESENTATION

Like every single sequence, the Recamán sequence can be represented visually. Here is the graph of the sequence:

Looks like a garden hose, doesn’t it?

## PYTHON CODE

Python is one of the most powerful languages, and I have written Python code to generate the Recamán sequence. This code prints out the first 10 Recamán numbers.

def recaman(n):
if not isinstance(n, int):
raise TypeError("%r is not an integer" %n)
elif n < 0:
raise ValueError("%d is negative" %n)
elif n == 0:
return 0
else:
lst = [recaman(k) for k in range(n)]
if (recaman(n-1)-n < 0) or (recaman(n-1)-n in lst):
return recaman(n-1)+n
else:
return recaman(n-1)-n

for i in range(1, 11):
print(recaman(i))

And here’s the output:

## MUSICAL REPRESENTATION

The musical representation of a sequence is as follows: you take a number of the sequence and take the “modulo 88” (the remainder when the number is divided by 88). You then get a number from 0 to 87. Add 1 to that number and you get 1 to 88. Now play the keys the numbers correspond to. 1 corresponds to C1, the lowest note on the piano1, 2 corresponds to D1, 3 corresponds to E1, and so on, where 88 corresponds to C8, the highest note. The Recamán Sequence sounds like horror movie music.

Sounds a bit spooky, right? Yes, it does.

I got the graph and musical adaptation from the Online Encyclopedia of Integer Sequences. Specifically, the entry for the Recamán Sequence is A005132. (after the entry ends, there are the links “graph”, which brings up the graph of the sequence, and “listen”, which brings up the musical adaptation. Note that the “listen” page uses MIDI for the music, but I had to convert it to MP3 for compatibility.)

But anyways this is the end of the blog. If you liked this blog, make sure to check out my other blogs and thank you.

#### Footnotes and References

1. or keyboard, or synth

## 2 Replies to “Recamán Sequence”

1. Siddhivinayak+Naik says:

Frankly I learnt it for the first time… thanks 👍👍

This site uses Akismet to reduce spam. Learn how your comment data is processed.