Difference between revisions of "Recaman's sequence"

From Fōrmulæ wiki
Jump to: navigation, search
(Creation of page)
 
(No difference)

Latest revision as of 19:42, 14 August 2019

This page is the answer to the task Recaman's sequence in the Rosetta Code.

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

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