Take the 2-minute tour ×

I need to write a code that counts the sum of the digits of a number, these is the exact text of the problem:The digital sum of a number n is the sum of its digits. Write a recursive function digitalSum(n) that takes a positive integer n and returns its digital sum. For example, digitalSum(2019) should return 12 because 2+0+1+9=12. These is the code I wrote :

def digitalSum(n):
   if n < 10:
      return n
      for i in S:
      return sum(L)

These code works fine, but it's not a recursive function, and I'm not allowed to change any int to str. May you help me?

share|improve this question
add comment

7 Answers

up vote 7 down vote accepted

Try this:

def digitalSum(n):
    if n < 10 :
        return n
    return n % 10 + digitalSum( n // 10 )

Edit: The logic behind this algorithm is that for every call of the recursive function, we chop off the number's last digit and add it to the sum. First we obtain the last digit with n % 10 and then we call the function again, passing the number with the last digit truncated: n // 10. We only stop when we reach a one-digit number. After we stop, the sum of the digits is computed in reverse order, as the recursive calls return.

Example for the number 12345 :

5 + digitalSum( 1234 )
5 + 4 + digitalSum( 123 )
5 + 4 + 3 + digitalSum( 12 )
5 + 4 + 3 + 2 + 1 <- done recursing
5 + 4 + 3 + 3
5 + 4 + 6
5 + 10
share|improve this answer
Posting a code snippet with no explanations is not the best way to answer homework questions. Consider reading meta.stackexchange.com/questions/10811/… –  gdbdmdb Aug 24 '12 at 10:03
I will try to be more helpful in the future, though with such a small piece of code there is little to be said. –  Grampa Aug 24 '12 at 10:07
Thank you a lot! Now I understand. –  Reginald Aug 24 '12 at 12:28
add comment

It's homework, so I'm not writing much code. Recursion can be used in the following way:

  • get the first (or last) digit
  • format the rest as a shorter number
  • add the digit and the digital sum of the shorter number (recursion!)
share|improve this answer
add comment

This is more of an algorithms question.

Here is your answer:

def digit_sum(a):

    if a== 0:
        return 0

    return a % 10 + digit_sum(a/10)

Let me know if you don't understand why it works and I'll provide an explanation.

share|improve this answer
Uptadet it to python 3, and it worked, but I don't understand what is digitalSum(a//10) ,it seems digitalSum(n//10) != n//10, so wath is digitalSum()? –  Reginald Aug 24 '12 at 10:15
add comment

Some hints:

  1. You can define inner functions in Python
  2. You can use the modulus operator (look up its syntax and usage) to good effect, here
  3. There's no need to build up an explicit list representation with a proper recursive solution

EDIT The above is a bit "bad" as a general answer, what if someone else has this problem in a non-homework context? Then Stack Overflow fails ...

So, here's how I would implement it, and you need to decide whether or not you should continue reading. :)

def digitalSum(n):
  def process(n, sum):
    if n < 10:
      return sum + n
    return process(n / 10, sum + n % 10)
  return process(n, 0)

This might be a bit too much, but even in a learning situation having access to one answer can be instructive.

My solution is more a verbose than some, but it's also more friendly towards a tail call optimizing compiler, which I think is a feature.

share|improve this answer
I consider my solution to be more elegant ;) –  Mike Vella Aug 24 '12 at 9:57
add comment
def digitalSum(n):
   if n < 10:
      return n
      return ???

The 1st part is from your existing code. The ??? is the part you need to work out. It could take one digit off n and add it to the digitalSum of the remaining digits.

You don't really need the else, but I left it there so the code structure looks the same

share|improve this answer
add comment
def digital_sum(number):
    if number < 10: 
        return number
        return number % 10 + digital_sum(number / 10) 
share|improve this answer
add comment

def drs_f(p):

 drs = sum([int (q) for q in str(p)])
 while drs >= 10:
     drs = sum([int(q) for q in str(drs)])
return drs
share|improve this answer
add comment

Your Answer


