1

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.

Anurag Jain
  • 111
  • 2

4 Answers4

3

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 ton (otherwise their product would be greater than n), so it is sufficient to check for all the primes less than or equal ton.

0

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.

Shubham
  • 2,847
  • 4
  • 24
  • 37
  • Upvoted, but your argument can be simpler -- k has at least one prime factor p, and p | k | n, so p divides n. [You also need to check prime numbers <= sqrt(n) rather than < sqrt(n) to catch the cases when n is the square of a prime]. – Paul Hankin Feb 25 '17 at 13:35
  • @PaulHankin Thanks for pointing out the missing `equals` in last argument. And I agree that my argument is a bit verbose but I think the crust of both the argument (yours and mine) is same. Shall I trim down my answer? – Shubham Feb 25 '17 at 14:24
  • Yes, I agree it's the same idea. I think it'd make a better answer if you edited it down since that'd make the idea easier to see. But up to you if you want to do that. – Paul Hankin Feb 25 '17 at 14:28
0

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)

pseudo_teetotaler
  • 1,485
  • 1
  • 15
  • 35
0

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

Weijie Sun
  • 194
  • 6