Skip to content

Commit 99c33c6

Browse files
committed
finding Fibonacci numbers
LeetCode
1 parent 12fac38 commit 99c33c6

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

fib_numbers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Learn Python together
2+
3+
""" Write a function to calculate the Fibonacci numbers.
4+
- Fibonacci numbers commonly denoted F(n) form a sequence,
5+
called the Fibonacci sequence, such that each number is the sum of
6+
the two preceding ones, starting from 0 and 1. That is,
7+
F(0) = 0, F(1) = 1
8+
F(n) = F(n - 1) + F(n - 2), for n > 1. """
9+
10+
# Solution
11+
def fibonacci(n):
12+
if n <= 0:
13+
return 0
14+
elif n == 1:
15+
return 1
16+
return fibonacci(n-1) + fibonacci(n-2) # recursive case
17+
18+
# Check
19+
print(fibonacci(5)) # Output -> 5
20+
print(fibonacci(12)) # Output -> 144
21+
print(fibonacci(-11)) # Output -> 0

0 commit comments

Comments
 (0)