forked from rafi007akhtar/c-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibo-quick.c
More file actions
47 lines (38 loc) · 719 Bytes
/
fibo-quick.c
File metadata and controls
47 lines (38 loc) · 719 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* Compute F(n) using Recursive-Square algorithm, where F(n) is the nth Fibonacci number defined as
* F(0) = 0
* F(1) = 1
* F(n) = F(n-1) + F(n-2)
*/
#include <stdio.h>
// User-defined functions
int fibo(int);
int mat[2][2] = {{1, 1}, {0, 1}};
int main()
{
int n, f;
printf("Enter n: ");
scanf("%d", &n);
f = fibo(n);
printf("%dth Fibonacci number = %d", n, f);
return 0;
}
int fibo(int n)
{
int i, j, k, sum = 0;
int pow[2][2];
while (n > 0)
{
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
// c[i, j] = sum(a[i,k] * b[k,j]) from k = 0 to n - 1
for (k = 0; k < 2; k++) sum += mat[i][k] + mat[k][j];
mat[i][j] = sum;
sum = 0;
}
}
n--;
}
return mat[0][1];
}