TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
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 <cstdint>
#include <iostream>
#include <vector>
Include dependency graph for fibonacci_sum.cpp:

Go to the source code of this file.

Namespaces

namespace  math
 for assert
 
namespace  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.
 

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

Definition in file fibonacci_sum.cpp.

Typedef Documentation

◆ matrix

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

Definition at line 31 of file fibonacci_sum.cpp.

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
nstart of sequence
mend of sequence
Returns
uint64_t the sum of sequence

Definition at line 91 of file fibonacci_sum.cpp.

91 {
92 return (result(m + 2) - result(n + 1));
93}
uint64_t result(uint64_t n)

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 137 of file fibonacci_sum.cpp.

137 {
138 test(); // execute the tests
139 return 0;
140}
static void test()

◆ 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
Tmatrix 1
Amartix 2
Returns
resultant matrix

Definition at line 39 of file fibonacci_sum.cpp.

40 {
41 math::fibonacci_sum::matrix result(2, std::vector<uint64_t>(2, 0));
42
43 // multiplying matrices
44 result[0][0] = T[0][0] * A[0][0] + T[0][1] * A[1][0];
45 result[0][1] = T[0][0] * A[0][1] + T[0][1] * A[1][1];
46 result[1][0] = T[1][0] * A[0][0] + T[1][1] * A[1][0];
47 result[1][1] = T[1][0] * A[0][1] + T[1][1] * A[1][1];
48
49 return result;
50}

◆ 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
Tmatrix
expower
Returns
resultant matrix

Definition at line 58 of file fibonacci_sum.cpp.

58 {
59 math::fibonacci_sum::matrix A{{1, 1}, {1, 0}};
60 if (ex == 0 || ex == 1) {
61 return T;
62 }
63
64 T = power(T, ex / 2);
65 T = multiply(T, T);
66 if (ex & 1) {
67 T = multiply(T, A);
68 }
69 return T;
70}
int multiply(int x, int res[], int res_size)
void power(int x, int n)

◆ result()

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

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

Parameters
nnumber
Returns
uint64_t ans, the sum of sequence

Definition at line 77 of file fibonacci_sum.cpp.

77 {
78 math::fibonacci_sum::matrix T{{1, 1}, {1, 0}};
79 T = power(T, n);
80 uint64_t ans = T[0][1];
81 ans = (ans - 1);
82 return ans;
83}

◆ test()

static void test ( )
static

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

Returns
void

Definition at line 102 of file fibonacci_sum.cpp.

102 {
103 uint64_t n = 0, m = 3;
104 uint64_t test_1 = math::fibonacci_sum::fiboSum(n, m);
105 assert(test_1 == 4);
106 std::cout << "Passed Test 1!" << std::endl;
107
108 n = 3;
109 m = 5;
110 uint64_t test_2 = math::fibonacci_sum::fiboSum(n, m);
111 assert(test_2 == 10);
112 std::cout << "Passed Test 2!" << std::endl;
113
114 n = 5;
115 m = 7;
116 uint64_t test_3 = math::fibonacci_sum::fiboSum(n, m);
117 assert(test_3 == 26);
118 std::cout << "Passed Test 3!" << std::endl;
119
120 n = 7;
121 m = 10;
122 uint64_t test_4 = math::fibonacci_sum::fiboSum(n, m);
123 assert(test_4 == 123);
124 std::cout << "Passed Test 4!" << std::endl;
125
126 n = 9;
127 m = 12;
128 uint64_t test_5 = math::fibonacci_sum::fiboSum(n, m);
129 assert(test_5 == 322);
130 std::cout << "Passed Test 5!" << std::endl;
131}
uint64_t fiboSum(uint64_t n, uint64_t m)
static void test_1()
static void test_2()
static void test_3()