Skip to content

Instantly share code, notes, and snippets.

@robinhouston
Created June 19, 2019 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinhouston/d1a56ae3f62f9f81e226c5eb3055fd8e to your computer and use it in GitHub Desktop.
Save robinhouston/d1a56ae3f62f9f81e226c5eb3055fd8e to your computer and use it in GitHub Desktop.
Sage code to find left-truncatable primes in bijective numeration
DIGITS = "123456789X"
ENCODE = dict(( (i+1, d) for (i, d) in enumerate(DIGITS) ))
DECODE = dict(( (d, i+1) for (i, d) in enumerate(DIGITS) ))
def encode(n):
a = []
while n > 0:
n, r = divmod(n, 10)
if r == 0: n, r = n - 1, 10
a.append(ENCODE[r])
return "".join(reversed(a))
def decode(s):
return reduce(lambda n, c: 10*n + DECODE[c], s, 0)
def search(s):
if not is_prime(decode(s)): return
print(s)
for digit in DIGITS:
search(digit + s)
search("3")
search("7")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment