Created
June 19, 2019 07:49
-
-
Save robinhouston/d1a56ae3f62f9f81e226c5eb3055fd8e to your computer and use it in GitHub Desktop.
Sage code to find left-truncatable primes in bijective numeration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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