编写一个函数persistence,它接受一个正参数
n
并返回其乘法持久性,即必须将中的数字相乘的次数。
n个
直到你达到一位数。
例如:
我试过这段代码,但游戏规则是我不能传递两个参数。所以我必须消除
counter
争论。
def persistence(n,counter): #recursion
n = list(str(n))
product = 1
for i in n:
product *= int(i)
if product < 10:
return counter+1
else:
counter +=1
return persistence(product,counter)
persistence(39) # returns 3, because 3*9=27, 2*7=14, 1*4=4
# and 4 has only one digit