Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Recursive method

 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do determine the recursive formula?

I have the following code that I need to rewrite into a recursive method:



I understand this code, and I understand the theory of recursion. I even understand the factorial and Fibonacci recursion code. But I'm dealing with two sets of numbers here and I'm confused.

Steph
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds like homework of some kind.

Recursion works by reducing a problem to a simpler problem of the same kind. What you need to do is reduce intDiv(n1, n2) to intDiv(n1-n2, n2), and terminate as appropriate.

Is it the fact that two numbers are involved that confuses you? That should be familiar if you have programmed Fibonacci recursively. Another example you could study is Euclid's GCD algorithm.
[ January 01, 2006: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic