Last active
December 15, 2020 07:03
-
-
Save MattJDavidson/6259bba9e330bc71613ffabb9ccd5dfd to your computer and use it in GitHub Desktop.
Peter prime
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
#!/usr/bin/env python | |
import math | |
import os | |
import sys | |
from io import BytesIO, IOBase | |
def peter_prime(n): | |
sum_so_far = 0 | |
for i, digit in enumerate((n // (10 ** i)) % 10 for i in range(math.ceil(math.log(n, 10)) - 1, -1, -1)): | |
if sum_so_far - n > 1: # exit early if we're already more than 1 over n | |
if i < math.log10(n)+1: # are we near the end of the digits for this n? | |
return | |
else: | |
break | |
sum_so_far += digit ** digit | |
# check for off by one for any solution | |
if abs(sum_so_far - n) <= 1: | |
print(f"calculated {sum_so_far} for original number {n}") | |
return | |
if __name__ == "__main__": | |
for n in range(3435, 10 ** 9): | |
peter_prime(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment