Time limit exceeded | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Time limit exceeded

I've got a code, whis is to find out if the number you entered is prime, but if I type 8, then the output is Time limit exceeded. Why is this happening? And how to correct it. Is it because of my phone (lenovo A319)? http://www.sololearn.com/app/csharp/playground/cr5bphXnQ6dg/ this is the code

18th Jun 2016, 9:49 AM
Mitiko
Mitiko - avatar
5 Answers
+ 3
Hi, still there? I just tried to rewrite your code in a way that it still looks similar to how you coded it, for you to easily understand its flow. If this work perfectly fine, just give me an Up vote, thanks! Hope this helps. -- UPDATED VERSION: using System; namespace IsPrime { class Program { static void Main(string[] args) { int num = int.Parse(Console.ReadLine()); Console.WriteLine(Prime(num)); } private static bool Prime(int num) { if (num > 1) { for (int i = 2; i < num; i++) { if (num % i == 0) return false; } return true; } else return false; } } }
18th Jun 2016, 11:40 AM
erwinmesi
erwinmesi - avatar
+ 1
When I try 8 it writes False and then an unhandled exception
18th Jun 2016, 12:58 PM
Mitiko
Mitiko - avatar
+ 1
I'm sorry, I forgot to remove the while(true) -- I updated the code, try it again now.
18th Jun 2016, 1:05 PM
erwinmesi
erwinmesi - avatar
+ 1
Yes, that works. But I still can't understand why I receive Time Limit exceeded
18th Jun 2016, 1:12 PM
Mitiko
Mitiko - avatar
0
Its because your not gay
15th Aug 2016, 11:17 AM
Chris Moore
Chris Moore - avatar