programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Tim Cooke
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Paul Clapham
• Rob Spoor
• Junilu Lacar
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Piet Souris
• Carey Brown
Bartenders:

# Recursion

Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
Hey guys!! I just reached the recursion section of the coding bat website! Yay! I don't recall every encountering anything like this before so it's taking me a bit to really grasp it. Anyway ... in the screen shot below ... I actually looked up the answer. But when I go through it I don't understand why it works. I DO understand the basics of recursion but I'll explain my confusion.

So in this example ... if n were 717 like the first example I understand the first time it checks it picks up that end 7 and then n is 71. The next round through it just divides by 10 since the remainder isn't 7. Then the number is just 7. So my confusion is ... how does it get that final 7? 7/10 doesn't give a remainder of 7 so how does it know to count that? So obviously I am missing something here.

Thanks!!

Screen shot won't work so I am just copying and pasting the problem and solution.

Given a non-negative int n, return the count of the occurrences of 7 as a digit, so for example 717 yields 2. (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).

public int count7(int n) {
if (n==0) return 0;

if (n%10 == 7) return 1 + count7(n/10);

return count7(n/10);
}

author
Posts: 23931
142
• Number of slices to send:
Optional 'thank-you' note:

Danielle Rutter wrote:how does it get that final 7? 7/10 doesn't give a remainder of 7 so how does it know to count that? So obviously I am missing something here.

The "7/10" is not the test. It is the "7 % 10 == 7" that is the test. The test is passed, and the 7/10 equals zero which is sent to the next recursion call. This zero causes the recursion to stop, and the whole thing is unrolled.

Henry

Sheriff
Posts: 7123
184
• Number of slices to send:
Optional 'thank-you' note:

Screen shot won't work so I am just copying and pasting the problem and solution.

That is preferred over screen shots.

Also, remember to UseCodeTags (that's a link).

Danielle Rutter
Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:

Henry Wong wrote:

The "7/10" is not the test. It is the "7 % 10 == 7" that is the test. The test is passed, and the 7/10 equals zero which is sent to the next recursion call. This zero causes the recursion to stop, and the whole thing is unrolled.

I mis-typed this. 7%10==7 ... the remainder of 7/10 is 7. But it's not! So the test isn't passed! That's why I'm confused. Obviously something very obvious just isn't clicking. It's always the easiest stuff that I have the hardest time understanding. :P

Knute Snortum
Sheriff
Posts: 7123
184
• Number of slices to send:
Optional 'thank-you' note:

7%10==7 ... the remainder of 7/10 is 7. But it's not! So the test isn't passed!

Huh? 7 % 10 does equal 7.

Danielle Rutter
Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:

Knute Snortum wrote:
Huh? 7 % 10 does equal 7.

Yeah ... please refer to the last part where I said I miss obvious things ... and have trouble understanding the easiest stuff. I swear I'm really not a moron. :P

Knute Snortum
Sheriff
Posts: 7123
184
• Number of slices to send:
Optional 'thank-you' note:

Yeah ... please refer to the last part where I said I miss obvious things

Got it. Sorry.