Recaman's sequence

From Fōrmulæ wiki
Revision as of 12:06, 13 December 2019 by Admin (Talk | contribs) (Description (from Rosetta Code))

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This page is a solution to the task Recaman's sequence in the Rosetta Code, written in Fōrmulæ.

Description (from Rosetta Code)

The Recamán's sequence generates Natural numbers.

Starting from zero, the n'th term a(n) is the previous term minus n i.e a(n) = a(n-1) - n but only if this is both positive and has not been previousely generated.

If the conditions don't hold then a(n) = a(n-1) + n.


  1. Generate and show here the first 15 members of the sequence.
  2. Find and show here, the first duplicated number in the sequence.
  3. Optionally: Find and show here, How many terms of the sequence are needed until all the integers 0..1000, inclusive, are generated.



Generation of the sequence

The following snippet generates the Recaman's sequence of a given number of terms:


This is an example:


Calculating all the cases of the task in a single program

The following is the program:


Running for computation of the first 15 members of the sequence, the first duplicate and the number of terms to generate the first 1,000 integers:


Additional examples

Plotting the sequence

The following snippet creates a plot for a given sequence:


Drawing the sequence as it was shown in the Numberphile video

The program:


And an example: