# Python program for OEIS A358538 # Michael S. Branicky, Nov 21 2022 # Modified Dec 03, 2022 from sympy import isprime from itertools import count, islice from sympy.utilities.iterables import partitions, multiset_permutations def okstr(lst): M = min(10, len(lst)) return all(lst.count(str(i)) == int(lst[i]) for i in range(M)) def partialokstr(lst): M = min(10, len(lst)) partialok, s = True, "" for i in range(M): missing = int(lst[i]) - lst.count(str(i)) if missing < 0: return False, "" else: s += missing*str(i) return True, s def agen(): for d in count(1): okset = set() for p in partitions(d, k=9, m=10): sp = "".join(str(k)*v for k, v in p.items()) zeros_needed = min(d, 10) - len(sp) if zeros_needed < 0 or zeros_needed > 9: continue s = sp + "0"*zeros_needed for m in multiset_permutations(s): if m[0] != "0": passes, needs = partialokstr(m) if passes: if d <= 10: if okstr(m): okset.add(int("".join(m))) else: for m2 in multiset_permutations(needs): if okstr(m+m2): okset.add(int("".join(m+m2))) yield from sorted(okset) alst = [] g = agen() for n in range(1, 10001): an = next(g) alst.append(an) with open('b358538.txt', 'a') as bfile: bfile.write(f"{n} {an}\n")