fibonacci.cpp File Reference

Generate fibonacci sequence. More...

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

## Functions

uint64_t | fibonacci (uint64_t n)
static void | test ()
int | main ()
| Main function.

## Detailed Description

Generate fibonacci sequence.

Calculate the the value on Fibonacci's sequence given an integer as input.

\[\text{fib}(n) = \text{fib}(n-1) + \text{fib}(n-2)\]

See also
  fibonacci_large.cpp, fibonacci_fast.cpp, string_fibonacci.cpp

## Function Documentation

## fibonacci()

uint64_t fibonacci | ( | uint64_t | n | ) |

Recursively compute sequences

Parameters

  n | input

Returns
  n-th element of the Fbinacci's sequence

{
/* If the input is 0 or 1 just return the same
This will set the first 2 values of the sequence */
if (n <= 1) {
return n;
}
/* Add the last 2 values of the sequence to get next */
return fibonacci(n - 1) + fibonacci(n - 2);
}
fibonacci
uint64_t fibonacci(uint64_t n)
Definition: fibonacci.cpp:19

## test()

static void test | ( |  | ) |

static

Function for testing the fibonacci() function with a few test cases and assert statement.

Returns
  `void`

{
uint64_t test_case_1 = fibonacci(0);
assert(test_case_1 == 0);
std::cout << "Passed Test 1!" << std::endl;
uint64_t test_case_2 = fibonacci(1);
assert(test_case_2 == 1);
std::cout << "Passed Test 2!" << std::endl;
uint64_t test_case_3 = fibonacci(2);
assert(test_case_3 == 1);
std::cout << "Passed Test 3!" << std::endl;
uint64_t test_case_4 = fibonacci(3);
assert(test_case_4 == 2);
std::cout << "Passed Test 4!" << std::endl;
uint64_t test_case_5 = fibonacci(4);
assert(test_case_5 == 3);
std::cout << "Passed Test 5!" << std::endl;
uint64_t test_case_6 = fibonacci(15);
assert(test_case_6 == 610);
std::cout << "Passed Test 6!" << std::endl << std::endl;
}
std::cout
std::endl
T endl(T... args)
Here is the call graph for this function: