This is my code of a recursive function written in an non-final form, I would like to pass it to iterative with a while, but I really can not do it, advice to do it with bottom-up? The abc method is a square root (x), and the number of iterations performed (n), the higher the iteration the better the result.
public static Double abc(int x,int n){
Double d = .0;
if(n==0){ //caso base
d = 1.0;
}else{
d = (abc(x,n-1)+(x/abc(x,n-1)))/2;
}
return d;
}
This is what I tried with the while loop:
public static Double abcIter(Integer x,Integer n){
Double res = 1.0;
Double r0=.0;
Integer numeroAuxiliar = 0;
while(numeroAuxiliar<=n){
res = (res+(x/res))/2;
r0 = res;
res = (r0+(x/r0))/2;
numeroAuxiliar++;
}
return res;
}
When I try some root and iteration with the while it does not give me the exact value. On the other hand, the first method if that works for me. Advice on the while method, and bottom-up?