• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursion  RSS feed

 
Shane McKenzie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Man, does recursion get on my nerves. :-) Here's my problem, and I have literally no idea where to even start, or I'd post a code snippet. I'm just about to take a final exam in a programming class, and I've had no trouble with anything to this point, except recursion. It drives me mad. What I need to do is create a method called remainder(int a, int b), that calculates the remainder (a % b) WITHOUT using the % operator. I pointed out the presence of the static methjod Math.pow() to my professor, and he was nonplussed. :-| Can anyone point me in the right direction???

shane
 
Darryl Nortje
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shane

You said
--> What I need to do is create a method called remainder(int a, int b),
--> that calculates the remainder (a % b) WITHOUT using the % operator.

Do you have to recursion?? 'cos this method is possible with simple maths.
a = 10
b = 3

a/b = 3
a/b * b = 9
a - (a/b * b) = 1

bob's your uncle.

Or must you use recursion???

cheers
Darryl
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The long way to do division is subtraction. What if the method remainder(a,b) said if a < b then return a, else do some subtraction and call itself. (Deliberately vague suggestion.)

Of course you can do this more efficiently in a loop, but if the objective is to play with recursion, then recurse (is that a verb?) with a smile.
 
jin sun
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^ Yea I think he's being tested on recursion, therefore he needs to show a recursion example to prove he understands the concept, thus efficiency is irrevelant for this situation.
[ July 25, 2005: Message edited by: jin sun ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!