r/PythonLearning • u/samoromi • 11d ago
school
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)
n = int(input("Количество чисел: "))
for i in range(n):
print(fib(i), end=" ")
0
Upvotes
View all comments
2
u/Naive_Programmer_232 11d ago
yep that's a recursive fibonacci algorithm. only issue with this is if n grows large, you'll run into a stack overflow error, "maximum recursion depth exceeded". So, I'd consider maybe writing this iteratively to avoid that or do a memoized (
dict) version (manually or viafunctools) for better performance.