+ 2
How to resolve execution time out????
I've to find 3 largest 9 digit palindrome prime number.I tried writing it in multiple ways.At last this is the code with minimum possible iteration I was able to come up with.But when I run the code still I'm getting the same execution timed out.Is there any other way to write this code?Can anyone help! https://code.sololearn.com/cx7UGmUfnTSd/?ref=app
2 Antworten
+ 2
Just add "return 0" or "break" after this "f = 0". Then you stop this for loop. Otherwise you will execute this loop for each number between 1 and your 9 digit input, what's a nightmare performance killer.
+ 2
Your prime number check iterates first over each number (!) though you should really stop calculating its factors, if you've found one! return inside the for loop or break to stop:
for(int i=1;i<=Math.sqrt(n);i++)
{
if(n%i==0)
f=0;
}