From ac710c7bc43a0d5a0f4269a8a2bf9723d867895a Mon Sep 17 00:00:00 2001 From: Mohammad Shakib <50780268+Mo-Shakib@users.noreply.github.com> Date: Wed, 8 Sep 2021 18:48:44 +0600 Subject: [PATCH] chore(Python): added to recursion n-th_fibonacci_number (#451) Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com> --- algorithms/Python/README.md | 1 + .../Python/recursion/n-th_fibonacci_number.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 algorithms/Python/recursion/n-th_fibonacci_number.py diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index 33a2b650..6bc42b1e 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -19,6 +19,7 @@ ## Recursion 1. [Factorial](recursion/factorial.py) +2. [n-th Fibonacci number](recursion/n-th_fibonacci_number.py) ## Scheduling diff --git a/algorithms/Python/recursion/n-th_fibonacci_number.py b/algorithms/Python/recursion/n-th_fibonacci_number.py new file mode 100644 index 00000000..bb367a2b --- /dev/null +++ b/algorithms/Python/recursion/n-th_fibonacci_number.py @@ -0,0 +1,18 @@ +# Function for nth fibonacci number - Using recursion +# Taking 1st two fibonacci numbers as 0 and 1 + +FibArray = [1] +def fibonacci(n): + if n<0: + print("Incorrect input") + if n == 0: + return 0 + 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(100))