forked from Zipcoder/PyPart4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci_recursive.py
More file actions
45 lines (34 loc) · 1.06 KB
/
fibonacci_recursive.py
File metadata and controls
45 lines (34 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Exercise 2
The Fibonacci Sequence is the series of numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Given the term n, determine the value of x(n).
n = 0 1 2 3 4 5 6 7 8 9 10 11 ..
x(n) = 0 1 1 2 3 5 8 13 21 34 55 89 ..
When n = 0, x(n) = 0
When n = 4, x(n) = 3
When n = 5, x(n) = 5
x(n) can be determined with the following rule:
x(n) = x(n - 1) + x(n - 2)
Create a program called fibonacci_recursive.py
Requirements
Given a term (n), determine the value of x(n).
In the fibonacci_recursive.py program, create a function called fibonnaci. The function should take in an integer and return the value of x(n).
This problem must be solved using recursion.
Constraints
n >= 0 and n <= 30
Answer below:
"""
def fibonacci(n):
if n>1:
return(fibonacci(n-1) + fibonacci(n-2))
elif n==0:
return 0
elif n==1:
return 1
return(fibonacci(n))
n = int(input("Provide a number between 0 and 30: "))
if n>=0 and n<=30:
print(fibonacci(n))
else:
print("You need to provide a valid input to run the function")