iteration to recursive | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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; }

26th Nov 2019, 4:52 PM
A S
A S - avatar
15 Answers
+ 1
A S I thought you got an answer in your previous post itself, didn't you?
26th Nov 2019, 4:56 PM
Avinesh
Avinesh - avatar
+ 1
A S so are you approaching from top to bottom or bottom to top?
26th Nov 2019, 5:00 PM
Avinesh
Avinesh - avatar
+ 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.
26th Nov 2019, 5:06 PM
Avinesh
Avinesh - avatar
+ 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
26th Nov 2019, 7:19 PM
Jnn
Jnn - avatar
+ 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.
27th Nov 2019, 3:14 PM
Jnn
Jnn - avatar
0
@Avinesh i figured out how to do it with loop and now i need to convert to recursive :)
26th Nov 2019, 4:57 PM
A S
A S - avatar
0
@Avinesh from bottom to top
26th Nov 2019, 5:02 PM
A S
A S - avatar
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
26th Nov 2019, 5:06 PM
Jnn
Jnn - avatar
0
When you dont make return statement to the recursive function call you cant get the output of the recurion
26th Nov 2019, 5:07 PM
Jnn
Jnn - avatar
0
@Jnn oh yes i forgot about return :D
26th Nov 2019, 6:30 PM
A S
A S - avatar
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
26th Nov 2019, 6:33 PM
Jnn
Jnn - avatar
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
26th Nov 2019, 6:33 PM
A S
A S - avatar
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
26th Nov 2019, 6:39 PM
A S
A S - avatar
0
You cant initialize the start variable in the for loop cause that is used in every iteration
26th Nov 2019, 7:21 PM
Jnn
Jnn - avatar
0
@Jnn thanks a lot!! could you please explain this line? return z/(xpow + recursion (xpow, z << 1));
27th Nov 2019, 3:04 PM
A S
A S - avatar