Nth value of Function Given x and y we define a function as follow : 
f(1)=x
f(2)=y
f(i)=f(i-1) + f(i+1) for i>2

Now given x and y, how to calculate f(n)
Example : If x=2 and y=3 and n=3 then answer is 1
as f(2) = f(1) + f(3), 3 = 2 + f(3), f(3) = 1.
Constraints are : x,y,n all can go upto 10^9.
 A: Calculate the first dozen numbers.  You should find a simple result for this algorithm.
A: The general way to solve $$F(i+1)= F(i) - F(i - 1)$$
Is to first to see that if $F(i + 1)$ can be written as the sum of 2 geometric series, so that $$F(n) = ar^n + bs^n$$
So $$ar^{n+1} + bs^{n+1} = ar^{n} + bs^n - ar^{n-1} - bs^{n-1}$$
 $$ar^{n-1}(1 - r + r^2) = -bs^{n-1}(1 - s + s^2)$$
Since $r\ne s$, it follows that both $r$ and $s$ must satisfy the quadratic equation $$1-x+x^2 = 0$$
So $$r = \frac{1 + \sqrt{3}i}{2}, s = \frac{1 - \sqrt{3}i}{2}$$
Now we find $a$ and $b$, using $F(1)$ and $F(2)$
$$F(1) = x = a\left(\frac{1 + \sqrt{3}i}{2}\right)^1 + b\left(\frac{1 - \sqrt{3}i}{2}\right)^1$$
$$F(2) = y = a\left(\frac{1 + \sqrt{3}i}{2}\right)^2 + b\left(\frac{1 - \sqrt{3}i}{2}\right)^2$$
And then you find the closed form of your recurrence series.
A: You can use this code:
#include <stdio.h>

int fib(int n,int x,int y){
     if (n==1) return x;
     else if (n==2) return y;
     else return fib(n-1,x,y) + fib(n-2,x,y);
}

int main()
{
   int x,y,n,z;
   printf("Give a value for x:");
   scanf("%d",&x);
   printf("Give a value for y:");
   scanf("%d",&y);
   printf("Give a value for n:");
   scanf("%d",&n);
   z=fib(n,x,y);
   printf("The result is: %d",z);
}

