How to sum digits of a number untill there is one

310 views Asked by At

I want to solve this but I have no idea how to

I´d appreciate your help

given n take the sum of the digits of n if that value has more than one digit continue until there´s only one

Expected output:

16 -> 1 + 6 = 7
942 -> 9 + 4 + 2 = 15 -> 1 + 5 = 6

I tried this but I don´t know how to repeat it untill there is only one digit

Def sum_digit(n):
 list_of_digits = list(map(int,str(n)))

su = []
for x in list_of_digits:
x = sum(list_of_digits)
su = x

print(su)

sum_digit(6784)
9

There are 9 answers

0
darshita_baldha On

Use a recursive function to sum the digits of a number until there's only one digit left.

def sum_digits(n):

   list_of_digits = list(map(int, str(n)))

   digit_sum = sum(list_of_digits)

   # If the sum has more than one digit, call the function recursively

   if digit_sum >= 10:
     return sum_digits(digit_sum)
   else:
     return digit_sum
1
Talha Tayyab On

You can use a while loop to repeat until the number reduce to a single digit.

def sum_digit(n):
    while n > 9:
        n = sum(int(i) for i in str(n))
    return n

sum_digit(16)
#7

sum_digit(942)
#6
0
blhsing On

You can pass the sum of digits of the current number to a recursive call until it is a single-digit number:

def sum_digit(n):
    return sum_digit(sum(map(int, str(n)))) if n > 9 else n

so that:

print(sum_digit(16))
print(sum_digit(942))

outputs:

7
6
0
SIGHUP On

Converting the value to a string and enumerating the digits (as suggested in other answers) is effective but slow. You can do it purely arithmetically as follows:

def sum_digit(n: int) -> int:
    while (_n := n) > 9:
        n = 0
        while _n > 0:
            n += _n % 10
            _n //= 10
    return _n
9
Sandipan Dey On

From this result, every integer is congruent to the sum of its digits mod 9.

Proof is easy:

n ≡ sum_{k=0}^{m} 10^k d_k (mod 9) ≡ sum_{k=0}^{m} (9+1)^k d_k (mod 9) ≡ sum_{k=0}^{m} d_k (mod 9), when m = number of digits in n - 1

Hence simply compute n % 9 to find sum of the digits of n until one digit, without any loop / recursion.

def sum_digits(n): # assumes n > 0, otherwise n = 0 is trivial
    # assert(n > 0)
    return (n-1) % 9 + 1                # 1. this will work
    # return n % 9 if n % 9 else 9      # 2. this will also work
0
Muhammad Shamshad Aslam On

Here is my solution:

Make sure you reinitialize the the sum = 0 within the keep_adding function to make it forget the previously calculated sum.

digit = 9999999999999999999999999888
def check_length(x):
    if len(str(x))>1:
        return True

def keep_adding(digit):
    sums=0
    for i in str(digit):
        sums+=int(i)
    return sums

while check_length(digit):
    digit=keep_adding(digit)

print(digit)

I tried it on a few different values of digit and it seems to be working as expected.

1
Tusher On

You can use recursion for efficiency.

Here is my solution using recursion:

def sum_of_digits(n):
    # Base case: if n is a single digit, return it
    if n < 10:
        return n
    
    
    digits = [int(digit) for digit in str(n)]
    
    # Calculate the sum of the digits
    total_sum = sum(digits)
    
    
    return sum_of_digits(total_sum)

And the output

print(sum_of_digits(16))  # Output: 7
print(sum_of_digits(942)) # Output: 6
print(sum_of_digits(6784)) # Output: 5
0
XMehdi01 On

You can utilize a while loop to continue the summation process until the number is reduced to a single digit

def sum_digits_until_single_digit(num):
    while num >= 10:
        total = 0
        for digit in str(num): 
            total += int(digit)
        num = total
    return num
0
XMehdi01 On

Another approach using Recursion:

def digital_root(n):
    total = 0
    while n > 0:
        digit = n % 10
        total += digit
        n //= 10
    if total > 9:
        return digital_root(total)
    else:
        return total
print(digital_root(942)) # 6