Why do we check up to the square root of a prime number to determine if it is prime?

前端 未结 13 895
春和景丽
春和景丽 2020-11-22 02:24

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?

13条回答
  •  抹茶落季
    2020-11-22 03:07

    Let's suppose that the given integer N is not prime,

    Then N can be factorized into two factors a and b , 2 <= a, b < N such that N = a*b. Clearly, both of them can't be greater than sqrt(N) simultaneously.

    Let us assume without loss of generality that a is smaller.

    Now, if you could not find any divisor of N belonging in the range [2, sqrt(N)], what does that mean?

    This means that N does not have any divisor in [2, a] as a <= sqrt(N).

    Therefore, a = 1 and b = n and hence By definition, N is prime.

    ...

    Further reading if you are not satisfied:

    Many different combinations of (a, b) may be possible. Let's say they are:

    (a1, b1), (a2, b2), (a3, b3), ..... , (ak, bk). Without loss of generality, assume ai < bi, 1<= i <=k.

    Now, to be able to show that N is not prime it is sufficient to show that none of ai can be factorized further. And we also know that ai <= sqrt(N) and thus you need to check till sqrt(N) which will cover all ai. And hence you will be able to conclude whether or not N is prime.

    ...

提交回复
热议问题