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

前端 未结 13 927
春和景丽
春和景丽 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:14

    Let's say m = sqrt(n) then m × m = n. Now if n is not a prime then n can be written as n = a × b, so m × m = a × b. Notice that m is a real number whereas n, a and b are natural numbers.

    Now there can be 3 cases:

    1. a > m ⇒ b < m
    2. a = m ⇒ b = m
    3. a < m ⇒ b > m

    In all 3 cases, min(a, b) ≤ m. Hence if we search till m, we are bound to find at least one factor of n, which is enough to show that n is not prime.

提交回复
热议问题