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.


            NORMAL MODE IS INTEGER
            VECTOR VALUES ELEMF = $2HA(,I2,4H) = ,I2*$
            DIMENSION A(100)
            A(0) = 0
 
            PRINT COMMENT $ FIRST 15 ELEMENTS$
            PRINT FORMAT ELEMF,0,0
            THROUGH EL, FOR I=1, 1, I.GE.15
EL          PRINT FORMAT ELEMF,I,NEXT.(I)
 
            PRINT COMMENT $ $
            PRINT COMMENT $ FIRST REPEATED ELEMENT$
RPT         THROUGH RPT, FOR I=I, 1, FIND.(I,NEXT.(I))
            PRINT FORMAT ELEMF,I,A(I)
 
            INTERNAL FUNCTION(N,TOP)
            ENTRY TO FIND.
            FI=0
SRCH        WHENEVER FI.GE.TOP, FUNCTION RETURN 0B
            WHENEVER A(FI).E.N, FUNCTION RETURN 1B
            FI=FI+1
            TRANSFER TO SRCH
            END OF FUNCTION
 
            INTERNAL FUNCTION(N)
            ENTRY TO NEXT.
            HI=A(N-1)+N
            LO=A(N-1)-N
            WHENEVER LO.L.0
                A(N)=HI
            OR WHENEVER FIND.(LO,N)
                A(N)=HI
            OTHERWISE
                A(N)=LO
            END OF CONDITIONAL
            FUNCTION RETURN A(N)
            END OF FUNCTION
            END OF PROGRAM 