Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Recursion

 
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);
}
 
author
Posts: 23877
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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.
 
Message for you sir! I think it is a tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic