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

前端 未结 13 1097
春和景丽
春和景丽 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条回答
  •  旧时难觅i
    2020-11-22 03:03

    Suppose n is not a prime number (greater than 1). So there are numbers a and b such that

    n = ab      (1 < a <= b < n)
    

    By multiplying the relation a<=b by a and b we get:

    a^2 <= ab
     ab <= b^2
    

    Therefore: (note that n=ab)

    a^2 <= n <= b^2
    

    Hence: (Note that a and b are positive)

    a <= sqrt(n) <= b
    

    So if a number (greater than 1) is not prime and we test divisibility up to square root of the number, we will find one of the factors.

提交回复
热议问题