| Author |
Java direct Recursion
|
mark jacob
Greenhorn
Joined: Oct 07, 2009
Posts: 1
|
|
I want to convert this mutual recursion prog into direct recursion. Any Help how to do this
class Arec {
int a;
int b;
static long h(int k)
{ a++;
if (k == 0) return 0;
else return h(k - 1) + i(k - 1);
}
static long i(int k)
{ b++;
if (k == 0) return 1;
else return h(k) * i(k - 1);
}
public static void main(String argv[])
{ int k;
for (k = 0; k< 4; k++)
{ a = 0;
b = 0;
System.out.println("\n h("+k+") = "+h(k)+", i("+k+") = "+i(k));
}
}
}
|
 |
Ninad Kulkarni
Ranch Hand
Joined: Aug 31, 2007
Posts: 774
|
|
Hi Mark,
Welcome to JavaRanch
UseCodeTags
By using code tag your code will be easier to read.
|
SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Who inflicted that exercise on you?
Please tell us what you think you should do. In fact I think it is better with the two methods.
|
 |
 |
|
|
subject: Java direct Recursion
|
|
|