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.
- maintain a collection (array) of visited numbers.
- Loop from 1 to
targetCount - 1. - Calculate the next jump.
- Print each member of the sequence on a new line.
01EXAMPLE 1
Input
targetCount = 5Output
0
1
3
6
2Explanation: 4-3 is 1 (visited), so 3+3=6. 6-4=2 (not visited).
Constraints
- Print exactly count numbers.
- Check "already visited" correctly.
MathLoopsLogic
JavaScriptSystem handles I/O — write your function only
Loading...
2 Hidden
Input Arguments
targetCount5
Expected Output
0 1 3 6 2
Click RUN to test your solution