Solved

sumDigits  challenge

Posted on 2016-09-09
7
75 Views
Last Modified: 2016-09-11
Hi,

http://codingbat.com/prob/p163932

I am working on above challenge
Given a non-negative int n, return the sum of its digits recursively (no loops). Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).

sumDigits(126) → 9
sumDigits(49) → 13
sumDigits(12) → 3

i was not sure how to find the sum of digits recursively without loop which i worked earlier. please advise
0
Comment
Question by:gudii9
  • 3
  • 2
  • 2
7 Comments
 
LVL 27

Expert Comment

by:d-glitch
ID: 41792111
Do you remember how to break up an integer into the LAST digit and the REST using % and / .

Recursion works by letting a function call itself multiple times, each time with a smaller or simpler argument.

The pseudo code would be something like:
function sumDigits( n)
sum = 0
if arg = 0 
     return sum
else
     sum = sum + LAST( arg) + sumDigits( REST( arg))

Open in new window


RECURSION is probably the single most important and elegant topic in computer science.
You should really read up on it if you don't understand the concept.  Challenges are not going to be enough.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41792185
Do you remember how to break up an integer into the LAST digit and the REST using % and / .
yes
RECURSION is probably the single most important and elegant topic in computer science.
You should really read up on it if you don't understand the concept.  Challenges are not going to be enough.
where should i read up on it? Any best material,book, site?
0
 
LVL 27

Accepted Solution

by:
d-glitch earned 250 total points
ID: 41792669
Here are a couple of references:
     http://introcs.cs.princeton.edu/java/23recursion/
     http://arxiv.org/pdf/cs/9301113


My pseudo code needs some mods:
function sumDigits( n)
if n = 1   
     return 1
else   
     return  LAST + sumDigits( REST)

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 27

Assisted Solution

by:rrz
rrz earned 250 total points
ID: 41793107
My pseudo code is a little different from d-glitch
if input is 0 then return 0
if not then return last + sumDigits(rest)
0
 
LVL 7

Author Comment

by:gudii9
ID: 41793534
I will try
0
 
LVL 7

Author Comment

by:gudii9
ID: 41793651
public int sumDigits(int n) {
  if (n < 10) 
{return n;
}
  return (n % 10) + sumDigits(n/10);
}

Open in new window


above passes all test. any improvements or alternate approaches?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41793722
 if (n < 10)
{return n;
Brilliant! I like your base case much better.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
This is an explanation of a simple data model to help parse a JSON feed
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now