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

前端 未结 13 894
春和景丽
春和景丽 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 02:54

    If a number n is not a prime, it can be factored into two factors a and b:

    n = a * b
    

    Now a and b can't be both greater than the square root of n, since then the product a * b would be greater than sqrt(n) * sqrt(n) = n. So in any factorization of n, at least one of the factors must be smaller than the square root of n, and if we can't find any factors less than or equal to the square root, n must be a prime.

提交回复
热议问题