Problem 44338. Recaman Sequence - I
Recaman Sequence (A005132 - - OEIS Link) is defined as follow;
seq(0) = 0; for n > 0, seq(n) = seq(n-1) - n if positive and not already in the sequence, otherwise seq(n) = seq(n-1) + n.
seq = 0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9 ... index = 1, 2, 3 ,...
To avoid zero index, start indexing from 1. return the first n elements in Recaman Sequence
Related Challenges :
- Recaman Sequence - I
- Recaman Sequence - II
- Recaman Sequence - III
Solution Stats
Problem Comments
-
8 Comments
Recaman sequence uses zero indexing, thus `seq(n) = seq(n-1) - (n-1)` or `seq(n) = seq(n-1) + (n-1)` (not the formula used in the description).
That is correct. I have modified the sequence to avoid zero indexing.
According to the definition here, seq(2) should be 2 because seq(n-1)+n when n=2, is 2. And so on...
Adiel, Thanks for your attention. I have changed the definition.
When i preinitialise y as follows:
y = zeros(1,x);
it doesn't pass, but when i just ommit preinitialisation - it works perfectly!
Hello, Mehmet OZC. I think the problem statement is still subject to misinterpretation. At the moment it looks like "index" means "n", which is inconsistent with the formula. To avoid this misunderstanding, you could insert one extra row to show explicitly the values of "n". ... Thus you would have: seq = 0, 1, 3, 6, ... . Then, n = 0, 1, 2, 3, ... . And finally, Index = 1, 2, 3, 4, ... . ____ Alternatively, you could omit any mention of indexing and just clarify with an _example_, such as: "The first four elements are [0, 1, 3, 6]."
David, you have already made a good explanation for a possible misinterpretation. I appreciate it. Thanks for your contribution. When I said indexing I meant something like linear indexing (https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html)
good question :-)
Solution Comments
Show commentsProblem Recent Solvers308
Suggested Problems
-
Project Euler: Problem 7, Nth prime
1427 Solvers
-
Project Euler: Problem 9, Pythagorean numbers
1150 Solvers
-
Back to basics 21 - Matrix replicating
1531 Solvers
-
Make a random, non-repeating vector.
9272 Solvers
-
7195 Solvers
More from this Author92
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!