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.


INT FUNC LastPos(INT ARRAY a INT count,value)
  INT pos
 
  pos=count-1
  WHILE pos>=0 AND a(pos)#value
  DO
    pos==-1
  OD
RETURN (pos)
 
PROC Main()
  DEFINE MAX="1000"
  INT ARRAY seq(MAX)
  INT i,pos
 
  seq(0)=0
  FOR i=1 TO MAX-1
  DO
    pos=LastPos(seq,i-1,seq(i-1))
    IF pos>=0 THEN
      seq(i)=i-1-pos
    ELSE
      seq(i)=0
    FI
  OD
 
  PrintE("Van Eck first 10 terms:")
  FOR i=0 TO 9
  DO
    PrintI(seq(i)) Put(32)
  OD
  PutE() PutE()
 
  PrintE("Van Eck terms 991-1000:")
  FOR i=990 TO 999
  DO
    PrintI(seq(i)) Put(32)
  OD
RETURN