Skip to content

Instantly share code, notes, and snippets.

@HaraldKorneliussen
Created January 11, 2021 10:43
Show Gist options
  • Select an option

  • Save HaraldKorneliussen/320b70acefc30240f6dc0dc76377b9f2 to your computer and use it in GitHub Desktop.

Select an option

Save HaraldKorneliussen/320b70acefc30240f6dc0dc76377b9f2 to your computer and use it in GitHub Desktop.
Program to generate the b file for A339608
#!/usr/bin/python
# Encode a natural number (incl 0) as a bijective number (string)
def bijEnc(num, symbols="ab"):
ret = ''
slen = len(symbols)
while num > 0:
num -= 1
ret = symbols[num % slen] + ret
num = num // slen
return ret
# Decode a bijective number (string) as a regular natural number
def bijDec(s, symbols="ab"):
acc = 0
for c in s:
acc *= len(symbols)
acc += (symbols.index(c) + 1)
return acc
# Factor a string into its Lyndon words
def lyndonFactor(s):
k = 0
ret = []
while k < len(s):
i = k
j = i + 1
while j < len(s) and s[i] <= s[j]:
if s[i] == s[j]:
i += 1
else:
i = k
j += 1
oldk = k
k = k + j - i
ret.append(s[oldk:k])
return ret
line_num=1
n = 1
while line_num <= 20000:
factors = lyndonFactor(bijEnc(n))
if len(factors) == 1:
print(line_num, bijDec(factors[0]))
line_num += 1
n+=1
@HaraldKorneliussen
Copy link
Author

Run in python3 to generate the exact file (it kind of works in Python 2, but requires reformatting to be a proper B file).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment