简体   繁体   中英

Getting wrong sum in program to condense a number to a single digit by adding the digits recursively in Python 2.7

Why am I getting the sum as 8 instead of 9 here, when I give the input as 12345678 ?

This is my code :

def single_digit(a):
    n=a
    sum=0
    while(n>0):
          r=n%10
          sum+=r
          n=n/10
          while sum>10:
               single_digit(a)
    return sum
a=input("\nEnter a number : ")
val=single_digit(a)
print"The number  condensed to a single digit = ",val

This would result in an infinite loop. The value of sum never changes during the execution of the inner while loop and hence the while sum > 10: loop never terminates, because once sum crosses 10 you are in no way modifying it. In other words, there is no way for the control to come out of loop once sum crosses 10.

I would write the function as follows

def single_digit(a):

    # Already in the simplest form. Nothing to do
    if a < 10:
        return a

    num = a
    sum_of_digits = 0

    # Compute sum of digits of the number
    while num > 0:
        remainder = num % 10
        sum_of_digits += remainder
        num = num // 10

    # Further condese the output to bring it to simple form i.e. single digit
    if sum_of_digits > 10:
        return single_digit(sum_of_digits)
    else:
        return sum_of_digits

Another clean and concise way to write the same code, in my opinion would be

def single_digit(a):
    sum_of_digits = sum(int(digit) for digit in str(a))
    if sum_of_digits < 10:
        return sum_of_digits
    else:
        return single_digit(sum_of_digits)
while sum>10:
   single_digit(a)

You are computing the sum of digits of a then checking if the sum is a single digit and if its not, you're computing the sum of digit of the original number a again, where you should computing for the new sum.

while sum>10:
   sum = single_digit(sum)

Also, if you want to go recursive, go full recursive:

def single_digit(a):
    # now it also works for negative numbers too
    if -10 < a < 10:  
        return a

    n=a
    sum=0
    while(n>0):
          r=n%10
          sum+=r
          n= n/10

    return single_digit(sum)

# a=input("\nEnter a number : ")
a = 12345678
val=single_digit(a)
print("The number  condensed to a single digit = ",val)

Naming variables is not easy, but using descriptive names makes life easier :)

def sum_of_digits(num):
    """ Recursively add the digits of a positive integer until the sum
    becomes a single digit. Return the sum. """

    if num < 10:    # base case
        return num

    num_copy = num
    result = 0

    while num_copy > 0:
          remainder = num_copy % 10
          result += remainder
          num_copy //= 10

    return sum_of_digits(result)

print(sum_of_digits(12345678))

It prints 9

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