fibonacci_matrix_exponentiation.cpp File Reference

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

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

## Functions

uint64_t | fibo (uint64_t n, uint64_t mod)
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

## Function Documentation

## fibo()

uint64_t fibo | ( | uint64_t | n,
|  | uint64_t | mod
| ) |  |

This function finds nth fibonacci number in a given modulus

Parameters

  n | nth fibonacci number
  mod | modulo number

{
std::vector<uint64_t> result(2,0);
std::vector<std::vector<uint64_t>> transition(2,std::vector<uint64_t>(2,0));
std::vector<std::vector<uint64_t>> Identity(2,std::vector<uint64_t>(2,0));
n--;
result[0]=1, result[1]=1;
Identity[0][0]=1; Identity[0][1]=0;
Identity[1][0]=0; Identity[1][1]=1;
transition[0][0]=0;
transition[1][0]=transition[1][1]=transition[0][1]=1;
while(n)
{
if(n%2)
{
std::vector<std::vector<uint64_t>> res(2, std::vector<uint64_t>(2,0));
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<2;k++)
{
res[i][j]=(res[i][j]%mod+((Identity[i][k]%mod*transition[k][j]%mod))%mod)%mod;
}
}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
Identity[i][j]=res[i][j];
}
}
n--;
}
else{
std::vector<std::vector<uint64_t>> res1(2, std::vector<uint64_t>(2,0));
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<2;k++)
{
res1[i][j]=(res1[i][j]%mod+((transition[i][k]%mod*transition[k][j]%mod))%mod)%mod;
}
}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
transition[i][j]=res1[i][j];
}
}
n=n/2;
}
}
return ((result[0]%mod*Identity[0][0]%mod)%mod+(result[1]%mod*Identity[1][0]%mod)%mod)%mod;
}
math::fibonacci_sum::result
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76
std::vector< uint64_t >

## main()

int main | ( | void |  | ) |

Main function

{
test();
uint64_t mod=1000000007;
std::cout<<"Enter the value of N: ";
uint64_t n=0; std::cin>>n;
std::cout<<n<<"th Fibonacci number in modulo " << mod << ": "<< fibo( n , mod) << std::endl;
}
std::cin
std::cout
std::endl
T endl(T... args)
fibo
uint64_t fibo(uint64_t n, uint64_t mod)
Definition: fibonacci_matrix_exponentiation.cpp:23
test
void test()
Definition: fibonacci_matrix_exponentiation.cpp:88
Here is the call graph for this function:

## test()

void test | ( |  | ) |

Function to test above algorithm

{
assert(fibo(6, 1000000007 ) == 8);
std::cout << "test case:1 passed\n";
assert(fibo(5, 1000000007 ) == 5);
std::cout << "test case:2 passed\n";
assert(fibo(10 , 1000000007) == 55);
std::cout << "test case:3 passed\n";
assert(fibo(500 , 100) == 25);
std::cout << "test case:3 passed\n";
assert(fibo(500 , 10000) == 4125);
std::cout << "test case:3 passed\n";
std::cout << "--All tests passed--\n";
}
Here is the call graph for this function: