I was doing recursion exercises and I came across this one. I don't really understand how to make the passes count, since every time I call the method, the value of the "count" variable is saved and gives a wrong number.
Write a recursive class method that, given two natural numbers a ≥ 0 and b > 0, calculates the quotient of their integer division, based on the fact that said operation can be performed as a series of successive subtractions (similar to the problem from computing the remainder of integer division in Section 10.4), following the recurrence: a/b = 0, if a<b
a/b = (a − b)/b + 1, if a ≥ b
This is the code I have written:
static int cont = 0;
public static int division(int a, int b){
if(a < b){
cont++;
return 0;}
else{
division( (a-b) / b + 1, b);
cont++;
}
return cont;
}
only when (a>=b) the counter should increment,
a/b = (a − b)/b + 1, si a ≥ b
this applies if you wanted to return the result same methodbut since you are wanting to return the counter as a quotient, it would be like this