TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
fibonacci_matrix_exponentiation.cpp File Reference

This program computes the N^th Fibonacci number in modulo mod input argument . More...

#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
Include dependency graph for fibonacci_matrix_exponentiation.cpp:

Go to the source code of this file.

Functions

uint64_t fibo (uint64_t n, uint64_t mod)
 
static void test ()
 
int main ()
 

Detailed Description

This program computes the N^th Fibonacci number in modulo mod input argument .

Takes O(logn) time to compute nth Fibonacci number

Author
villayatali123
[unknown author]()
See also
fibonacci.cpp, fibonacci_fast.cpp, string_fibonacci.cpp, fibonacci_large.cpp

Definition in file fibonacci_matrix_exponentiation.cpp.

Function Documentation

◆ fibo()

uint64_t fibo ( uint64_t n,
uint64_t mod )

This function finds nth fibonacci number in a given modulus

Parameters
nnth fibonacci number
modmodulo number

Definition at line 25 of file fibonacci_matrix_exponentiation.cpp.

25 {
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;
86}
double k(double x)
Another test function.
uint64_t result(uint64_t n)

◆ main()

int main ( void )

Main function

Definition at line 108 of file fibonacci_matrix_exponentiation.cpp.

108 {
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;
116}
static void test()
uint64_t fibo(uint64_t n, uint64_t mod)

◆ test()

static void test ( )
static

Function to test above algorithm

Definition at line 91 of file fibonacci_matrix_exponentiation.cpp.

91 {
92 assert(fibo(6, 1000000007) == 8);
93 std::cout << "test case:1 passed\n";
94 assert(fibo(5, 1000000007) == 5);
95 std::cout << "test case:2 passed\n";
96 assert(fibo(10, 1000000007) == 55);
97 std::cout << "test case:3 passed\n";
98 assert(fibo(500, 100) == 25);
99 std::cout << "test case:3 passed\n";
100 assert(fibo(500, 10000) == 4125);
101 std::cout << "test case:3 passed\n";
102 std::cout << "--All tests passed--\n";
103}