fibonacci_sum.cpp File Reference

An algorithm to calculate the sum of Fibonacci Sequence: \(\mathrm{F}(n) + \mathrm{F}(n+1) + .. + \mathrm{F}(m)\). More...

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

## Namespaces

| math
| for std::vector
| fibonacci_sum
| Functions for the sum of the Fibonacci Sequence: \(\mathrm{F}(n) + \mathrm{F}(n+1) + .. + \mathrm{F}(m)\).

## Typedefs

using | math::fibonacci_sum::matrix = std::vector< std::vector< uint64_t > >

## Functions

math::fibonacci_sum::matrix | math::fibonacci_sum::multiply (const math::fibonacci_sum::matrix &T, const math::fibonacci_sum::matrix &A)
math::fibonacci_sum::matrix | math::fibonacci_sum::power (math::fibonacci_sum::matrix T, uint64_t ex)
uint64_t | math::fibonacci_sum::result (uint64_t n)
uint64_t | math::fibonacci_sum::fiboSum (uint64_t n, uint64_t m)
static void | test ()
int | main ()
| Main function. More...

## Detailed Description

An algorithm to calculate the sum of Fibonacci Sequence: \(\mathrm{F}(n) + \mathrm{F}(n+1) + .. + \mathrm{F}(m)\).

An algorithm to calculate the sum of Fibonacci Sequence: \(\mathrm{F}(n) + \mathrm{F}(n+1) + .. + \mathrm{F}(m)\) where \(\mathrm{F}(i)\) denotes the i-th Fibonacci Number . Note that F(0) = 0 and F(1) = 1. The value of the sum is calculated using matrix exponentiation. Reference source: https://stackoverflow.com/questions/4357223/finding-the-sum-of-fibonacci-numbers

Author
  Sarthak Sahu

## Function Documentation

## fiboSum()

uint64_t math::fibonacci_sum::fiboSum | ( | uint64_t | n,
|  | uint64_t | m
| ) |  |

Function to compute sum of fibonacci sequence from n to m.

Parameters

  n | start of sequence
  m | end of sequence

Returns
  uint64_t the sum of sequence

{
return (result(m + 2) - result(n + 1));
}
math::fibonacci_sum::result
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
test(); // execute the tests
return 0;
}
test
static void test()
Definition: fibonacci_sum.cpp:101
Here is the call graph for this function:

## multiply()

math::fibonacci_sum::matrix math::fibonacci_sum::multiply | ( | const math::fibonacci_sum::matrix & | T,
|  | const math::fibonacci_sum::matrix & | A
| ) |  |

Function to multiply two matrices

Parameters

  T | matrix 1
  A | martix 2

Returns
  resultant matrix

{
math::fibonacci_sum::matrix result(2, std::vector<uint64_t>(2, 0));
// multiplying matrices
result[0][0] = T[0][0] * A[0][0] + T[0][1] * A[1][0];
result[0][1] = T[0][0] * A[0][1] + T[0][1] * A[1][1];
result[1][0] = T[1][0] * A[0][0] + T[1][1] * A[1][0];
result[1][1] = T[1][0] * A[0][1] + T[1][1] * A[1][1];
return result;
}
std::vector
Here is the call graph for this function:

## power()

math::fibonacci_sum::matrix math::fibonacci_sum::power | ( | math::fibonacci_sum::matrix | T,
|  | uint64_t | ex
| ) |  |

Function to compute A^n where A is a matrix.

Parameters

  T | matrix
  ex | power

Returns
  resultant matrix

{
math::fibonacci_sum::matrix A{{1, 1}, {1, 0}};
if (ex == 0 || ex == 1) {
return T;
}
T = power(T, ex / 2);
T = multiply(T, T);
if (ex & 1) {
T = multiply(T, A);
}
return T;
}
math::fibonacci_sum::power
math::fibonacci_sum::matrix power(math::fibonacci_sum::matrix T, uint64_t ex)
Definition: fibonacci_sum.cpp:57
machine_learning::multiply
std::vector< std::valarray< T > > multiply(const std::vector< std::valarray< T >> &A, const std::vector< std::valarray< T >> &B)
Definition: vector_ops.hpp:460
Here is the call graph for this function:

## result()

uint64_t math::fibonacci_sum::result | ( | uint64_t | n | ) |

Function to compute sum of fibonacci sequence from 0 to n.

Parameters

  n | number

Returns
  uint64_t ans, the sum of sequence

{
math::fibonacci_sum::matrix T{{1, 1}, {1, 0}};
T = power(T, n);
uint64_t ans = T[0][1];
ans = (ans - 1);
return ans;
}
ans
ll ans(ll n)
Definition: matrix_exponentiation.cpp:91
Here is the call graph for this function:

## test()

static void test | ( |  | ) |

static

Function for testing fiboSum function. test cases and assert statement.

Returns
  `void`

{
uint64_t n = 0, m = 3;
uint64_t test_1 = math::fibonacci_sum::fiboSum(n, m);
assert(test_1 == 4);
std::cout << "Passed Test 1!" << std::endl;
n = 3;
m = 5;
uint64_t test_2 = math::fibonacci_sum::fiboSum(n, m);
assert(test_2 == 10);
std::cout << "Passed Test 2!" << std::endl;
n = 5;
m = 7;
uint64_t test_3 = math::fibonacci_sum::fiboSum(n, m);
assert(test_3 == 26);
std::cout << "Passed Test 3!" << std::endl;
n = 7;
m = 10;
uint64_t test_4 = math::fibonacci_sum::fiboSum(n, m);
assert(test_4 == 123);
std::cout << "Passed Test 4!" << std::endl;
n = 9;
m = 12;
uint64_t test_5 = math::fibonacci_sum::fiboSum(n, m);
assert(test_5 == 322);
std::cout << "Passed Test 5!" << std::endl;
}
std::cout
std::endl
T endl(T... args)
math::fibonacci_sum::fiboSum
uint64_t fiboSum(uint64_t n, uint64_t m)
Definition: fibonacci_sum.cpp:90
test_1
static void test_1()
Definition: heavy_light_decomposition.cpp:505
test_2
static void test_2()
Definition: heavy_light_decomposition.cpp:549
test_3
static void test_3()
Definition: heavy_light_decomposition.cpp:592
Here is the call graph for this function: