+ 8

How to overcome timelimit in c language?

I need 1000 loop but that makes my code have timelimit problem

20th Nov 2020, 6:18 PM
Briana
Briana - avatar
23 ответов
+ 7
Briana well then you can solve the problem by making your function working only through iteration
20th Nov 2020, 6:26 PM
Davide
Davide - avatar
+ 13
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
20th Nov 2020, 9:18 PM
David Carroll
David Carroll - avatar
+ 6
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.
21st Nov 2020, 11:17 AM
Davide
Davide - avatar
+ 3
1000 laps are not that much. What are you doing in the loop? Are you calling a recursive function?
20th Nov 2020, 6:23 PM
Davide
Davide - avatar
+ 3
Davide uh.. Okay. I'll try, thankyou
20th Nov 2020, 6:28 PM
Briana
Briana - avatar
+ 2
Your welcome 🤗
20th Nov 2020, 6:29 PM
Davide
Davide - avatar
+ 2
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
21st Nov 2020, 11:43 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 2
Briana you are welcome 🤗
21st Nov 2020, 12:32 PM
Davide
Davide - avatar
+ 1
That's just a limitation if sololearn. You can run it on a PC without limitation
20th Nov 2020, 6:24 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Davide yes..
20th Nov 2020, 6:24 PM
Briana
Briana - avatar
+ 1
Benjamin Jürgens no.. It's timelimit in online judge
20th Nov 2020, 6:24 PM
Briana
Briana - avatar
+ 1
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
20th Nov 2020, 6:29 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Briana Can you post the code. That would help everyone a lot.
21st Nov 2020, 10:28 AM
Aradhay Mathur
Aradhay Mathur - avatar
+ 1
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
21st Nov 2020, 10:34 AM
Briana
Briana - avatar
+ 1
Briana Can you explain what you want as the output because I can understand the code.
21st Nov 2020, 10:37 AM
Aradhay Mathur
Aradhay Mathur - avatar
21st Nov 2020, 10:54 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Benjamin Jürgens no.. it's own formulas. I only want to overcome the timelimit error
21st Nov 2020, 10:55 AM
Briana
Briana - avatar
+ 1
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
21st Nov 2020, 11:07 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Davide i already tried it. It's work with iteration. Thankyou so much
21st Nov 2020, 12:30 PM
Briana
Briana - avatar
+ 1
For your case, use simple iteration and memoization.
23rd Nov 2020, 9:00 AM
LastSecond959
LastSecond959 - avatar