Im supposed to create a recursive and non recursive function to calculate this.
F(n) = 1 for n = 0 or n = 1 or n=2
F(n) = F((n+1)/2)^2 + F((n-1)/2)^ 2 for n is odd
F(n) = F(n/2 + 1)^2 – F(n/2 – 1)^ 2 for n is even
I know how to do the recursive one which looks like this and returns the fibonacci numbers
if (n == 0 || n == 1 || n == 2) {
return 1;}
else if (n % 2 == 0) {
a=F(n/2+1);
b=F(n/2-1);
return a*a - b*b; }
else {
c=F((n+1)/2);
d=F((n-1)/2);
return c*c + d*d; }
but how do you change it to a non-recursive function?