# Python program for OEIS A238334 # Michael S Branicky, Feb 23 2023 # A238334 Squares that do not contain a shorter substring that is a square. 4 data = [0, 1, 4, 9, 25, 36, 576, 676, 5776, 27556, 33856, 538756, 586756, 665856, 682276, 763876, 767376, 853776, 872356, 2637376, 2775556, 2835856, 5635876, 6885376, 7376656, 22886656, 23755876, 23775376, 26275876, 26687556, 26873856, 32672656, 32878756, 37527876] from time import time time0 = time() # (Python) from math import isqrt from itertools import count, islice def issquare(n): return isqrt(n)**2 == n def cond(s): if len(s) == 1: return True if any(d in s for d in "0149"): return False ss = (s[i:i+l] for i in range(len(s)) for l in range(2, len(s))) return not any(issquare(int(u)) for u in ss) def agen(): # generator of terms digset, valid = "0123456789", set("0123456789") for e in count(2): eset = set() newvalid = set() for tstr in valid: if len(tstr) == 1 or tstr[0] != "0": t = int(tstr) t2 = t**2 if cond(str(t2)): eset.add(t2) yield from sorted(eset) print("DONE", e-1, "digits", len(eset), len(valid), time()-time0) for tstr in valid: t = int(tstr) tset = set(tstr) for d in digset: dtstr = d + tstr dt = int(dtstr) remstr = str(dt**2)[-e:] if cond(remstr): newvalid.add(dtstr) assert len(dtstr) == e valid = newvalid if len(valid) == 0: return print(list(islice(agen(), 34))) # ~~~~ print(data) assert data == list(islice(agen(), 34)) g = agen() for n in range(1, 10001): an = next(g) if n%1000 == 0: print(n, an, time()-time0) with open("b238334.txt", "a") as bfile: bfile.write(f"{n} {an}\n")