Skip to content

Instantly share code, notes, and snippets.

@giuliano0
Created March 28, 2019 17:49
Show Gist options
  • Save giuliano0/be8c3d60aa66f6cfcf727758097358fb to your computer and use it in GitHub Desktop.
Save giuliano0/be8c3d60aa66f6cfcf727758097358fb to your computer and use it in GitHub Desktop.
My take on Multiplicative Persistence, inspired by this Numberphile video: https://www.youtube.com/watch?v=Wim9WJeDTHQ
from functools import reduce
def per(n):
k = 0 # k is the persistence
def per_rec(n, k):
digits = [int(c) for c in str(n)]
if len(digits) == 1:
print('persistence ' + str(k))
# with functools
new_n = reduce(lambda x, y: x * y, digits)
# without functools, if you will
#new_n = 1
#for x in digits:
# new_n *= x
print('%s = %d' % (' * '.join([d for d in str(n)]), new_n))
per_rec(new_n, k + 1)
per_rec(n, k)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment