Recaman Sequence
Visualization
Mathematical Art
Number Patterns
Sequence Analysis

Visualization of the Recaman Sequence

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The Recamán sequence is famous not only because of its recursive definition, but because its visualization produces surprisingly intricate patterns from a very simple rule. The classic visualization draws arcs between consecutive terms, turning number-sequence movement into a form of mathematical art.

Define the Sequence First

The sequence starts at 0. For each step n, you try to subtract n. If that would produce a negative number or a value already used earlier, you add n instead.

That gives the beginning of the sequence:

0, 1, 3, 6, 2, 7, 13, 20, 12, 21, ...

The interesting visual behavior comes from the constant switching between backward and forward jumps.

Generate the Sequence in Python

python
1def recaman(n_terms):
2    seen = {0}
3    values = [0]
4
5    for n in range(1, n_terms):
6        candidate = values[-1] - n
7        if candidate > 0 and candidate not in seen:
8            values.append(candidate)
9        else:
10            values.append(values[-1] + n)
11        seen.add(values[-1])
12
13    return values

This implementation keeps a seen set so the rule about repeated values is efficient.

The Classic Arc Diagram

A popular visualization places the integers on a number line and draws a semicircle between each consecutive pair. One common stylistic rule is:

  • draw arcs above the line for even steps,
  • draw arcs below the line for odd steps.

That alternating orientation makes the structure easier to read visually.

python
1import matplotlib.pyplot as plt
2from matplotlib.patches import Arc
3
4values = recaman(80)
5fig, ax = plt.subplots(figsize=(14, 6))
6
7for i in range(1, len(values)):
8    a, b = values[i - 1], values[i]
9    center = (a + b) / 2
10    width = abs(b - a)
11    height = width
12    theta1, theta2 = (0, 180) if i % 2 == 0 else (180, 360)
13
14    arc = Arc((center, 0), width=width, height=height,
15              theta1=theta1, theta2=theta2)
16    ax.add_patch(arc)
17
18ax.set_xlim(min(values) - 5, max(values) + 5)
19ax.set_ylim(-max(values) / 2, max(values) / 2)
20ax.axhline(0, color="black", linewidth=0.8)
21plt.show()

The result is the classic Recamán arc picture.

Why the Visualization Is Interesting

What looks random at first glance still contains structure:

  • early jumps are small and local,
  • later arcs spread wider across the number line,
  • repeated fallback to addition creates long forward leaps,
  • the picture becomes dense around some regions and sparse around others.

That balance between simple definition and complex visual output is why the sequence is so memorable.

Variations Worth Trying

You can experiment with:

  • coloring arcs by step number,
  • scaling line thickness by jump length,
  • animating the sequence construction,
  • plotting points instead of arcs to study recurrence behavior.

These variations make the same mathematical object useful for both teaching and visual exploration.

Scaling the Plot Changes the Feel

The same sequence can look very different depending on figure size, term count, arc orientation, and axis scaling. Small visual choices can make the image feel like a clear mathematical diagram or a dense abstract drawing. That is part of why Recamán visualizations are popular in both teaching and generative art contexts.

Common Pitfalls

  • Forgetting to track previously seen values and generating the wrong sequence.
  • Assuming the visualization itself defines the sequence instead of the recurrence rule.
  • Letting plot limits clip large arcs too early.
  • Using too many terms without adjusting figure size and making the image unreadable.
  • Mixing different arc-orientation rules and then comparing the pictures as though they were equivalent.

Summary

  • The Recamán sequence is generated by subtracting n when possible and otherwise adding n.
  • Arc diagrams are the classic way to visualize its jump structure.
  • A small amount of Python and Matplotlib is enough to create the standard picture.
  • The appeal comes from complex visual behavior emerging from a very simple rule.
  • Visualization is useful both as mathematical exploration and as algorithmic art.

Course illustration
Course illustration

All Rights Reserved.