# Problem with recursive method. Can you help?

• December 6th, 2011, 07:15 PM
TFLeGacY
Problem with recursive method. Can you help?
The point of my code is to find the answer of logbn. I am stuck on the part where a is incremented. I'm not exactly sure where to increment it because every value input will return 1. Any advice? Thanks!
So here is my code.

/**
* Calculates base-b log of n recursively
* @param b the base of the logarithm to be found
* @param n the value whose logarithm is being found
* @return base b log of n
* Pre-Condition: n must be integer multiple of b
* @since Lesson 6-4
* @version 12-6-11
* @author TFLeGacY
*/
public int log(int b, int n)
{ int a = 0;
if(n / b == 1)
return 1;
else
{ log(b, n = n / b);
a++;
}
return a;
}
• December 6th, 2011, 07:40 PM
Norm
Re: Problem with recursive method. Can you help?
One of your problems could be due to integer division. There are no fractions. 10/6 = 1
• December 6th, 2011, 07:42 PM
Freaky Chris
Re: Problem with recursive method. Can you help?
Another problem could be the lack of storing return values
• December 6th, 2011, 07:51 PM
Norm
Re: Problem with recursive method. Can you help?
The OP needs to post the algorithm that the code is supposed to implement.
• December 6th, 2011, 07:55 PM
TFLeGacY
Re: Problem with recursive method. Can you help?
Thanks so much for the help you guys! It got it working great! In case you were curious, here is the code that calculates the base-b log of n.

public int log(int b, int n)
{ if(n / b == 1)
return 1;
else
{ return 1 + log(b, n = n / b);
}
}
• December 6th, 2011, 07:57 PM
Freaky Chris
Re: Problem with recursive method. Can you help?
You should be aware that your solution whilst working has a couple of redundant features, I'd recommend having a think about what they might be if you want to excel in programming and problem solving :)

Chris
• December 7th, 2011, 04:44 PM
TFLeGacY
Re: Problem with recursive method. Can you help?
@Freaky Chris, the last post I put up was the exact answer for my homework. I do plan on excelling in programming and I just wanted to let you know that this code was simply an assignment.