0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5, 3, 0, 3, â¦
That is the Van Eckâs Series.
Letâs undergo it step-by-step.
Time period 1: The primary time period is 0.
Time period 2: Since we havenât observed 0 ahead of, the second one time period is 0.
Time period 3: Since we had observed a nil ahead of, one step again, the 3rd time period is 1
Time period 4: Since we havenât observed a 1 ahead of, the fourth time period is 0
Time period 5: Since we had observed a nil ahead of, two steps again, the 5th time period is two.
And so forth…
Your activity is to search out the n_th quantity in Van Eckâs Series. (1-based)
The Answer in Python
Choice 1
from collections import Counter
c=Counter()
SEQ = [0]
for i in vary(1000):
n = SEQ[-1]
if no longer c[n]: c[n]=i
SEQ.append(i-c[n])
c[n]=i
seq=SEQ.__getitem__
Choice 2
def dist(arr):
for i in vary (1, len(arr)):
if arr[-1-i] == arr[-1]:
go back i
go back 0
def seq(n):
s = [0, 0]
for _ in vary (n):
s.append(dist(s))
go back s[n-1]
def seq(n):
van, eck = [0], 0
whilst n := n - 1:
van.insert(0, eck := van.index(eck, 1) if eck in van[1:] else 0)
go back eck
Check instances to validate the answer
from answer import seq
import check
from random import randint
@check.describe("Pattern assessments:")
def assessments():
@check.it("Small numbers")
def _():
s = [0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6]
for i in vary (len(s)):
check.assert_equals(seq(i+1), s[i])
@check.it('Greater numbers')
def __():
s = [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, 46, 0, 6, 23, 0]
for i in vary (len(s)):
check.assert_equals(seq(i+50), s[i])
@check.describe('Random assessments:')
def r():
def dist(arr):
for i in vary (1, len(arr)):
if arr[-1-i] == arr[-1]:
go back i
go back 0
def ref_sol(n):
s = [0, 0]
for _ in vary (n):
s.append(dist(s))
go back s[n-1]
@check.it('200 random assessments:')
def _():
for _ in vary (200):
a = randint(100, 1000)
exp = ref_sol(a)
check.assert_equals(seq(a), exp)