Van Eck Sequence
June 14, 2019
We maintain an a-list prev
that stores, for each v in the list, the index i at which it appears. There will be one entry in the a-list for each member of the sequence; repeated sequence entries will appear multiple times, and assoc
will return the most recent, which stores the associated index:
(define-generator (van-eck) (let loop ((v 0) (i 0) (prev (list))) (yield v) (let ((t v)) (let ((v (- i (cond ((assoc v prev) => cdr) (else i))))) (loop v (+ i 1) (cons (cons t i) prev))))))
Here is the function in action:
> (take-gen 25 (van-eck)) (0 0 1 0 2 0 2 2 1 6 0 5 0 2 6 5 4 0 5 3 0 3 2 9 0)
You can run the program at https://ideone.com/tKuo7T.
Fun little sequence, though not much of a programming challenge. Here is my take on it using Julia 1.0:
function main(n::Int64 = 100)
x = zeros(Int64, n)
end
Interestingly, the sequence yields larger gaps towards the higher numbers, for the samples tested. This makes the sequence “complete” for the early part of it, a pattern that is likely to continue for larger samples. Still not convinced about its usefulness, but a fun little exercise nevertheless. Have a nice weekend!
In Python.
Here is a R7RS Scheme + (srfi 69) solution that runs in linear time
(or constant time per element if implemented as a generator), with the
usual hash-table caveat.
Here’s a solution in C++.
Example Usage: