Is_Prime function


There is no need to test the numbers up to x-1. It is sufficient to test up to the sqrt(x). Figure out why!


Hey Ruok987,

It might be helpful to people if you provided them with a hint as to why that's sufficient :wink:


I will reply by an example that should clarify the idea: Suppose you check if 10 is prime. You divide by 2, the result is 5. There is no need to check if it is divisible by 5. If you generalize this example you conclude that it is sufficient to check up to the sqrt( x ). I hope this helps.