From 27486c262915ca8e5a689db50f6e7b08a150b38d Mon Sep 17 00:00:00 2001 From: Manish kumar chaurasia <30630740+kmanish31@users.noreply.github.com> Date: Thu, 10 Oct 2019 23:47:34 +0530 Subject: [PATCH] create fibonacci using golden ratio another method to calculate nth fibonacci number --- Fibonacci/fibonaaci using golden ratio | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Fibonacci/fibonaaci using golden ratio diff --git a/Fibonacci/fibonaaci using golden ratio b/Fibonacci/fibonaaci using golden ratio new file mode 100644 index 0000000..24fe4d8 --- /dev/null +++ b/Fibonacci/fibonaaci using golden ratio @@ -0,0 +1,42 @@ +// C++ program to find n-th fibonacci +#include +using namespace std; + +// Approx value of golden ratio +double golden = 1.6180339; + +int fib[6] = { 0, 1, 1, 2, 3, 5 }; + +// function to find nth Fibonacci number +int fibonacci (int n) +{ + + if (n < 6) + return fib[n]; + + // counting from 5th term + int j = 5, fibn = 5; + + while (j < n) { + + // function to round off the value + fibn = round(fibn * golden); + j++; + } + + + return fibn; +} + + +int main() +{ + int n = 9; + +// print nth fibonacci + + cout << n <<"th Fibonacci:" + << fibonacci(n) << endl; + + return 0; +}