#include int main() { int n; int a=1, b=1, c=1, d=0; // Fibonacci Q-matrix [[1,1],[1,0]] int res_a=1, res_b=0, res_c=0, res_d=1; // Identity matrix int x,y,z,w; printf("Enter n: "); scanf("%d", &n); while(n > 0) { if(n & 1) { // res = res * base x = res_a*a + res_b*c; y = res_a*b + res_b*d; z = res_c*a + res_d*c; w = res_c*b + res_d*d; res_a=x; res_b=y; res_c=z; res_d=w; } // base = base * base x = a*a + b*c; y = a*b + b*d; z = c*a + d*c; w = c*b + d*d; a=x; b=y; c=z; d=w; n >>= 1; // divide by 2 } printf("Fibonacci = %d\n", res_b); // F(n) is stored here return 0; }