Python Forum
fibonacci ***Time limit exceeded***
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fibonacci ***Time limit exceeded***
#1
def fib(number): 
  if number==0: 
      return 0
  elif number==1: 
      return 1
  else: 
    return fib(number-1)+fib(number-2)

n=int(input())
while n!="END" and n>0:
  print (fib(n))
  n=int(input())
With this code i can show the N fibonacci number given. But my code is not accepted because of this
***Time limit exceeded***. Any ideas?
Reply
#2
You are making the same calculations over and over again. Say you try fib(5). That's fib(4) + fib(3). But those are fib(3) + fib(2) + fib(2) + fib(1). So far you've called fib(3) and fib(2) twice each. It would be faster to just generate the numbers until you have n of them. Now, if you have to pass multiple tests, I would save the numbers you calculate for the first test, so you don't have to calculate them again.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
This is the list that they told me,to suppose that i have
F=[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...]
Reply
#4
Ichabod801's suggestion is an example of dynamic programming. It is probably what you're supposed to do. Accessing F[k] is much faster than calling fib(k). See this image extracted from wikipedia.
Reply
#5
(Nov-24-2018, 05:24 AM)frequency Wrote: This is the list that they told me,to suppose that i have
F=[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...]

Right. So start with [1, 1], and keep adding the last two numbers together until you have n of them. Then return the last one.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
So should i make a look to append the 1 last and 2 last number calculation on the last position,so i can call the item after?
Reply
#7
I'm not sure I understood your question, but you would start with [1, 1]. Add the last two and append it, to get [1, 1, 2]. Add the last two and append it, to get [1, 1, 2, 3]. Keep going until you have n numbers, and return the last one.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
So i made this
f=[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
for i in range (99999):
    f.append(f[-1]+f[-2])
user_input=input()
while user_input!="END":
    user_input=int(user_input)
    print (f[user_input-1])
    user_input=input()
9999 in range because it will test maximum 9999 digit number.On idle it works but on my submittion page,it will cause a MemoryError.
Reply
#9
First of all, you are still doing too much calculation. You only need to calculate until you have as many numbers as requested.

Second of all, you have 5 nines in your range, not 4. If that doesn't fix the problem, just keep track of the last two numbers, and the count of which number you're on.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
f=[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
for i in range (9999):
    f.append(f[-1]+f[-2])
user_input=input()
while user_input!="END":
    user_input=int(user_input)
    print (f[user_input-1])
    user_input=input()
I actually removed the wrongfuly placed 9 there. Got it working
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fibonacci Yasunaga 7 2,948 May-16-2021, 02:36 PM
Last Post: csr
  Assign a value if datetime is in between a particular time limit klllmmm 2 2,767 Jan-02-2021, 07:00 AM
Last Post: klllmmm
  Time Limit Exceeded error loves 5 3,163 Dec-03-2020, 07:15 AM
Last Post: Sofia_Grace
Bug maximum recursion depth exceeded while calling a Python object error in python3 Prezess 4 3,783 Aug-02-2020, 02:21 PM
Last Post: deanhystad
  RecursionError: maximum recursion depth exceeded in comparison ? leoahum 11 13,088 Mar-18-2019, 01:53 PM
Last Post: leoahum
  'Time Limit Exceeded' Problem bkpee3 2 5,444 Nov-14-2018, 03:51 AM
Last Post: bkpee3
  If conditions with time limit unknowntothem 4 3,075 Nov-09-2018, 08:59 PM
Last Post: nilamo
  Fibonacci sequence Darbandiman123 2 2,707 Sep-26-2018, 02:32 PM
Last Post: Darbandiman123
  Help debug my fibonacci implementation dineshpabbi10 6 4,000 May-16-2018, 12:12 PM
Last Post: dineshpabbi10
  maximum recursion depth exceeded saba_keon 3 7,438 Apr-08-2018, 07:30 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020