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.


   positive =: >&0
   unique =: -.@:e.
   condition =: (positive@:] *. unique~) ({: - #)

   NB. with the agenda set by the condition, add or subtract tail with tally
   recaman_term =: ({: + #)`({: - #)@.condition


   NB. generate four hundred thousand terms and display the first 15
   15 {. R=:(, recaman_term)^:400000]0
0 1 3 6 2 7 13 20 12 21 11 22 10 23 9


   NB. plot the sequence to see why numberphile might be interested.
   load'plot'
   plot 470{.R

   NB. binaryish search for first duplicate.
   (-:&# ~.) 100 {. R
0
   (-:&# ~.) 50 {. R
0
   (-:&# ~.) 25 {. R
0
   (-:&# ~.) 12 {. R
1
   (-:&# ~.) 18 {. R
1
   (-:&# ~.) 21 {. R
1
   (-:&# ~.) 23 {. R
1
   (-:&# ~.) 24 {. R
1
   (-:&# ~.) 25 {. R
0
