I can go higher with my computer, even if the reason is mainly because it's a really bad algorithm.
For that kind of problem, you should use dynamic programming, in short, you store the result of every sequence, so in order to get the next one, you query your already gotten result instead of doing all the sequence over again.
You can read a bt more here : https://www.geeksforgeeks.org/program-for-nth-fibonacci-number/
It will take more space as you keep all result in memory, but you will gain way more time.
You are using a recursive function in a loop, where the main goal of the recursive function is to get n-1, this is .. a lot of wasted time. Too much in fact. You should either use iterative, or recursive, but not both at the same time.
f = [0,1]
def fibonacci(n):
if(n>1):
fibonacci(n-1)
if(n<=0):
return
f.append(f[n]+f[n-1])
n = int(input("Enter number of terms:"))-2
print("Fibonacci sequence:")
fibonacci(n)
for e in f:
print(e)
To be honest, i won't take too much time writting an optimized fibonacci recursive function while there's probably a lot around.
I edited your code as much as I can, trying to keep the spirit.
So, we launch the recursive function only "once"
fibonacci(n)
We set n as input()-2 because we already have two number in our sequence.
f=[0, 1]
This is the list where we will keep our sequence
def fibonacci(n):
if(n>1):
fibonacci(n-1)
if(n<=0):
return
f.append(f[n]+f[n-1])
What we will do in the recursive function is :
Telling that we need the previous result if till we get down to 1. We already have f[1] so we don't have to calculated it. We have a return if we provide somthing like 0 or lower, preventing it to break if some user put negative value.
Then we add the result to the end of f[]
After we ran the recursive function, this will give you a list with an ordered fibonnaci sequence to n
real 0m0,034s
user 0m0,018s
sys 0m0,014s
This is the time i get with n=1000 on my device