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.

Task

  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.

References:

Answer

Generation of the sequence

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

RecamanCodeGeneral.png

This is an example:

RecamanCodeGeneralExample.png

Calculating all the cases of the task in a single program

The following is the program:

RecamanCode.png

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:

Recaman01.png

Additional examples

Plotting the sequence

The following snippet creates a plot for a given sequence:

RecamanPlotting01.png

Drawing the sequence as it was shown in the Numberphile video

The program:

RecamanDrawingProgram.png

And an example:

RecamanDrawingExample01.png