Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
792 views
in Technique[技术] by (71.8m points)

python - multiplicative digital root of a number using loops

I need to find the multiplicative digital root of a number in python using only loops.

something that does the same as the below code but using loops:

print("multiplicative digital root of a number Calculator")
print("-"*50)

num = input("Enter a number: ")

def droot(num):
    if len(num) == 1:
        return num
    else:
        sum = 0
        for i in num:
            sum += int(i)
        num = str(sum)
        return droot(num)

print("The digital root of ", num, " is: ", droot(num))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Basically, you're looking for something like this:

number = 9876; #Example
divider = 1;
digits = []
answer = -1
while true:
    value = 1
    while number % divider != 0: #Fills list with digits
        digits.append(number % divider)
        divider = divider * 10
    while list.len() != 0: # Empty list for next iteration
        value = value * list[0]
        list.remove(0) # The elements will always be 0, as the first one is constantly removed
    number = value # Change for next iteration
    if value == value % 10: # If there's only a single digit left, you've got it
        answer = value # Finish up, close
        break

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...