I recently learned about a really interesting sequence of integers, called the Recamán sequence (it’s sequence A005132 in the Online Encyclopedia of Integer Sequences). It is very simple to define, but the resulting complexity shows how powerful self-reference is (for both good and evil). Here’s the definition. The first term of the sequence is , and each term differs from by . Now, if were always just more than , we would have the triangular numbers:
Note how is one more than ; is more than ; is more than ; and so on.
But that’s not quite how the Recamán sequence is defined. In the Recamán sequence, “wants” to be less than : it will be so if is nonnegative and has not already appeared in the sequence. Otherwise, it “settles” for being more than (as with the triangular numbers).
So . Then:
- has to differ from by —that is, it must be or . But it can’t be negative, so it is.
- How about ? It must be away from , so either or —but again, it can’t be negative, so it is .
- Now, must be away from , so it must be or . is nonnegative, but it has already appeared in the sequence as , so we choose .
- So far, this is looking a lot like the triangular numbers! But let’s see what happens with . It must be , that is, either or . And here something different happens: is positive and has not appeared yet in the sequence, so .
Continuing this pattern, we get
From the definition, you might initially think that no number will ever be repeated: we explicitly avoid picking numbers that have already occurred in the sequence, right? Well, we don’t pick if it has already occurred, but in that case we definitely pick whether it has already occurred or not—and in fact sometimes it has. You don’t have to continue the sequence very much farther before you find, for example, .
Here’s a scatterplot of the first terms, with the -axis scaled by 1/2 to make it easier to see:
From the graph we can see that the numbers tend to form long parallel alternating runs where the top numbers are increasing by and the bottom decreasing. For example, we can see the first of these starting at :
This makes sense: we are alternately adding , then subtracting , then adding , then subtracting , and so on. The run will be broken when we hit a number that has already occurred: in this case, the number after is not , because that has already occurred, so instead it jumps up again to . After , it jumps up one more time (since was already in the sequence) and we get a very short parallel sequence before it falls back down and starts another alternating sequence .
So far, this looks regular-ish—one might hope to discover some regular patterns that could, for example, lead to a closed formula. But our hopes are dashed when we look further out in the sequence:
It doesn’t look very regular-ish anymore! And this is what I find fascinating about the Recamán sequence: the self-reference in its definition (we choose only when it has not already appeared) throws a giant monkey wrench of chaos into the works, so that it is very difficult to find any patterns or prove anything definite about it, even though it is still highly structured. To see what I mean, here’s a graph of the first 5000 terms:
There is most definitely structure—for example, all the terms seem to fall along these curving “bands” radiating out from the origin. I imagine one might even be able to say something approximate about the shape of those curves. But there is still obviously lots of chaos—it’s hard to discern any regular patterns.
So we know that the Recamán sequence is not 1-1: some numbers can appear multiple times. But is it onto? That is, does every number appear at least once, somewhere in the sequence? It is conjectured that this is true, but no one knows—it is an open question! According to the OEIS entry, after looking at the first terms, the smallest number that is still missing is ; every number smaller than that has appeared somewhere in the first terms of the sequence. Crazily though, is still missing after looking at the first terms!!! (That’s a staggeringly large number of terms; it is far more than the estimated number of atoms in the universe.) So perhaps the Recamán sequence is not onto after all—is there something special about so that it never apears? Or does it eventually appear very very far into the sequence? No one knows, and to be honest, I think the latter actually seems more likely. But it just underscores how difficult it would be to prove this.