OFFSET
1,4
COMMENTS
A variant of "Van Eck's" sequence A181391, but "looking back" for the second to last appearance of the value of a(n) instead of the last one. If a(n) has appeared just once before, a(n+1) is also 0.
LINKS
Allan Teitelman, Table of n, a(n) for n = 1..10000
Allan Teitelman, C Code
MAPLE
b:= proc() [0$3] end:
a:= proc(n) option remember; local t; t:= (h->
`if`(h=0, 0, n-1-h))(`if`(n=1, 0, b(a(n-1))[1]));
b(t):= [b(t)[2..3][], n]; t
end:
seq(a(n), n=1..100); # Alois P. Heinz, Apr 19 2021
MATHEMATICA
a[1]=0; a[n_]:=a[n]=If[Length[s=Position[Array[a, n-1], a[n-1]]]>2, First@@{s[[-1]]-s[[-3]]}, 0]; Array[a, 100] (* Giorgos Kalogeropoulos, Apr 24 2021 *)
PROG
(C) see link above
(Python)
def aupton(terms):
n, alst, locs2 = 1, [0], dict()
while n < terms:
an = alst[-1]
if an in locs2:
if len(locs2[an]) > 1:
i = min(locs2[an]); locs2[an].discard(i); anp1 = n - i
else: anp1 = 0
else: locs2[an] = set(); anp1 = 0
alst.append(anp1); locs2[an].add(n); n += 1
return alst
print(aupton(82)) # Michael S. Branicky, Apr 24 2021
CROSSREFS
KEYWORD
easy,nonn
AUTHOR
Allan Teitelman, Apr 19 2021
STATUS
approved