Weekly Challenge: Recamán's Sequence

This week we’ll get to know Recamán's sequence. It is a sequence of nonnegative integers and can be written like so: a(0) = 0; for n > 0, a(n) = a(n – 1) – n if that number is positive and not already in the sequence, otherwise a(n) = a(n – 1) + n.

For you to fully understand the sequence, I had written a few members (7 to be exact) {0, 1, 3, 6, 2, 7, 13} and now I’m looking for the 8th element. So the first step for me is to calculate a(8) based on a(n) = a(n – 1) – n which would be 6 and I can see that this number is already in the sequence, therefore I calculate a(8) based on a(n) = a(n – 1) + n and get 20. I would have to do the same if a(n) was negative.

Find out more about the Recamán's sequence here.

In this challenge your task is to create a function that would find an element in Recamán's sequence when an index of the requested element is given. You should not create a static array or list and write the elements yourself since the element you have to search for could have an index of a 1000 or more and you would lose points as well.

For example, you are given an index (n) with a value of

9000
so after finding the element in the sequence (a(n)) the value of the answer in this case should be assigned to
9470

The given index will always be greater than 0.
The function getNoInRecamansSeq(int index) is where you should write your code. You don’t have to copy the whole class in this case, but if that makes it easier for you, go ahead.

If some details are not clear write an email to and I will try to add more info.

Starts on Tue, 11 October 2016 10:00

Ends on Sat, 15 October 2016 10:00

Points you can get: 15