25 AnswersNew Answer
25 AnswersNew Answer
Briana well then you can solve the problem by making your function working only through iteration
There isn't any limit of 1000 loops. It's not about the loop count as much as it's about what you're doing in each loop. Notice that this very simple Code Bit can loop 200 million times without timing out. https://code.sololearn.com/cKk2O52voasj/?ref=app
Briana every time you call the function it calls itself 2 times, in each of them it cals itself 2 more time, and so on till you reach j(1) and j(0). So when you call j(40) just one time you are actually calling the function 2^40 times that means 1 099 500 000 000 times. If you want to overcome the time limit you have to forget about recursion. To use iteration make an array that start with 2 and 1 and then with a loop you mke all the following subscripts equal to the sum of the previous two plus 1.
1000 laps are not that much. What are you doing in the loop? Are you calling a recursive function?
Davide uh.. Okay. I'll try, thankyou
Your welcome 🤗
Briana you are welcome 🤗
That's just a limitation if sololearn. You can run it on a PC without limitation
Benjamin Jürgens no.. It's timelimit in online judge
If you post your code you might get some suggestions how to optimize it. I don't know online judge, but every online code execution service will have limitations like that
Briana Can you post the code. That would help everyone a lot.
Aradhay Mathur this https://code.sololearn.com/cLBcN551QI1Y/?ref=app When i put any number of testcase and i put like 30-40 for case 1 (example), the output will out so long time
Briana Can you explain what you want as the output because I can understand the code.
Briana are you trying to calculate fibonacci numbers? Then have a look at other codes for reference https://code.sololearn.com/cw4lQtpo1ke1/?ref=app https://code.sololearn.com/c6xOYOCrd7FU/?ref=app https://code.sololearn.com/cc6zpO6KQAZx/?ref=app
Benjamin Jürgens no.. it's own formulas. I only want to overcome the timelimit error
Ok, so it is really just the timeout problem. You are recursing to the botton (n0 and n1) every time and since the code uses recursion twice, the effort doubles from n to n+1 Use an array J to save all known values. Use recursion only if the needed value is not in the array
Some small general advices: - Use proper indention - Don't suppress warnings like Aradhay suggested, they tell you valuable information and actually help you - Don't implement the limits into your code, they are just for you to know what to expect The warning says your function j does not return anything in some cases (n>40). Remove the condition on the last else branch
Davide i already tried it. It's work with iteration. Thankyou so much
For your case, use simple iteration and memoization.
Learn Playing. Play Learning
SoloLearn Inc.4 Embarcadero Center, Suite 1455
Send us a message