From f31260589dc3dde35c3649f2338094f4cdc520a0 Mon Sep 17 00:00:00 2001 From: hogrider301 <81501664+hogrider301@users.noreply.github.com> Date: Mon, 3 Oct 2022 14:33:03 +0530 Subject: [PATCH] Create fibonacci.py --- Algorithms/fibonacci.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Algorithms/fibonacci.py diff --git a/Algorithms/fibonacci.py b/Algorithms/fibonacci.py new file mode 100644 index 0000000..1102aaf --- /dev/null +++ b/Algorithms/fibonacci.py @@ -0,0 +1,18 @@ +# Function for nth fibonacci number - Dynamic Programming +# Taking 1st two fibonacci numbers as 0 and 1 + +FibArray = [0, 1] + +def fibonacci(n): + if n<0: + print("Incorrect input") + elif n<= len(FibArray): + return FibArray[n-1] + else: + temp_fib = fibonacci(n-1)+fibonacci(n-2) + FibArray.append(temp_fib) + return temp_fib + +# Driver Program + +print(fibonacci(9))