The Recamán's sequence generates Natural numbers.

Starting from a(0)=0, the n'th term a(n), where n>0, 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

 Generate and show here the first 15 members of the sequence.
 Find and show here, the first duplicated number in the sequence.
 Optionally: Find and show here, how many terms of the sequence are needed until all the integers 0..1000, inclusive, are generated.


References

 A005132, The On-Line Encyclopedia of Integer Sequences.
 The Slightly Spooky Recamán Sequence, Numberphile video.
 Recamán's sequence, on Wikipedia.


a = 0                                  ' the first one is free ;-)
Print "First 15 numbers:"
 
For i = 1 Step 1                       ' start loop
  If i<16 Then Print a,                ' print first 15 numbers
  b = Iif ((a-i<1) + (Func(_Peek(Max(0, a-i)))), a+i, a-i)
  If Func(_Peek(b)) Then Print "\nFirst repetition: ";b : Break
  Proc _Set(Set(a, b))                 ' set bit in bitmap
Next
End                                    ' terminate program
                                       ' bitmap functions
_Set  Param(1) : Let @(a@/32) = Func(_Poke(a@/32, a@%32)) : Return
_Poke Param(2) : Return (Or(@(a@), Shl(1, b@)))
_Peek Param(1) : Return (And(@(a@/32), Shl(1, a@%32))>0)
 