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

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

    ...

    0 讨论(0)
  • 2020-11-22 03:13

    It's all really just basic uses of Factorization and Square Roots.

    It may appear to be abstract, but in reality it simply lies with the fact that a non-prime-number's maximum possible factorial would have to be its square root because:

    sqrroot(n) * sqrroot(n) = n.

    Given that, if any whole number above 1 and below or up to sqrroot(n) divides evenly into n, then n cannot be a prime number.

    Pseudo-code example:

    i = 2;
    
    is_prime = true;
    
    while loop (i <= sqrroot(n))
    {
      if (n % i == 0)
      {
        is_prime = false;
        exit while;
      }
      ++i;
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 03:16

    So to check whether a number N is Prime or not. We need to only check if N is divisible by numbers<=SQROOT(N). This is because, if we factor N into any 2 factors say X and Y, ie. N=XY. Each of X and Y cannot be less than SQROOT(N) because then, XY < N Each of X and Y cannot be greater than SQROOT(N) because then, X*Y > N

    Therefore one factor must be less than or equal to SQROOT(N) ( while the other factor is greater than or equal to SQROOT(N) ). So to check if N is Prime we need only check those numbers <= SQROOT(N).

    0 讨论(0)
  • 2020-11-22 03:20

    Any composite number is a product of primes.

    Let say n = p1 * p2, where p2 > p1 and they are primes.

    If n % p1 === 0 then n is a composite number.

    If n % p2 === 0 then guess what n % p1 === 0 as well!

    So there is no way that if n % p2 === 0 but n % p1 !== 0 at the same time. In other words if a composite number n can be divided evenly by p2,p3...pi (its greater factor) it must be divided by its lowest factor p1 too. It turns out that the lowest factor p1 <= Math.square(n) is always true.

    0 讨论(0)
提交回复
热议问题