The sequence is generated by following this pseudo-code:

A:  The first term is zero.
    Repeatedly apply:
        If the last term is *new* to the sequence so far then:
B:          The next term is zero.
        Otherwise:
C:          The next term is how far back this last term occured previously.



Example

Using A:

0
Using B:

0 0
Using C:

0 0 1
Using B:

0 0 1 0
Using C: (zero last occurred two steps back - before the one)

0 0 1 0 2
Using B:

0 0 1 0 2 0
Using C: (two last occurred two steps back - before the zero)

0 0 1 0 2 0 2 2
Using C: (two last occurred one step back)

0 0 1 0 2 0 2 2 1
Using C: (one last appeared six steps back)

0 0 1 0 2 0 2 2 1 6
...



Task

 Create a function/procedure/method/subroutine/... to generate the Van Eck sequence of numbers.
 Use it to display here, on this page:
 The first ten terms of the sequence.
 Terms 991 - to - 1000 of the sequence.


References

 Don't Know (the Van Eck Sequence) - Numberphile video.
 Wikipedia Article: Van Eck's Sequence.
  OEIS sequence: A181391.


        IDENTIFICATION DIVISION.
        PROGRAM-ID. VAN-ECK.
 
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 CALCULATION.
            02 ECK      PIC 999 OCCURS 1000 TIMES.
            02 I        PIC 9999.
            02 J        PIC 9999.
        01 OUTPUT-FORMAT.
            02 ITEM     PIC ZZ9.
            02 IDX      PIC ZZZ9.
 
        PROCEDURE DIVISION.
        B.  PERFORM GENERATE-ECK.
            PERFORM SHOW VARYING I FROM 1 BY 1 UNTIL I = 11.
            PERFORM SHOW VARYING I FROM 991 BY 1 UNTIL I = 1001.
            STOP RUN.
 
        SHOW.
            MOVE I TO IDX.
            MOVE ECK(I) TO ITEM.
            DISPLAY 'ECK(' IDX ') = ' ITEM.
 
        GENERATE-ECK SECTION.
        B.  SET ECK(1) TO 0.
            SET I TO 1.
            PERFORM GENERATE-TERM 
                VARYING I FROM 2 BY 1 UNTIL I = 1001.
 
        GENERATE-TERM SECTION.
        B.  SUBTRACT 2 FROM I GIVING J.
        LOOP.
            IF J IS LESS THAN 1 GO TO TERM-IS-NEW.
            IF ECK(J) = ECK(I - 1) GO TO TERM-IS-OLD.
            SUBTRACT 1 FROM J.
            GO TO LOOP.
 
        TERM-IS-NEW.
            SET ECK(I) TO 0.
            GO TO DONE.
 
        TERM-IS-OLD.
            COMPUTE ECK(I) = (I - J) - 1.
 
        DONE. EXIT.