Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursion  RSS feed

 
Danielle Rutter
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah ... please refer to the last part where I said I miss obvious things

Got it. Sorry.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!