Prime number test | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Prime number test

Let me know a better way to check whether a number is prime or not. https://code.sololearn.com/cgCjY1UDVzWO/?ref=app

16th May 2019, 9:06 AM
Chetan Satpute
Chetan Satpute - avatar
2 Answers
+ 9
You don't need to check all the way up to x. If x isn't divisible by any number up to sqrt(x)+1, it is a prime number. Also, you don't need to check every single possible divisor. You can check if x is divisible by 2 or 3 and then go from 3 up to sqrt(x) in steps of 2.
16th May 2019, 9:43 AM
Anna
Anna - avatar