Recamán Sequence: Draw Honest SVG Arcs
Preserve the legal repeated 42, derive every semicircle from the recurrence, and export an accessible poster with deterministic receipts.
The Recamán sequence makes hypnotic arc posters, but the popular ‘never repeat a number’ explanation is false: only the subtraction candidate must be unused. This tutorial implements the exact recurrence, turns every step into an auditable SVG arc, and exports a reproducible poster receipt.
The exact Recamán sequence recurrence comes first
Set a(0)=0. For each positive step n, first consider subtraction: c=a(n−1)−n. Choose c only when it is positive and has not appeared among the earlier terms. Otherwise choose a(n−1)+n. That ordered branch rule—not a general ban on repeats—is the complete finite construction used here.
The OEIS A005132 record is the authority for the sequence values and references. A useful implementation stores more than the new value. Every row records n, prior value, subtraction candidate, whether the candidate was positive, whether it had been visited, chosen branch, chosen value, and a snapshot hash of the visited set. With that ledger, an SVG can be traced back to arithmetic rather than treated as decorative output.
Start with known early terms: 0, 1, 3, 6, 2, 7, 13. At n=6 the subtraction candidate is 1, which is positive but already visited, so the addition branch yields 13. That row tests branch precedence. A program that checks positivity but forgets membership will emit a familiar-looking arc field that is already wrong.
The lab bounds the requested length to 8–256 terms. It generates integers exactly, keeps the work finite, and rejects pasted signs, decimals, exponent notation, and oversized input. The generative poster begins only after the recurrence ledger passes. If the numbers are wrong, no amount of elegant stroke styling can rescue the edition.
| Step | Previous | Subtraction candidate | Reason | Chosen value |
|---|---|---|---|---|
| 6 | 7 | 1 | Positive but already used; add | 13 |
| 24 | 18 | −6 | Not positive; add even though result repeats | 42, also a(20) |
Reading rule: labels, order, values, shapes, and patterns carry the conclusion; color is supplementary.
Legal repeated values reveal the common myth
The Recamán sequence rule prevents reuse only when selecting the subtraction candidate. The addition fallback is accepted even when its result has appeared before. That detail creates a legal repeat: a(20)=42 and a(24)=42. A tutorial that says the Recamán sequence never repeats values has replaced the recurrence with a different algorithm.
The distinction becomes clear at n=24. The previous term is 18. Subtraction proposes −6, which is not positive, so the rule falls back to 18+24=42. The fact that 42 was already visited at n=20 does not veto the addition. The ledger highlights both rows and labels the second occurrence “legal addition repeat.”
This is original value a visual tutorial can make tangible. Two arcs may arrive at the same numbered endpoint at different moments without indicating a duplicate rendering bug. The semantic table lists both step numbers and their branch reasons; the graphic marks the shared endpoint with a diamond and heavier outline so the claim survives without color.
The first mutation fixture, BAN_REPEATED_42, applies a global uniqueness filter. It must diverge at step 24 and be rejected with an exact diagnostic. The inverse mistake also has a fixture: ACCEPT_USED_SUBTRACTION chooses a positive subtraction candidate even though it has already appeared. Together they prove the validator understands the asymmetric rule instead of merely comparing a few memorized terms.
This finite evidence does not establish that the sequence eventually visits every integer. It does not make a claim about terms beyond the generated bound. It only proves that the chosen prefix follows the declared greedy recurrence, including its legal repetition.
Turn each step into honest SVG arcs
Map each Recamán sequence integer value to an x-coordinate with one fixed horizontal scale. Consecutive terms become the endpoints of a semicircle. For an arc from x₀ to x₁, the radius in sequence units is |a(n)−a(n−1)|/2. Because the recurrence changes the value by exactly n, the expected radius is n/2 before applying the drawing scale.
An SVG elliptical arc path uses a move command followed by A rx ry 0 0 sweep x y. The SVG 2 path specification defines those parameters. This poster uses equal radii, a zero rotation, the small-arc flag, and a declared sweep bit. The viewport adds padding around the maximum radius so the first or last stroke cannot be clipped.
The geometry receipt retains both mathematical and pixel units: prior value, next value, delta, sequence radius, x endpoints, pixel radius, baseline, sweep, and exact path string. An independent parser reads the exported d attribute and recomputes the expected endpoints and radius. It does not trust the generator's intermediate object.
The WRONG_RADIUS mutant uses n rather than n/2. WRONG_ENDPOINT swaps the sequence value for the step index. Both still produce valid SVG syntax, which is why syntax validation alone is insufficient. A visible poster can be aesthetically coherent while lying about its source data. Honest generative work treats geometry as a testable mapping.
Alternate sweeps without hiding chronology
Odd and even Recamán sequence steps alternate above and below the baseline. In SVG terms, that means the sweep flag changes with step parity while the endpoints preserve the sequence order. The alternation is a composition rule, not part of the Recamán recurrence, so the receipt labels it separately.
Chronology remains visible through more than hue. Every eighth arc receives a slightly heavier stroke, selected milestones have numbered tick marks, and the repeated-42 endpoint uses a diamond. The semantic equivalent is an ordered table whose rows match the SVG group order. A viewer can therefore recover sequence, branch, direction, and emphasis even in forced-colors mode or without the image.
The WRONG_SWEEP mutant freezes every flag to the same side. Its values and radii remain correct, so the arithmetic audit passes while the composition audit fails. That separation matters: one giant pass state would not identify whether a defect lives in recurrence, geometry, styling, or accessibility.
Related creative-coding systems make the same distinction. Fourier drawing with SVG epicycles separates coefficient math from traced geometry, while hypotrochoid SVG posters separates roulette parameters from the final path. Here, arithmetic chooses endpoints and the alternating-sweep policy chooses presentation. Both are explicit, and neither impersonates the other.
- Endpoints
- Scaled values a(n−1) and a(n), never step indexes.
- Radius
- |a(n)−a(n−1)|/2 = n/2 sequence units.
- Flags
- Zero rotation, small arc, and a sweep bit derived from parity.
- Chronology
- SVG group and semantic-table rows use step order.
Reading rule: labels, order, values, shapes, and patterns carry the conclusion; color is supplementary.
Build a generative poster system, not a rainbow trace
A disciplined poster needs hierarchy. The background is warm bone, primary strokes alternate cobalt and coral, milestone strokes gain weight, and endpoint symbols use charcoal outlines. Color supports the reading but does not carry it alone. Line weight, dash, marker shape, labels, and DOM order provide redundant signals.
Use one scale derived from the generated prefix and the available width. Do not rescale individual arcs. A global transform preserves relative radius and makes editions comparable. The exported SVG records steps, scale, padding, palette name, maximum term, maximum radius, and a canonical hash in metadata. Changing the palette produces a new edition hash while the recurrence hash remains stable.
The composition should tolerate density. At small step counts, the poster shows milestone labels and repeated endpoints. At larger counts, it reduces label frequency while leaving every arc in the accessible description table. Strokes use vector-effect="non-scaling-stroke" so responsive resizing does not turn the largest arcs into visual walls.
The approach extends beyond this sequence. Maurer rose SVG generative art uses a modular walk to create structure; one-stroke kolam SVG treats topology as a release gate. The Recamán sequence poster combines those instincts: a strict discrete rule, a legible visual system, and evidence that the output still corresponds to the input after styling.
Validate recurrence and geometry independently
The Recamán sequence lab uses two independent passes. The generator produces rows by maintaining a visited set. The verifier ignores the stored branch and rebuilds the expected next term from prior emitted values. It checks that the subtraction candidate was positive and unseen when selected, that fallback addition was used otherwise, and that |a(n)−a(n−1)|=n.
The geometry verifier starts from the verified value pairs, not from the generator's path objects. It parses each path string, checks the move and endpoint coordinates, derives the expected pixel radius from n/2, and verifies the alternating sweep. It also confirms that viewBox bounds contain each arc extremum plus the declared padding.
Independent hashes make the separation visible. The recurrence receipt hashes ordered values and branch reasons. The geometry receipt hashes scale, baseline, paths, and bounds. The edition receipt adds palette and stroke hierarchy. If a palette changes, recurrence and geometry hashes remain fixed. If the step count changes, all downstream hashes change.
Five mutants exercise the independent checks: global-repeat ban, already-used subtraction, wrong radius, wrong endpoint, and wrong sweep. Each has one expected failure code. A control run must pass before the mutants run, and repeated controls must serialize identically. This prevents a mutation suite from looking healthy when the baseline is already invalid.
These tests verify a bounded prefix and its elliptical arc path mapping. They do not resolve conjectures about the infinite sequence, and they do not prove that every SVG renderer rasterizes strokes identically.
- Odd arcs: solid stroke and upper sweep.
- Even arcs: dashed or lower sweep.
- Milestones: heavier stroke plus numbered baseline tick.
- Repeated 42: outlined diamond plus text label naming steps 20 and 24.
- Bounds: one global scale and explicit viewBox padding.
Reading rule: labels, order, values, shapes, and patterns carry the conclusion; color is supplementary.
Export accessible SVG with its semantic twin
The downloadable Recamán sequence SVG is a document, not a screenshot. It has a title and description connected with aria-labelledby, a stable viewBox, a background rectangle, grouped arcs in chronological order, and metadata containing the receipt hash. The visible poster includes a compact legend for side, weight, and the repeated-value marker.
Accessibility requires a second representation of the thesis. The SVG Accessibility API Mappings explains how SVG elements participate in accessibility APIs, but support details can vary. The article therefore places a visible HTML table beside every thesis figure, and the lab places an ordered receipt table beside the generated SVG. It is useful even when the graphic is unavailable.
Exports share provenance. JSON contains the full recurrence, geometry, bounds, palette, hashes, and mutation outcomes. CSV contains one row per arc with branch and path data. SVG includes the same recurrence and geometry hashes in metadata. The download filenames include the bounded step count but no clock value, so identical inputs generate identical file bytes.
The lab never inserts user text with innerHTML. It parses one integer, clamps work through explicit validation, writes status through textContent, constructs SVG nodes with the namespace API, and issues no request. Keyboard focus order follows input, generate, run mutations, and three export buttons. Reduced-motion mode changes nothing because the poster has no animation.
Let mutation tests protect the artwork
A finished Recamán sequence edition should ship with its control receipt and mutation ledger. BAN_REPEATED_42 protects the legal addition repeat. ACCEPT_USED_SUBTRACTION protects the visited-set branch. WRONG_RADIUS protects n/2 geometry. WRONG_ENDPOINT protects value-to-position mapping. WRONG_SWEEP protects the declared alternating composition.
Reviewers should check the repeated 42 explicitly, not only early terms. They should compare recurrence and geometry hashes across two runs, open the SVG without the lab, inspect the title and description, and read several rows in the semantic table. A screenshot approval is useful for composition but cannot substitute for those checks.
The release decision is narrow: does this bounded poster faithfully render the exact recurrence under the declared visual rules? Passing does not establish a mathematical property of uncomputed terms, prove that every integer appears, or claim novelty for the sequence itself. It establishes reproducibility and honest mapping for one edition.
That constraint makes the art stronger. The shared endpoint at 42 becomes a compositional event grounded in a real rule rather than an arbitrary flourish. The alternating arcs form rhythm, while branch labels expose the discrete logic underneath. The result can be playful and rigorous at the same time because the poster does not ask viewers to choose between visual delight and inspectable truth.
Runnable local artifact — The lab verifies only its finite recurrence prefix and SVG geometry; it makes no claim about uncomputed terms, eventual coverage, or unresolved sequence conjectures.
Choose 8–256 terms, rebuild the ordered branch ledger, require |a(n)−a(n−1)|=n, allow legal repeated additions, derive radius n/2 and alternating sweeps, run five mutation fixtures, and export deterministic SVG, JSON, or CSV.