I want to make a recursive function that sums all the digits in a number

19 views (last 30 days)
So this is the actual question
Write a recursive function that computes the sum of the digits provided as an input argumenet. No need to check the input. Not allowed to use loops or string conversion like str2num. e.g. a number 12345 = 1+2+3+4+5 = 15. There is a hint: consider a mathematical operaion how to get the last digit of a base 10 number.
So far my plan is basically take the last digit of the input 12345
last digit =5
takeaway this value from the orignal number
12345-5=12340
divide by 10
12340/10=1234
add the last digit to the output
output=last digit + prev output
and recall the function till input = 0 at which point output the output
Below is what I currently have and being my first recursion problem quite holey. I am not sure of the base case but also if I define output as 0 then everytime I call the function the output will be 0, so I don't really know how to solve this
function [output]=digit_sum(input)
a = mod(input,10)
output=a+output
input=input-a
input=input/10
if input==0
output=output
return
else
[output]=digit_sum(input)
end
end

Accepted Answer

John D'Errico
John D'Errico on 21 Oct 2020
Edited: John D'Errico on 21 Oct 2020
The "last" digit? A little ambiguously stated. In the number 12345, is 5 the last digit you are asking about?
It looks like you already know that you can divide by 10. What happens to that last digit then? Could you find the greatest integer that does not exceed the result? How would that help you? Do you know of a function that can convert a floating point number to an integer, still in a double form? (Yes, I suppose you could also decide to cast a number to an integer class, then back into a double. That would work too.)
There are other tools you might consider too.
Perhaps the best way to learn about them would be to do this in MATLAB:
help elfun
This will give you the first line of the help blocks of every function in MATLAB in the directory of elementary mathematical functions. So any of them seem like they would be helpful?
You can find the solution yourself, and if you do, you will be much farther ahead than if I tell it to you on platter. So take a look at the results of the help command I suggested. This will also teach you a way to learn other things in MATLAB. Do some exploring. I promise the solution can be found in elfun. (It is not just elves they hide in there.)
Edit: The problem has moved forward a bit. The question is now how to step foward.
My first comment is digit_sum must know when to terminate the recursion. The step forward will actually begin with knowing when to stop the recursion. Every recursive function typically has a test in the beginning, testing to see if it is done.
The logic could look like this:
function outsum = digit_sum(innum)
if innum == 0
% this is the case where it can quit
outsum = 0;
else
% we need to work more deeply here
% compute a, using mod
a = ...
% you already know how to modify the input
% subtract off a from innum, then divide by 10
innum = ...
outsum = a + digit_sum(innum);
end
I've not done the actual work there, just giving you the flow. You already know the rest.
digit_sum cannot escape the recursion until it gets to the end, until the input number has nothing left to it. At that point, the recursion folds back up, one level after another.
Take a shot at that. I think you will have working code now pretty quickly. Ask again if you get closer, but still feel stuck.
  6 Comments
katherine keogh
katherine keogh on 21 Oct 2020
I got this one too! Your clue on the other post complety unlocked it for me! Here was my solution, I'm sure someone as clever as you can solve it in basically no lines BUT I UNDERSTAND IT NOW!
function [output]=digit_sum(input)
if input==0
output=0
else
a = mod(input,10)
input=input-a
input=input/10
w=[a,digit_sum(input)]
output=sum(w)
end
end

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!