java - Recursive method to calculate log -
i did following calculate recursive logarithm:(b base of log here)
int log(int b, int n ) { if (n/b ==1) { return 1; } else { return log( b, n/b)+1 ; } }
however, it's wrong.i'm doing opendsa interactive platform.the original question following:
function "log", write missing base case condition , recursive call. function computes log of "n" base "b". example: log 8 base 2 equals 3 since 8 = 2*2*2. can find dividing 8 2 until reach 1, , count number of divisions made. should assume "n" "b" integer power. 1 int log(int b, int n ) { 2 if <<missing base case condition>> { 3 return 1; 4 } else { 5 return <<missing recursive case action>> 6 } 7 }
my code incorrect.i'm getting infinite recursion.
if format must this:
int log(int b, int n ) { if <<enter base case>> { return 1; } else { return <<enter action case>> ; } }
then safest method (that can come with) be:
int log(int b, int n ) { if (n <= b) { return 1; } else { return log(b, n/b)+1 ; } }
however round nearest positive number.