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.


01.10 T "FIRST 15"
01.20 F N=0,14;D 2;T %2,A(N)
01.30 T !"FIRST REPEATED"
01.40 D 2;S Y=1
01.50 F M=0,N-1;S Y=Y*(A(M)-A(N))
01.60 I (Y)1.7,1.8,1.7
01.70 S N=N+1;G 1.4
01.80 T A(N)," AT A(",N,")"!
01.90 Q
 
02.05 I (N)2.1,2.06,2.1
02.06 A(0)=0;R
02.10 S X=A(N-1)-N
02.20 I (X)2.7
02.30 S Y=1
02.40 F M=0,N-1;S Y=Y*(A(M)-X)
02.50 I (Y)2.6,2.7,2.6
02.60 S A(N)=X;R
02.70 S A(N)=A(N-1)+N