简体   繁体   中英

Additive and multiplicative persistence in python


i am having some trouble with my code. I have to find the additive and multiplicative persistence of a number. So far I can find the persistence just one time, but it has to keep looping in order for the answer to be less than 9. Here are the results:

type a number thats greater than 9: 1234
additive persistence result:  10
multiplicative persistence result:  240
Press enter to exit

However, this is wrong because it should break down 10 and 240. It is supposed to do 1+0=1 and 2*4*0=0. I know i probably need a while loop to do this, it's just that i don't know how. This is for my CS class and even my teacher really didn't know how to do it. here is my code:

a=raw_input("type a number thats greater than 9: ")

sum_1=0

for element in a:
    sum_1+=int(element)
print "additive persistence result: ",sum_1


for element in a:
    sum_1*=int(element)
print "multiplicative persistence result: ",sum_1
print"Press enter to exit"
raw_input()

I'll get you started on the first one:

while len(a) > 1:
    sum_1 = 0
    for element in a:
        sum_1+=int(element)
    a = str(sum_1)

This is a good use case for the reduce() function:

def persistence(num, op):
    while True:
        digits = list(map(int, str(num)))
        if len(digits) == 1:
            return digits[0]
        num = reduce(op, digits)

You can call this with op being either operator.add or operator.mul :

>>> persistence(1234, operator.add)
1
>>> persistence(1234, operator.mul)
8

How about you try these code:

#Multiplicative Persistence

from functools import reduce  # omit on Python 2
import operator

#Q_num is used for asking the input of the number
Q_num = int(input('What is the input number: '))

#D_num is used to seperated the number from Q_num and put them in the list
D_num = ([int(A_num)for A_num in str(Q_num)])
print(D_num)

#Multiply is used to convert function to D_num(numbers that already being seperated)
Multiply = reduce(operator.__mul__, D_num)
print(Multiply)

#D_mulF is used to seperated the number from Multiply and put them in the list
D_mulF = ([int(B_num)for B_num in str(Multiply)])
print(D_mulF)

#Multiplier is used to convert function to D_mulF(numbers that already being seperated)
Multiplier = reduce(operator.__mul__, D_mulF)
print(Multiplier)


#Additive Persistence
#Q_num is used for asking the input of the number
Q_num = int(input('What is the input number: '))

#D_num is used to seperated the number from Q_num and put them in the list
D_num = ([int(A_num)for A_num in str(Q_num)])
print(D_num)

#D_sum is used to convert the sum() function to D_num(numbers that already being seperated)
D_sum = (sum(D_num))  # Add all of the number in the list
print(D_sum)

#Answer is used to seperated the number from D_sum and put them in the list
Answer = ([int(A_num2)for A_num2 in str(D_sum)])
print(Answer)

#F_ans is used to convert function to Answer(numbers that already being seperated)
F_ans = (sum(Answer))
print(F_ans)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM