Created
March 22, 2019 05:54
-
-
Save Strilanc/6b1313cb793b98594b3dcd43c06c48dc to your computer and use it in GitHub Desktop.
Code to search for numbers with large persistence of multiplication. Finds the largest known ones in under a second.
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
def persistence(n): | |
t = 0 | |
while n >= 10: | |
t += 1 | |
n2 = 1 | |
for d in str(n): | |
n2 *= int(d) | |
n = n2 | |
return t | |
best = 5 | |
for total in range(20000): | |
for a in [False, True]: | |
for b in range(total): | |
if a: | |
k2 = b | |
k5 = 0 | |
else: | |
k2 = 0 | |
k5 = b | |
for k3 in range(total - b): | |
k7 = total - k2 - k3 - k5 | |
n = 2**k2 * 3**k3 * 5**k5 * 7**k7 | |
s = persistence(n) | |
if s >= best: | |
m = int('2'*k2 + '3'*k3 + '5'*k5 + '7'*k7) | |
print(persistence(m), m, 'from 2**{} * 3**{} * 5**{} * 7**{}'.format(k2, k3, k5, k7)) | |
best = s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment