Created
March 28, 2019 17:49
-
-
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
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
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