Created
May 11, 2012 08:26
-
-
Save tomoki/2658347 to your computer and use it in GitHub Desktop.
Fibonacci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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