The shown algorithm checks for every integer between 2 and sqrt(n) if n is divisible by it.
If n was divisible by a number greater than sqrt(n), say a
, then there would be a factor b
so that a * b = n
and b < a
. In this case the algorithm will find b "first" and see that n is not prime.
Therefore it is not necessary to check any number > sqrt(n).