0
iteration to recursive
I have problems converting iteration to recursive function this is the loop to convert: https://code.sololearn.com/c6DOR5Btc93q/#cpp this is what i have tried(it doesnt work): double y (double x){ int n=256; double o; double a; if(n>=2){ n=n/2; o=x*x+(n/(x*x)); a=x*x+(n/o); o=a; y(x); } else return 1/o; }
15 Answers
+ 1
A S I thought you got an answer in your previous post itself, didn't you?
+ 1
A S so are you approaching from top to bottom or bottom to top?
+ 1
A S if you consider your x=16 then your y is coming out something around 0.003906 and this is just an approximation. It will vary for different values of x. Just wanted to let you know because I don't think you would have solved it on paper.
+ 1
https://code.sololearn.com/c98i3E3327mz/?ref=app
I altered your code cause i think you make somthing wrong
And then i made it the way i would do the recursion
+ 1
In the first recursion call z =1
So 1/(x*x + recursion (x*x, 2)) is returned. So with all the recursions the whole arithmetic expression is build up. z << 1 is the binary left shift with 1 which is equal to z * 2 because 2^1=2.
x*x is always the same so i save it in xpow.
0
@Avinesh i figured out how to do it with loop and now i need to convert to recursive :)
0
@Avinesh from bottom to top
0
double recursive(double x, double o, double n){
if(n<=1) return 1/o;
else {
o=x*x+(n/(x*x));
o=x*x+(n/o)
return recursive(x,o,n/2);
}
}
I didnt test it but maybe this works
0
When you dont make return statement to the recursive function call you cant get the output of the recurion
0
@Jnn oh yes i forgot about return :D
0
And why do you need a in your calculation, cause you only assing a with a value and after that pass this value to o, so why do you not pass it directly to o
0
@Jnn in your code o,a,x arguments are required but only x is provided(as you can see in my original loop). And it doesnt say to start dividing n from 256 in your code
0
because this is the original formula: https://imgur.com/pmJzAFV
i am using a and o to go from the bottom to the top by passing one to another
0
You cant initialize the start variable in the for loop cause that is used in every iteration
0
@Jnn thanks a lot!!
could you please explain this line?
return z/(xpow + recursion (xpow, z << 1));