Van Eck sequence: Difference between revisions

From Rosetta Code
Content added Content deleted
(New draft task with python examples.)
 
(Fix, and change to less ambiguous letters.)
Line 2: Line 2:
The sequence is generated by following this pseudo-code:
The sequence is generated by following this pseudo-code:
<pre>
<pre>
1: The first term is zero.
A: The first term is zero.
Repeatedly apply:
Repeatedly apply:
If the last term is *new* to the sequence so far then:
If the last term is *new* to the sequence so far then:
2: The next term is zero.
B: The next term is zero.
Otherwise:
Otherwise:
3: The next term is how far back this last term occured previousely.
C: The next term is how far back this last term occured previousely.
</pre>
</pre>




;Example:
;Example:
Using 1:
Using A:
:<code>0</code>
:<code>0</code>
Using 2:
Using B:
:<code>0 0</code>
:<code>0 0</code>
Using 3:
Using C:
:<code>0 0 1</code>
:<code>0 0 1</code>
Using 2:
Using B:
:<code>0 0 1 0</code>
:<code>0 0 1 0</code>
Using 3: (zero last occured two steps back - before the one)
Using C: (zero last occured two steps back - before the one)
:<code>0 0 1 0 2</code>
:<code>0 0 1 0 2</code>
Using 1:
Using B:
:<code>0 0 1 0 2 0</code>
:<code>0 0 1 0 2 0</code>
Using 3: (two last occured two steps back - before the zero)
Using C: (two last occured two steps back - before the zero)
:<code>0 0 1 0 2 0 2 2</code>
:<code>0 0 1 0 2 0 2 2</code>
Using 3: (two last occured one step back)
Using C: (two last occured one step back)
:<code>0 0 1 0 2 0 2 2 1</code>
:<code>0 0 1 0 2 0 2 2 1</code>
Using 3: (one last appeared six steps back)
Using C: (one last appeared six steps back)
:<code>0 0 1 0 2 0 2 2 1 6</code>
:<code>0 0 1 0 2 0 2 2 1 6</code>
...
...

Revision as of 17:08, 17 June 2019

Van Eck sequence is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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 previousely.


Example

Using A:

0

Using B:

0 0

Using C:

0 0 1

Using B:

0 0 1 0

Using C: (zero last occured two steps back - before the one)

0 0 1 0 2

Using B:

0 0 1 0 2 0

Using C: (two last occured two steps back - before the zero)

0 0 1 0 2 0 2 2

Using C: (two last occured 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
  1. Create a function/proceedure/method/subroutine/... to generate the Van Eck sequence of numbers.
  2. Use it to display here, on this page:
  1. The first ten terms of the sequence.
  2. Terms 991 - to - 1000 of the sequence.
Reference

Python

<lang python>def van_eck():

   n, seen, val = 0, {}, 0
   while True:
       yield val
       last = {val: n}
       val = n - seen.get(val, n)
       seen.update(last)
       n += 1
  1. %%

if __name__ == '__main__':

   print("Van Eck: first 10 terms:  ", list(islice(van_eck(), 10)))
   print("Van Eck: terms 991 - 1000:", list(islice(van_eck(), 1000))[-10:])</lang>
Output:
Van Eck: first 10 terms:   [0, 0, 1, 0, 2, 0, 2, 2, 1, 6]
Van Eck: terms 991 - 1000: [4, 7, 30, 25, 67, 225, 488, 0, 10, 136]

Python: Alternate

The following stores the sequence so far in a list seen rather than the first example that just stores last occurrences in a dict. <lang python>def van_eck():

   n = 0
   seen = [0]
   val = 0
   while True:
       yield val
       if val in seen[1:]:
           val = seen.index(val, 1)
       else:
           val = 0
       seen.insert(0, val)
       n += 1</lang>
Output:

As before.