chore(Python): added to recursion n-th_fibonacci_number (#451)
Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com>pull/454/head
parent
fc70e7f0f5
commit
ac710c7bc4
|
@ -19,6 +19,7 @@
|
||||||
## Recursion
|
## Recursion
|
||||||
|
|
||||||
1. [Factorial](recursion/factorial.py)
|
1. [Factorial](recursion/factorial.py)
|
||||||
|
2. [n-th Fibonacci number](recursion/n-th_fibonacci_number.py)
|
||||||
|
|
||||||
## Scheduling
|
## Scheduling
|
||||||
|
|
||||||
|
|
|
@ -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))
|
Loading…
Reference in New Issue