Pages

Wednesday, October 7, 2009

Fibonacci using quick matrix

Solutions :

int fibo(int n){       // using quick method matrix program
int i, h, j, k, t;
i=h=1;
j=k=0;
while(n>0){
if(n%2==1){
t=j*h;
j=i*h + j*k +t;
i= i*k + t;
}
t= h*h;
h=2*h*k + t;
k= k*k +t;
n= (int) n/2;
}
return j;
}

1 comment:

  1. it is a very quick method to generate fibonacci numbers. it worls within the time O(log(n))

    ReplyDelete