+ 1

Any way to make this program better?

I've recently finished the python course and this is my first "real" program. Does anyone have an idea to make it faster/shorter? Thanks in advance. https://code.sololearn.com/crj1BZ2fGb4U/?ref=app

29th May 2017, 9:03 PM
Edgar Badalyan
Edgar Badalyan - avatar
4 Respuestas
+ 7
The code is a bit redundant sometimes, as the only one thing you need is n variable which you can convert to int after checking for input validity. Cleaning the code will make it easier for you to maintain and devlop it in the future. But that does not impact the speed too much. What is more relevant here is that it will suffice to check up divisors range from 2 to sqrt(n), no need to check any further, if not found. That will speed things up, especially for really large numbers. Since sqrt() is a math module method, you can replace it with n**0.5 So the valid range would be range(3, int(n**.5)+1, 2)
29th May 2017, 9:10 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
Nice idea, thanks. I'll update the code.
29th May 2017, 9:14 PM
Edgar Badalyan
Edgar Badalyan - avatar
+ 1
It isn't working properly. it says 35 is a prime number. 35 is a composite number. not a prime
29th May 2017, 11:53 PM
LordHill
LordHill - avatar
+ 1
Thanks for pointing it out. Corrected it! The code Sould work now.
30th May 2017, 4:55 AM
Edgar Badalyan
Edgar Badalyan - avatar