0

I was reviewing the topic of recursion in a tutorial site and came across the problem and solution below:

Problem:

Given an integer, create a function which returns the sum of all the individual digits in that integer. For example: if n = 4321, return 10 since 4+3+2+1=10.

Solution:

def sum_func(n):
    if n<10:
        return n
    else:
        return n%10 + sum_func(int(n/10))
    pass

I understand the "if n<10" is the base case - if n is single digit, then just return n. What I couldn't wrap my head around is the "n%10 + sum_func(int(n/10))" line of code. Assume n is 235, n%10 will equal 5 and int(n/10) will equal 23. Then ultimately 5 + 23 will become 28 and not 10.

Could someone please help me understand what "n%10 + sum_func(int(n/10))" does? If you could walk through the logic one line at a time that would be greatly appreciated!

2
  • 2
    int(n/10) is 23, but you are not adding that. Instead, you are adding sum_func(int(n/10)), which is 5 (2+3). Commented Nov 6, 2019 at 3:51
  • 2
    n%10 gives last integer 1, 2 3 4 in this order. So try to get digits you need to slice n with int(n/10) this gives 432, 43, 4 so you got 1+2+3+4=10 Commented Nov 6, 2019 at 3:52

2 Answers 2

4

if n = 235 then int(n/10) is 23. However, you do not add 5 and 23. You add sum_func(int(n/10)) and 5.

sum_func(int(n/10)) is not int(n/10)

sum_func(int(n/10)) adds the digits in the number "23" together.

As such, sum_func(int(n/10)) == 2 + 3 == 5

sum_func(235) == sum_func(235)
              == sum_func(23)    + 5
              == sum_func(2) + 3 + 5
              == 2           + 3 + 5
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Samuel. Your answer was very helpful. I was careless in the beginning and missed the "function within function" and thought I just add 5 and 23 together. But it all makes sense now :)
2

As you say if there's only 1 digit return it.

The % is the modulus operator - i.e. the remainder when dividing by 10, or simply the last digit (i.e. 235%10 = 5)

int(n/10) drops the last digit since the int() function rounds down - i.e. 235 -> 23

Now what you seem to be confused by is within sum_func it calls sum_func again with the remainder once the last digit has been dropped i.e. 23 (this is the recursion part) which will then return 5.

i.e. you have

sum_func(235)
=5 + sum_func(23)
=5 + (3 + sum_func(2))
=5 + (3 + (2))
=10

2 Comments

Thank you! I indeed got confused with the sum_func within the sum_func. It's essentially a function within the function. I didn't catch that initially that's why I got confused.
yeah, that's exactly what recursion is - and if you have to be really careful that one of the branches returns something or you can get infinite recursion (well not really infinite, it'll crash eventually)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.