A181391 Van Eck's sequence: For n >= 1, if there exists an m < n such that a(m) = a(n), take the largest such m and set a(n+1) = n-m; otherwise a(n+1) = 0. Start with a(1)=0.
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, 4, 9, 3, 6, 14, 0, 6, 3, 5, 15, 0, 5, 3, 5, 2, 17, 0, 6, 11, 0, 3, 8, 0, 3, 3, 1, 42, 0, 5, 15, 20, 0, 4, 32, 0, 3, 11, 18, 0, 4, 7, 0, 3, 7, 3, 2, 31, 0, 6, 31, 3, 6, 3, 2, 8, 33, 0, 9, 56, 0, 3, 8, 7, 19, 0, 5, 37, 0, 3, 8, 8, 1
Offset: 1
Examples
We start with a(1) = 0. 0 has not appeared before, so the rule says a(2) = 0. Now 0 HAS occurred before, at a(1), which is 1 term before, so a(3) = 1. 1 has not occurred before, so a(4) = 0. 0 appeared most recently at term a(2), which is 2 terms earlier, so a(5) = 2. 2 has not occurred before, so a(6) = 0. And so on.
Links
- N. J. A. Sloane, Table of n, a(n) for n = 1..100000
- Benjamin Chaffin, Frequency of values 0..500 over the first 10^12 terms
- Benjamin Chaffin, Frequency of values 0..1000000 over the first 10^12 terms
- Benjamin Chaffin, Average distance between 0s over the first 10^12 terms
- Po-chia Chen, Additional comments on A181391, Jun 19 2019 [These comments have not yet been reviewed. - _N. J. A. Sloane_, Jun 20 2019]
- Po-chia Chen, Further comments on A181391, Jun 20 2019 [These comments have also not yet been reviewed. - _N. J. A. Sloane_, Jul 05 2019]
- Code Golf Stack Exchange, Nth term of Van Eck Sequence
- Brady Haran and N. J. A. Sloane, Don't Know (the Van Eck Sequence), Numberphile video (2019).
- N. J. A. Sloane, Fortran program
Crossrefs
Cf. A171862, A171863, A171864, A171865, A171866, A171867, A171887, A171888, A171889, A171896, A171897 (numbers in order of appearance), A171898, A171899.
Cf. also A171911-A171918 (starting with other numbers than 0), A171951-A171956, A171957, A171958, A175041, A175100, A268755, A274425, A309363 (using 2 instead of 0 to mark a new value).
Cf. A025480.
Programs
-
Haskell
import Data.List (findIndex, unfoldr) import Data.Maybe (fromMaybe) a181391 n = a181391_list !! (n-1) a181391_list = 0 : (unfoldr g [0]) where g xs = Just (m, m : xs) where m = 1 + fromMaybe (-1) (findIndex (== head xs) $ tail xs) -- Reinhard Zumkeller, Oct 31 2011
-
J
NB. see www.Jsoftware.com (, # <:@- }: i: {:)^:({.`}.) 100 0 NB. Arie Bos, Dec 10 2010
-
Julia
function A181391(len) L = [0, 0] for n in 2:len k = findlast(m -> L[n] == L[m], 1:n-1) push!(L, k == nothing ? 0 : n - k) end L end println(A181391(96)) # Peter Luschny, May 19 2019
-
Maple
M:=10000; a:=Array(1..M); last:=Array(0..M,-1); a[1]:=0; a[2]:=0; last[0]:=2; nxt:=1; for n from 3 to M do hist:=last[nxt]; a[n]:=nxt; last[nxt]:=n; nxt:=0; if hist>0 then nxt:=n-hist; fi; od: [seq(a[n],n=1..M)]; # N. J. A. Sloane, Oct 18 2010
-
Mathematica
m = 100; ClearAll[a, last]; a[] = 0; last[] = -1; last[0] = 2; nxt = 1; Do[ hist = last[nxt]; a[n] = nxt; last[nxt] = n; nxt = 0; If[ hist > 0 , nxt = n - hist], {n, 3, m}]; Table[a[n], {n, 1, m}] (* Jean-François Alcover, Dec 01 2011, after Maple program by N. J. A. Sloane *) A181391L = Nest[# /. {{Longest[p___], a_, q___, a_} :> {p, a, q, a, Length[{a, q}]}, {a___} :> {a, 0}} &, {}, #] &; A181391L[97] (* JungHwan Min, Jan 14 2017 *)
-
PARI
A181391_vec(N,a=0,i=Map())={vector(N,n,a=if(n>1,iferr(n-mapget(i,a),E,0)+mapput(i,a,n)))} \\ M. F. Hasler, Jun 11 2019
-
Python
A181391 = [0,0] for n in range(1,10**4): for m in range(n-1,-1,-1): if A181391[m] == A181391[n]: A181391.append(n-m) break else: A181391.append(0) # Chai Wah Wu, Aug 14 2014
-
Python
A181391 = [0] last_pos = {} for i in range(10**4): new_value = i - last_pos.get(A181391[i], i) A181391.append(new_value) last_pos[A181391[i]] = i # Ehsan Kia, Jun 12 2019
-
R
vaneckw <-function(howmany = 100){ howmany = round(howmany[1]) ve = c(0,0) for (jj in 2:(howmany)) { thefind <- which(ve[1:(jj-1)] == ve[jj]) if (length(thefind)){ ve <- c(ve,jj-thefind[length(thefind)]) } else ve <- c(ve,0) } return(invisible(ve)) } #Carl Witthoft, Jun 14 2019
Comments