Fibonacci funtion added (#767)

* finbonacci funtion added

* finbonacci funtion added

* finbonacci funtion added

* finbonacci funtion added

* Update fibonacci.cpp

Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
vonzo 2020-05-19 17:50:15 +02:00 committed by GitHub
parent a11c09e6ba
commit 03664deca9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

25
math/fibonacci.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <iostream>
#include <cassert>
/* Calculate the the value on Fibonacci's sequence given an
integer as input
Fibonacci = 0, 1, 1, 2, 3, 5,
8, 13, 21, 34, 55,
89, 144, ... */
int fibonacci(uint n) {
/* 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);
}
int main() {
int n;
std::cin >> n;
assert(n >= 0);
std::cout << "F(" << n << ")= " << fibonacci(n) << std::endl;
}