Skip to the plate
FORMA PUBLIC DOMAIN GENERATIVE ATLAS / ED. 0.28
Plate 74, Recamán Sequence: a still of the sequence / greedy walk plate as the atlas renders it, in the curves accent.

PL. 74  ·  CURVES / SEQUENCE / GREEDY WALK

Recamán Sequence

Bernardo Recamán Santos, 1991 · via N. J. A. Sloane

OPEN THE LIVE PLATE ▸

DEFINITION

a₀ = s
aₙ = aₙ₋₁ − n   if positive and unvisited
aₙ = aₙ₋₁ + n   otherwise

NOTES

Step backwards if you can, forwards if you must, with strides that grow by one each time. Recamán sent the sequence to Sloane in 1991; it became OEIS A005132, and Sloane has called it a favourite. Whether every number is eventually visited is still open — the smallest one not known to appear is 852,655. The drawing is the familiar one, consecutive terms joined by semicircles alternating above and below the number line, and the reveal walks it stride by stride because the rule is the exhibit.

PROVENANCE

Origin
B. Recamán Santos, in a 1991 letter to N. J. A. Sloane; OEIS A005132
Standing
Public domain — an integer sequence and its folk rendering
Drawing
The alternating semicircles are not the OEIS’s own plot: that rendering was popularised by Numberphile’s "The Slightly Spooky Recamán Sequence", 14 January 2018, and it is the one drawn here
Constants
The start value seeds a genuinely different walk — the greedy rule never repeats a picture. A005132 is defined from a₀ = 0, where "step back if positive" and "step back if non-negative" agree; away from zero they do not, and this plate takes the strictly-positive reading

HOUDINI · VEX

The same published mathematics as a Detail Wrangle body. Paste it into a Wrangle with Run Over set to Detail; every constant is the published value plus a tweak channel, so Create Spare Parameters gives a slider that starts where the paper does.

// FORMA — PL. 74 · RECAMÁN SEQUENCE — Bernardo Recamán Santos, 1991 · via N. J. A. Sloane
//   a₀ = s
//   aₙ = aₙ₋₁ − n   if positive and unvisited
//   aₙ = aₙ₋₁ + n   otherwise
// Paste into a Detail Wrangle (Run Over: Detail), no inputs needed.
// Written from the published mathematics, not adapted from any code.
// Constants arrive at their published values. Press the node's Create
// Spare Parameters button and every tweak becomes a slider — starting
// at 0, the published figure, and moving in the constant's own units.
// https://forma-gen.com/#plate=recaman

float p_n     = 85 + chf('n_tweak');          // strides · live 40 .. 140
float p_start = 0 + chf('start_tweak');       // s — start at · live 0 .. 40

// The plate's own colour: FORMA's CURVES accent as a cosine ramp,
// brightest near t = 0 and t = 1, near-black around t = 0.5.
vector forma_ramp(float t){
  return set(
    0.11 + 0.1196 * cos(6.28318530718 * (t + 0)),
    0.46 + 0.5 * cos(6.28318530718 * (t + 0.05)),
    0.2453 + 0.2667 * cos(6.28318530718 * (t + 0.1)));
}

// Subtract when you can, add when you must: the sequence on the raw integer
// line, each stride a semicircle, sides alternating — Numberphile's
// rendering, credited as such on the plate. Coordinates are the integers
// themselves; scale to taste. Alpha carries the faint number line.
int forma_arc = 24;   // segments per semicircle

int n = int(rint(p_n)), start = int(rint(p_start));
int seen[], seq[];
push(seen, start);  push(seq, start);
int a = start, hi = start;
for (int k = 1; k <= n; k++){
    int back = a - k;
    if (back > 0 && find(seen, back) < 0) a = back;
    else                                  a = a + k;
    push(seen, a);  push(seq, a);
    hi = max(hi, a);
}
// the number line, faint
int base = addprim(0, "polyline");
int b0 = addpoint(0, set(0.0, 0.0, 0.0));
int b1 = addpoint(0, set(float(hi), 0.0, 0.0));
setpointattrib(0, "Cd", b0, forma_ramp(0.95));
setpointattrib(0, "Cd", b1, forma_ramp(0.95));
setpointattrib(0, "Alpha", b0, 0.2);
setpointattrib(0, "Alpha", b1, 0.2);
addvertex(0, base, b0);
addvertex(0, base, b1);
// the strides — one semicircle each, sides alternating
for (int k = 1; k <= n; k++){
    float x1 = float(seq[k - 1]), x2 = float(seq[k]);
    float cx = (x1 + x2) / 2.0, r = abs(x2 - x1) / 2.0;
    float up  = k % 2 == 1 ? 1.0 : -1.0;     // Houdini y-up: odd strides arc up
    float dir = x2 > x1 ? 1.0 : -1.0;
    vector col = forma_ramp(0.84 + 0.2 * float(k) / float(n));
    int prim = addprim(0, "polyline");
    for (int i = 0; i <= forma_arc; i++){
        float th = 3.14159265359 * float(i) / float(forma_arc);
        int pt = addpoint(0, set(cx - dir * r * cos(th), up * r * sin(th), 0.0));
        setpointattrib(0, "Cd", pt, col);
        setpointattrib(0, "Alpha", pt, 0.62);
        addvertex(0, prim, pt);
    }
}

AFTER EFFECTS · EXPRESSION

The same published mathematics as a Shape Layer path expression. Paste it onto a Path property; every constant is the published value plus a Slider Control named _tweak, so a bare paste already draws the figure and each slider moves one constant in its own units. Trim Paths is the comet.

// FORMA — PL. 74 · RECAMÁN SEQUENCE — Bernardo Recamán Santos, 1991 · via N. J. A. Sloane
//   a₀ = s
//   aₙ = aₙ₋₁ − n   if positive and unvisited
//   aₙ = aₙ₋₁ + n   otherwise
// After Effects port — paste onto a Shape Layer's Path property
// (Contents › Shape › Path). Written from the published mathematics, not
// adapted from any code. Constants arrive at their published values; add a
// Slider Control (Effect › Expression Controls) named <k>_tweak and that
// constant moves in its own units, starting at 0 — the published figure.
// The plate's comet and its reveal are Trim Paths; the stroke colour is
// FORMA's CURVES accent, #3DFF88. Animation runs on time.
// https://forma-gen.com/#plate=recaman

// A missing slider reads 0, so a bare paste already draws the figure.
function forma_tweak(n){ try { return effect(n)("Slider"); } catch (e){ return 0; } }
var p_n     = 85 + forma_tweak("n_tweak");        // strides · live 40 .. 140
var p_start = 0 + forma_tweak("start_tweak");     // s — start at · live 0 .. 40

// The frame: the plate's W × H canvas is this comp, with the origin at the
// layer's anchor; canvas y already runs down, as After Effects' does.
var forma_W = thisComp.width, forma_H = thisComp.height, forma_t = time;
var forma_phase = 0.48834191891364753;   // this plate's own fixed phase, as the page has it
function forma_pt(x, y){ return [x - forma_W / 2, y - forma_H / 2]; }

// Recamán's sequence: from s, step back k if that lands on a positive unseen
// number, otherwise step forward k. Each stride is drawn as a semicircle
// between its two terms, above the line for odd k and below for even, so the
// sequence becomes one continuous chain of arcs — the same chain the page
// reveals stride by stride (Trim Paths, End 0 → 100%).
var n = Math.round(p_n), start = Math.round(p_start);
var seen = {}, seq = [start], a = start, hi = start;
seen[start] = true;
for (var k = 1; k <= n; k++){
  var back = a - k;
  a = (back > 0 && !seen[back]) ? back : a + k;
  seen[a] = true; seq.push(a);
  if (a > hi) hi = a;
}
var sc = forma_W * 0.9 / hi, ox = forma_W * 0.05, yl = forma_H * 0.58;
var pts = [];
for (var m = 1; m <= n; m++){
  var x1 = ox + seq[m - 1] * sc, x2 = ox + seq[m] * sc;
  var cx = (x1 + x2) / 2, r = Math.abs(x2 - x1) / 2;
  var up = m % 2 === 1, dir = x2 > x1 ? 1 : -1;
  for (var i = (m === 1 ? 0 : 1); i <= 24; i++){
    var th = Math.PI * i / 24;
    pts.push(forma_pt(cx - dir * r * Math.cos(th), yl + (up ? -1 : 1) * r * Math.sin(th)));
  }
}
createPath(pts, [], [], false);