Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Strilanc/6b1313cb793b98594b3dcd43c06c48dc to your computer and use it in GitHub Desktop.
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.
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