Code:
float logarithm(float m, float n, float x, float y, float c)
{
float ans,g,a;
g=sqrt(m*n);
a=(x+y)/2;
if (g<c)
{
m=g;
x=a;
}
else if (g>c)
{
n=g;
y=a;
}
while ((fabs(m-n))>0.01)
{
return (logarithm(m, n, x, y, c));
}
return a;
}
This is sort of recursive, but you're making it more complicated then it has to be, and thus losing much of the elegance of the recursive solution :
Code:
float logarithm(float m, float n, float x, float y, float c)
{
float ans,g,a;
/* Add this part to eliminate the while loop */
if((fabs(m-n))>0.01)
{
return a;
}
g=sqrt(m*n);
a=(x+y)/2;
if (g<c)
{
/* Don't do this
m=g;
x=a;
*/
/* Use this instead */
logarithm(g,n,a,y,c);
}
else if (g>c)
{
/* Same as above
n=g;
y=a;
*/
logarithm(m,g,x,a,c);
}
/* Now this loop is useless
while ((fabs(m-n))>0.01)
{
return (logarithm(m, n, x, y, c));
}
return a;
*/
}