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.


 
/* ARM assembly Raspberry PI  */
/*  program vanEckSerie.s   */
 
 /* REMARK 1 : this program use routines in a include file 
   see task Include a file language arm assembly 
   for the routine affichageMess conversion10 
   see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes                       */
/************************************/
.include "../constantes.inc"
 
.equ MAXI,      1000
 
/*********************************/
/* Initialized data              */
/*********************************/
.data
sMessResultElement:    .asciz " @ "
szCarriageReturn:   .asciz "\n"
 
/*********************************/
/* UnInitialized data            */
/*********************************/
.bss  
sZoneConv:                  .skip 24
TableVanEck:                .skip 4 * MAXI
/*********************************/
/*  code section                 */
/*********************************/
.text
.global main 
main:                             @ entry of program 
    mov r2,#0                     @ begin first element
    mov r3,#0                     @ current counter
    ldr r4,iAdrTableVanEck        @ table address
    str r2,[r4,r3,lsl #2]         @ store first zéro
1:                                @ begin loop 
    mov r5,r3                     @ init current indice
2:
    sub r5,#1                     @ decrement
    cmp r5,#0                     @ end table ?
    movlt r2,#0                   @ yes, move zero to next element
    blt 3f
    ldr r6,[r4,r5,lsl #2]         @ load element
    cmp r6,r2                     @ and compare with the last element
    bne 2b                        @ not equal
    sub r2,r3,r5                  @ else compute gap
3:
    add r3,r3,#1                  @ increment counter
    str r2,[r4,r3,lsl #2]         @ and store new element
    cmp r3,#MAXI
    blt 1b
 
    mov r2,#0
4:                                @ loop display ten elements
    ldr r0,[r4,r2,lsl #2]
    ldr r1,iAdrsZoneConv
    bl conversion10               @ call décimal conversion
    ldr r0,iAdrsMessResultElement
    ldr r1,iAdrsZoneConv          @ insert conversion in message
    bl strInsertAtCharInc
    mov r1,#0                     @ final zéro
    strb r1,[r0,#5]               @ 
    bl affichageMess              @ display message
    add r2,#1                     @ increment indice
    cmp r2,#10                    @ end ?
    blt 4b                        @ no -> loop
    ldr r0,iAdrszCarriageReturn
    bl affichageMess
 
    mov r2,#MAXI - 10
5:                                @ loop display ten elements 990-999
    ldr r0,[r4,r2,lsl #2]
    ldr r1,iAdrsZoneConv
    bl conversion10               @ call décimal conversion
    ldr r0,iAdrsMessResultElement
    ldr r1,iAdrsZoneConv          @ insert conversion in message
    bl strInsertAtCharInc
    mov r1,#0                     @ final zéro
    strb r1,[r0,#5]               @ 
    bl affichageMess              @ display message
    add r2,#1                     @ increment indice
    cmp r2,#MAXI                    @ end ?
    blt 5b                        @ no -> loop
    ldr r0,iAdrszCarriageReturn
    bl affichageMess
 
100:                              @ standard end of the program 
    mov r0, #0                    @ return code
    mov r7, #EXIT                 @ request to exit program
    svc #0                        @ perform the system call
iAdrszCarriageReturn:    .int szCarriageReturn
iAdrsMessResultElement:  .int sMessResultElement
iAdrsZoneConv:           .int sZoneConv  
iAdrTableVanEck:         .int TableVanEck
 
/***************************************************/
/*      ROUTINES INCLUDE                           */
/***************************************************/
.include "../affichage.inc"
 