Python/maths/fibonacci_sequence_recursion.py

23 lines
571 B
Python
Raw Normal View History

2018-10-19 12:48:28 +00:00
# Fibonacci Sequence Using Recursion
2019-10-05 05:14:13 +00:00
2018-10-19 12:48:28 +00:00
def recur_fibo(n):
"""
>>> [recur_fibo(i) for i in range(12)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
"""
return n if n <= 1 else recur_fibo(n - 1) + recur_fibo(n - 2)
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
2018-10-19 12:48:28 +00:00
def main():
limit = int(input("How many terms to include in fibonacci series: "))
if limit > 0:
print(f"The first {limit} terms of the fibonacci series are as follows:")
2018-10-19 12:48:28 +00:00
print([recur_fibo(n) for n in range(limit)])
else:
print("Please enter a positive integer: ")
2019-10-05 05:14:13 +00:00
if __name__ == "__main__":
2018-10-19 12:48:28 +00:00
main()