To test whether a number is prime or not, why do we have to test whether it is divisible only up to the square root of that number?
It's all really just basic uses of Factorization and Square Roots.
It may appear to be abstract, but in reality it simply lies with the fact that a non-prime-number's maximum possible factorial would have to be its square root because:
sqrroot(n) * sqrroot(n) = n
.
Given that, if any whole number above 1
and below or up to sqrroot(n)
divides evenly into n
, then n
cannot be a prime number.
Pseudo-code example:
i = 2;
is_prime = true;
while loop (i <= sqrroot(n))
{
if (n % i == 0)
{
is_prime = false;
exit while;
}
++i;
}