|
1 | 1 | /**
|
2 |
| - * @file |
| 2 | + * @file |
3 | 3 | * @brief This program computes the N^th Fibonacci number in modulo mod
|
4 | 4 | * input argument .
|
5 | 5 | *
|
6 | 6 | * Takes O(logn) time to compute nth Fibonacci number
|
7 |
| - * |
| 7 | + * |
8 | 8 | *
|
9 | 9 | * \author [villayatali123](https://github.com/villayatali123)
|
10 | 10 | * \author [unknown author]()
|
11 |
| - * @see fibonacci.cpp, fibonacci_fast.cpp, string_fibonacci.cpp, fibonacci_large.cpp |
| 11 | + * @see fibonacci.cpp, fibonacci_fast.cpp, string_fibonacci.cpp, |
| 12 | + * fibonacci_large.cpp |
12 | 13 | */
|
13 | 14 |
|
14 |
| -#include<iostream> |
15 |
| -#include<vector> |
16 | 15 | #include <cassert>
|
| 16 | +#include <cstdint> /// for integral typedefs |
| 17 | +#include <iostream> |
| 18 | +#include <vector> |
17 | 19 |
|
18 | 20 | /**
|
19 | 21 | * This function finds nth fibonacci number in a given modulus
|
20 | 22 | * @param n nth fibonacci number
|
21 |
| - * @param mod modulo number |
| 23 | + * @param mod modulo number |
22 | 24 | */
|
23 |
| -uint64_t fibo(uint64_t n , uint64_t mod ) |
24 |
| -{ |
25 |
| - std::vector<uint64_t> result(2,0); |
26 |
| - std::vector<std::vector<uint64_t>> transition(2,std::vector<uint64_t>(2,0)); |
27 |
| - std::vector<std::vector<uint64_t>> Identity(2,std::vector<uint64_t>(2,0)); |
28 |
| - n--; |
29 |
| - result[0]=1, result[1]=1; |
30 |
| - Identity[0][0]=1; Identity[0][1]=0; |
31 |
| - Identity[1][0]=0; Identity[1][1]=1; |
32 |
| - |
33 |
| - transition[0][0]=0; |
34 |
| - transition[1][0]=transition[1][1]=transition[0][1]=1; |
35 |
| - |
36 |
| - while(n) |
37 |
| - { |
38 |
| - if(n%2) |
39 |
| - { |
40 |
| - std::vector<std::vector<uint64_t>> res(2, std::vector<uint64_t>(2,0)); |
41 |
| - for(int i=0;i<2;i++) |
42 |
| - { |
43 |
| - for(int j=0;j<2;j++) |
44 |
| - { |
45 |
| - for(int k=0;k<2;k++) |
46 |
| - { |
47 |
| - res[i][j]=(res[i][j]%mod+((Identity[i][k]%mod*transition[k][j]%mod))%mod)%mod; |
48 |
| - } |
49 |
| - } |
50 |
| - } |
51 |
| - for(int i=0;i<2;i++) |
52 |
| - { |
53 |
| - for(int j=0;j<2;j++) |
54 |
| - { |
55 |
| - Identity[i][j]=res[i][j]; |
56 |
| - } |
57 |
| - } |
58 |
| - n--; |
59 |
| - } |
60 |
| - else{ |
61 |
| - std::vector<std::vector<uint64_t>> res1(2, std::vector<uint64_t>(2,0)); |
62 |
| - for(int i=0;i<2;i++) |
63 |
| - { |
64 |
| - for(int j=0;j<2;j++) |
65 |
| - { |
66 |
| - for(int k=0;k<2;k++) |
67 |
| - { |
68 |
| - res1[i][j]=(res1[i][j]%mod+((transition[i][k]%mod*transition[k][j]%mod))%mod)%mod; |
69 |
| - } |
70 |
| - } |
71 |
| - } |
72 |
| - for(int i=0;i<2;i++) |
73 |
| - { |
74 |
| - for(int j=0;j<2;j++) |
75 |
| - { |
76 |
| - transition[i][j]=res1[i][j]; |
77 |
| - } |
78 |
| - } |
79 |
| - n=n/2; |
80 |
| - } |
81 |
| - } |
82 |
| - return ((result[0]%mod*Identity[0][0]%mod)%mod+(result[1]%mod*Identity[1][0]%mod)%mod)%mod; |
| 25 | +uint64_t fibo(uint64_t n, uint64_t mod) { |
| 26 | + std::vector<uint64_t> result(2, 0); |
| 27 | + std::vector<std::vector<uint64_t>> transition(2, |
| 28 | + std::vector<uint64_t>(2, 0)); |
| 29 | + std::vector<std::vector<uint64_t>> Identity(2, std::vector<uint64_t>(2, 0)); |
| 30 | + n--; |
| 31 | + result[0] = 1, result[1] = 1; |
| 32 | + Identity[0][0] = 1; |
| 33 | + Identity[0][1] = 0; |
| 34 | + Identity[1][0] = 0; |
| 35 | + Identity[1][1] = 1; |
| 36 | + |
| 37 | + transition[0][0] = 0; |
| 38 | + transition[1][0] = transition[1][1] = transition[0][1] = 1; |
| 39 | + |
| 40 | + while (n) { |
| 41 | + if (n % 2) { |
| 42 | + std::vector<std::vector<uint64_t>> res(2, |
| 43 | + std::vector<uint64_t>(2, 0)); |
| 44 | + for (int i = 0; i < 2; i++) { |
| 45 | + for (int j = 0; j < 2; j++) { |
| 46 | + for (int k = 0; k < 2; k++) { |
| 47 | + res[i][j] = |
| 48 | + (res[i][j] % mod + |
| 49 | + ((Identity[i][k] % mod * transition[k][j] % mod)) % |
| 50 | + mod) % |
| 51 | + mod; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + for (int i = 0; i < 2; i++) { |
| 56 | + for (int j = 0; j < 2; j++) { |
| 57 | + Identity[i][j] = res[i][j]; |
| 58 | + } |
| 59 | + } |
| 60 | + n--; |
| 61 | + } else { |
| 62 | + std::vector<std::vector<uint64_t>> res1( |
| 63 | + 2, std::vector<uint64_t>(2, 0)); |
| 64 | + for (int i = 0; i < 2; i++) { |
| 65 | + for (int j = 0; j < 2; j++) { |
| 66 | + for (int k = 0; k < 2; k++) { |
| 67 | + res1[i][j] = |
| 68 | + (res1[i][j] % mod + ((transition[i][k] % mod * |
| 69 | + transition[k][j] % mod)) % |
| 70 | + mod) % |
| 71 | + mod; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + for (int i = 0; i < 2; i++) { |
| 76 | + for (int j = 0; j < 2; j++) { |
| 77 | + transition[i][j] = res1[i][j]; |
| 78 | + } |
| 79 | + } |
| 80 | + n = n / 2; |
| 81 | + } |
| 82 | + } |
| 83 | + return ((result[0] % mod * Identity[0][0] % mod) % mod + |
| 84 | + (result[1] % mod * Identity[1][0] % mod) % mod) % |
| 85 | + mod; |
83 | 86 | }
|
84 | 87 |
|
85 | 88 | /**
|
86 | 89 | * Function to test above algorithm
|
87 | 90 | */
|
88 |
| -void test() |
89 |
| -{ |
90 |
| - assert(fibo(6, 1000000007 ) == 8); |
| 91 | +void test() { |
| 92 | + assert(fibo(6, 1000000007) == 8); |
91 | 93 | std::cout << "test case:1 passed\n";
|
92 |
| - assert(fibo(5, 1000000007 ) == 5); |
| 94 | + assert(fibo(5, 1000000007) == 5); |
93 | 95 | std::cout << "test case:2 passed\n";
|
94 |
| - assert(fibo(10 , 1000000007) == 55); |
| 96 | + assert(fibo(10, 1000000007) == 55); |
95 | 97 | std::cout << "test case:3 passed\n";
|
96 |
| - assert(fibo(500 , 100) == 25); |
| 98 | + assert(fibo(500, 100) == 25); |
97 | 99 | std::cout << "test case:3 passed\n";
|
98 |
| - assert(fibo(500 , 10000) == 4125); |
| 100 | + assert(fibo(500, 10000) == 4125); |
99 | 101 | std::cout << "test case:3 passed\n";
|
100 | 102 | std::cout << "--All tests passed--\n";
|
101 | 103 | }
|
102 | 104 |
|
103 | 105 | /**
|
104 | 106 | * Main function
|
105 | 107 | */
|
106 |
| -int main() |
107 |
| -{ |
108 |
| - test(); |
109 |
| - uint64_t mod=1000000007; |
110 |
| - std::cout<<"Enter the value of N: "; |
111 |
| - uint64_t n=0; std::cin>>n; |
112 |
| - std::cout<<n<<"th Fibonacci number in modulo " << mod << ": "<< fibo( n , mod) << std::endl; |
| 108 | +int main() { |
| 109 | + test(); |
| 110 | + uint64_t mod = 1000000007; |
| 111 | + std::cout << "Enter the value of N: "; |
| 112 | + uint64_t n = 0; |
| 113 | + std::cin >> n; |
| 114 | + std::cout << n << "th Fibonacci number in modulo " << mod << ": " |
| 115 | + << fibo(n, mod) << std::endl; |
113 | 116 | }
|
0 commit comments