Recaman's Sequence

HardAcc. 70.8%
+45 XP 25

The Skipping Stone

Recaman's sequence $a_n$ is defined as: $a_0 = 0$ $a_n = a_{n-1} - n$ (if the result is positive and not already in the sequence) Else, $a_n = a_{n-1} + n$.

The Assignment

Your function receives a parameter targetCount.

  1. maintain a collection (array) of visited numbers.
  2. Loop from 1 to targetCount - 1.
  3. Calculate the next jump.
  4. Print each member of the sequence on a new line.

01EXAMPLE 1

InputtargetCount = 5
Output0 1 3 6 2

Explanation: 4-3 is 1 (visited), so 3+3=6. 6-4=2 (not visited).

Constraints

  • Print exactly count numbers.
  • Check "already visited" correctly.
MathLoopsLogic
JavaScript
Loading...
2 Hidden

Input Arguments

targetCount5

Expected Output

0
1
3
6
2

Click RUN to test your solution