Reputation: 69
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)
Upvotes: 4
Views: 645
Reputation: 9213
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
Upvotes: 0
Reputation: 9213
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
Upvotes: 0
Reputation: 1078
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)
print(sum_of_digits(16)) # Output: 7
print(sum_of_digits(942)) # Output: 6
print(sum_of_digits(6784)) # Output: 5
Upvotes: 0
Reputation: 23129
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
Upvotes: 6
Reputation: 137
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.
Upvotes: 0
Reputation: 27211
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
Upvotes: 1
Reputation: 107015
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
Upvotes: 1
Reputation: 27770
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
Upvotes: 6
Reputation: 142
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
Upvotes: 0