We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 12fac38 commit 99c33c6Copy full SHA for 99c33c6
fib_numbers.py
@@ -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