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

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

    Let's say we have a number "a", which is not prime [not prime/composite number means - a number which can be divided evenly by numbers other than 1 or itself. For example, 6 can be divided evenly by 2, or by 3, as well as by 1 or 6].

    6 = 1 × 6 or 6 = 2 × 3

    So now if "a" is not prime then it can be divided by two other numbers and let's say those numbers are "b" and "c". Which means

    a=b*c.

    Now if "b" or "c" , any of them is greater than square root of "a "than multiplication of "b" & "c" will be greater than "a".

    So, "b" or "c" is always <= square root of "a" to prove the equation "a=b*c".

    Because of the above reason, when we test if a number is prime or not, we only check until square root of that number.

提交回复
热议问题