I am using the code below, its basically babylonian method, how can i modify it so that it returns correct value for numbers that are b/w 0 and 1. moreover can anyone tell me how can i find cubic root of a number
int main(){
double n;
cin>>n;
double e= 0.0000001;
double x = n;
double r = 1;
while(x-r >0.0000001){
x = (x+r)/2;
r = n/x;
}
printf("%f",x);
return 0;
}
I. Don’t use the difference, but its absolut value. E.g.,
II. Use the following iteration for y=x^(1/n): y’=((n-1)*y^n + x)/(n*y^(n-1))
EDIT
II. describes the iteration step for a n-th root of x. For the cube root, use
y’ = (2y^3 +x)/(3y^2)
The approach bases on Newton’s method, see http://en.wikipedia.org/wiki/Newton%27s_method