Skip to content

Instantly share code, notes, and snippets.

@tomoki
Created May 11, 2012 08:26
Show Gist options
  • Save tomoki/2658347 to your computer and use it in GitHub Desktop.
Save tomoki/2658347 to your computer and use it in GitHub Desktop.
Fibonacci
#include <stdio.h>
long long fibiter(long long a,long long b,long long p,long long q,long long count){
if(count == 0){
return b;
}
if(count % 2 == 0){
return fibiter(a,b,(p*p+q*q),(2*p*q+q*q),(count/2));
}else{
return fibiter((b*q+a*q+a*p),(b*p+a*q),p,q,(count-1));
}
}
long long fib(long long n){
return fibiter(1,0,0,1,n);
}
int main(){
long long i;
for(i=1;i<10;i++){
printf("%lld\n",fib(i));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment