It's known that to check whether a number 'n' is prime or not we just need to check whether it has a factor less than the square root of n.
My question is isn't it is sufficient to check for all primes less than the square root of n.
It's known that to check whether a number 'n' is prime or not we just need to check whether it has a factor less than the square root of n.
My question is isn't it is sufficient to check for all primes less than the square root of n.
Every integer n greater than 1 either is prime itself or is the product of prime numbers (Fundamental theorem of arithmetic). Thus if n isn't a prime itself it must be divisible by at least two primes. At least one of these must be less than or equal to √n (otherwise their product would be greater than n), so it is sufficient to check for all the primes less than or equal to √n.
We can argue like this:
Let the number to check be n
. Let there be a number k <= sqrt(n)
which divides n
. Now, we can write k
as follows:
k = (p_1^a_1)(p_2^a_2)...(p_x^a_x)
where p_1, p_2, ..., p_x
are prime number less than or equal to k
and a_1, a_2, ..., a_x >= 1
. Now, since k
divides n
, and since we know that p_1, p_2, ..., p_x
divides k
, by transitivity, we can deduce that p_1, p_2, ..., p_x
divides n
. Thus, to prove that n
is non-prime, it is sufficient to check if any of the prime numbers <= sqrt(n)
divides n
or not.
Yes, what you are saying is absolutely correct.
You just need to check for all the primes less than equal to squareRoot(n)
, but the point is you don't know whether the number is prime or not. So, you traverse till squareRoot(n)
Yes it is only need to check the factor less sqrt(n). But this algorithm is a little bit slow. I have another better algorithm called Miller_Rabin_primality My previous project code Source code
Here is the Wiki