From 03664deca98e04f0e58464553acdc339d9f78ae5 Mon Sep 17 00:00:00 2001 From: vonzo <34040775+vonzo@users.noreply.github.com> Date: Tue, 19 May 2020 17:50:15 +0200 Subject: [PATCH] Fibonacci funtion added (#767) * finbonacci funtion added * finbonacci funtion added * finbonacci funtion added * finbonacci funtion added * Update fibonacci.cpp Co-authored-by: Christian Clauss --- math/fibonacci.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 math/fibonacci.cpp diff --git a/math/fibonacci.cpp b/math/fibonacci.cpp new file mode 100644 index 000000000..1c07cd93f --- /dev/null +++ b/math/fibonacci.cpp @@ -0,0 +1,25 @@ +#include +#include + +/* 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; +}