为什么我们要检查一个数的平方根来确定该数是否是素数?

发布于 2024-11-03 17:42:14 字数 44 浏览 0 评论 0原文

为了测试一个数是否是素数,为什么我们必须测试它是否只能被该数的平方根整除?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

醉南桥 2024-11-10 17:42:14

如果数字n不是素数,它可以分解为两个因子ab

n = a * b

现在ab 不能同时大于 n 的平方根,因为这样乘积 a * b 将大于 sqrt(n) * sqrt(n) = n。因此,在n的任何分解中,至少有一个因子必须小于或等于n的平方根,并且如果我们找不到任何小于或等于n的因子大于或等于平方根,n 必须是质数。

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 less than or equal to 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.

撧情箌佬 2024-11-10 17:42:14

假设m = sqrt(n),然后m × m = n。现在,如果 n 不是素数,则 n 可以写成 n = a × b,因此 m × m = a × b。请注意,m 是实数,而 nab 是自然数。

现在可以有3种情况:

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

在所有 3 种情况下,min(a, b) ≤ m。因此,如果我们搜索到m,我们一定会找到n的至少一个因子,这足以证明n不是素数。

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.

阳光①夏 2024-11-10 17:42:14

因为如果一个因子大于 n 的平方根,则与其相乘等于 n 的另一个因子必然小于 n 的平方根。

Because if a factor is greater than the square root of n, the other factor that would multiply with it to equal n is necessarily less than the square root of n.

来日方长 2024-11-10 17:42:14

假设 n 不是质数(大于 1)。因此,存在数字 ab

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

通过将关系 a<=b 乘以 ab 我们得到:

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

因此:(注意 n=ab

a^2 <= n <= b^2

因此:(注意 ab 是正数)

a <= sqrt(n) <= b

因此,如果一个数字(大于 1)不是素数,并且我们测试该数字的平方根的整除性,我们将找到其中一个因子。

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.

空心↖ 2024-11-10 17:42:14

这实际上只是因式分解和平方根的基本用法。

它可能看起来很抽象,但实际上它只是基于这样一个事实:非素数的最大可能阶乘必须是其平方根,因为:

sqrroot(n) * sqrroot(n) = n

鉴于此,如果任何大于 1 且小于或等于 sqrroot(n) 的整数均分为 < em>n,则 n 不能是素数。

伪代码示例:

i = 2;

is_prime = true;

while loop (i <= sqrroot(n))
{
  if (n % i == 0)
  {
    is_prime = false;
    exit while;
  }
  ++i;
}

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;
}
绻影浮沉 2024-11-10 17:42:14

假设给定的整数 N 不是素数,

那么 N 可以分解为两个因子 ab2 2 2 2 。 =a,b< N 使得 N = a*b
显然,它们不能同时大于 sqrt(N)

不失一般性,我们假设 a 较小。

现在,如果您找不到属于 [2, sqrt(N)] 范围内的 N 的任何约数,这意味着什么?

这意味着 N[2, a] 中没有任何除数,如 a <= sqrt(N)

因此,a = 1b = n,因此根据定义,N 是质数

...

如果您不满意,请进一步阅读

(a, b) 的许多不同组合都是可能的。假设它们是:

(a1, b1), (a2, b2), ( a3, b3), ..... , (ak, bk).不失一般性,假设 ai < bi1<= i<=k

现在,为了能够证明 N 不是素数,只要证明 i 中没有一个可以被进一步分解就足够了。我们还知道 ai <= sqrt(N) 因此您需要检查直到 sqrt(N) 这将涵盖所有一个i。因此,您将能够得出 N 是否是质数的结论。

...

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.

...

甜`诱少女 2024-11-10 17:42:14

所以要检查一个数N是否是质数。
我们只需要检查 N 是否可以被数字<=SQROOT(N)整除。这是因为,如果我们将 N 分解为任意 2 个因子,即 X 和 Y,即。 N=XY。
X 和 Y 中的每一个都不能小于 SQROOT(N),因为这样,X
Y <氮
X 和 Y 中的每一个都不能大于 SQROOT(N),因为这样,X*Y > SQROOT(N)。 N

因此,一个因子必须小于或等于 SQROOT(N) (而另一个因子大于或等于 SQROOT(N) )。
因此,要检查 N 是否为素数,我们只需检查那些 <= SQROOT(N) 的数字。

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, X
Y < 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).

野鹿林 2024-11-10 17:42:14

假设我们有一个数字“a”,它不是质数[非质数/合数意味着 - 一个可以被 1 或它本身以外的数字整除的数字。例如,6 可以被 2、或 3、以及 1 或 6 整除]。

6 = 1 × 6 或 6 = 2 × 3

所以现在如果“a”不是质数,那么它可以被另外两个数字整除,假设这些数字是“b”和“c”。这意味着

a=b*c。

现在,如果“b”或“c”,则它们中的任何一个都大于“a”的平方根,而不是“b”与“c”的乘积。 “c”将大于“a”。

因此,“b”或“c”总是<=“a”的平方根,以证明方程“a=b*c”。

由于上述原因,当我们测试一个数是否为素数时,我们只检查该数的平方根。

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.

番薯 2024-11-10 17:42:14

给定任何数字n,找到其因子的一种方法是求其平方根p

sqrt(n) = p

当然,如果我们将p乘以它本身,然后我们得到n

p*p = n

它可以重写为:

a*b = n

其中p = a = b。如果a增加,则b减少以维持a*b = n。因此,p 是上限。

更新:我今天再次重读了这个答案,它对我来说变得更加清晰了。值 p 不一定表示整数,因为如果是整数,则 n 就不是素数。因此,p 可以是实数(即带分数)。现在我们只需遍历 p 的整个范围,而不是遍历 n 的整个范围。另一个 p 是镜像副本,因此实际上我们将范围减半。然后,现在我发现我们实际上可以继续重新计算平方根,并对 p 进一步进行一半的范围。

Given any number n, then one way to find its factors is to get its square root p:

sqrt(n) = p

Of course, if we multiply p by itself, then we get back n:

p*p = n

It can be re-written as:

a*b = n

Where p = a = b. If a increases, then b decreases to maintain a*b = n. Therefore, p is the upper limit.

Update: I am re-reading this answer again today and it became clearer to me more. The value p does not necessarily mean an integer because if it is, then n would not be a prime. So, p could be a real number (ie, with fractions). And instead of going through the whole range of n, now we only need to go through the whole range of p. The other p is a mirror copy so in effect we halve the range. And then, now I am seeing that we can actually continue re-doing the square root and doing it to p to further half the range.

夜空下最亮的亮点 2024-11-10 17:42:14

令 n 为非素数。因此,它至少有两个大于 1 的整数因子。令 f 为 n 个这样的因子中最小的一个。假设f>开方那么 n/f 是一个 ≤ sqrt n 的整数,因此小于 f。因此,f 不可能是n 的最小因子。反证法; n 的最小因子必​​须 ≤ sqrt n。

Let n be non-prime. Therefore, it has at least two integer factors greater than 1. Let f be the smallest of n's such factors. Suppose f > sqrt n. Then n/f is an integer ≤ sqrt n, thus smaller than f. Therefore, f cannot be n's smallest factor. Reductio ad absurdum; n's smallest factor must be ≤ sqrt n.

罪歌 2024-11-10 17:42:14

任何合数都是素数的乘积。

假设n = p1 * p2,其中p2 > p1 并且它们是素数。

如果 n % p1 === 0n 是一个合数。

如果n % p2 === 0,那么也猜一下n % p1 === 0

所以不可能同时 if n % p2 === 0n % p1 !== 0
换句话说,如果合数n可以被整除
p2,p3...pi(它的较大因子)也必须除以它的最小因子p1
事实证明,最低因子 p1 <= Math.square(n) 始终为真。

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.

つ可否回来 2024-11-10 17:42:14

是的,正如上面正确解释的那样,迭代到数字平方根的 Math.floor 来检查其素数就足够了(因为 sqrt 涵盖了所有可能的除法情况;而 Math. Floor,因为任何高于 sqrt 的整数都已经超出了其范围)。

这是一个可运行的 JavaScript 代码片段,它代表了这种方法的简单实现 - 并且它的“运行时友好性”足以处理相当大的数字(我尝试检查素数和非素数10**12,即1万亿,与 质数在线数据库,即使在我的廉价手机上也没有遇到错误或滞后):

function isPrime(num) {
  if (num % 2 === 0 || num < 3 || !Number.isSafeInteger(num)) {
    return num === 2;
  } else {
    const sqrt = Math.floor(Math.sqrt(num));
    for (let i = 3; i <= sqrt; i += 2) {
      if (num % i === 0) return false;
    }
    return true;
  }
}
<label for="inp">Enter a number and click "Check!":</label><br>
<input type="number" id="inp"></input>
<button onclick="alert(isPrime(+document.getElementById('inp').value) ? 'Prime' : 'Not prime')" type="button">Check!</button>

Yes, as it was properly explained above, it's enough to iterate up to Math.floor of a number's square root to check its primality (because sqrt covers all possible cases of division; and Math.floor, because any integer above sqrt will already be beyond its range).

Here is a runnable JavaScript code snippet that represents a simple implementation of this approach – and its "runtime-friendliness" is good enough for handling pretty big numbers (I tried checking both prime and not prime numbers up to 10**12, i.e. 1 trillion, compared results with the online database of prime numbers and encountered no errors or lags even on my cheap phone):

function isPrime(num) {
  if (num % 2 === 0 || num < 3 || !Number.isSafeInteger(num)) {
    return num === 2;
  } else {
    const sqrt = Math.floor(Math.sqrt(num));
    for (let i = 3; i <= sqrt; i += 2) {
      if (num % i === 0) return false;
    }
    return true;
  }
}
<label for="inp">Enter a number and click "Check!":</label><br>
<input type="number" id="inp"></input>
<button onclick="alert(isPrime(+document.getElementById('inp').value) ? 'Prime' : 'Not prime')" type="button">Check!</button>

×眷恋的温暖 2024-11-10 17:42:14

要测试数字 n 的素性,首先需要一个如下所示的循环:

bool isPrime = true;
for(int i = 2; i < n; i++){
    if(n%i == 0){
        isPrime = false;
        break;
    }
}

上面的循环的作用是:对于给定的 1 1 1 1 1 1 1 我< n,它检查 n/i 是否为整数(余数为 0)。如果存在一个i,其中n/i是整数,那么我们可以确定n不是素数,此时循环终止。如果没有 i,n/i 是整数,则 n 是素数。

与每个算法一样,我们会问:我们能做得更好吗?

让我们看看上面的循环中发生了什么。

i 的序列为: i = 2, 3, 4, ... , n-1

整数检查的序列为: j = n/i,即 n/2, n/3, n/4, ... , n/(n-1)

如果对于某些 i = a,n/a 是整数,则 n/a = k(整数)

或 n = ak,显然 n > n k> 1(如果 k = 1,则 a = n,但 i 永远不会达到 n;如果 k = n,则 a = 1,但 i 从 2 开始)

此外,n/k = a,如上所述,a 是i 的值使得 n > a> 1.

因此,a 和 k 都是 1 到 n(不包括)之间的整数。因为,在某个迭代中 i = a,以及在其他迭代中 i = k,i 达到该范围内的每个整数。如果 n 的素性测试对于 min(a,k) 失败,那么对于 max(a,k) 也将失败。所以我们只需要检查这两种情况之一,除非 min(a,k) = max(a,k) (其中两个检查减少到一个)即 a = k ,此时 a*a = n,其中意味着 a = sqrt(n)。

换句话说,如果 n 的素性测试对于某些 i >= sqrt(n) (即 max(a,k))失败,那么对于某些 i <= n (即 min (a,k))。因此,如果我们运行 i = 2 到 sqrt(n) 的测试就足够了。

To test the primality of a number, n, one would expect a loop such as following in the first place :

bool isPrime = true;
for(int i = 2; i < n; i++){
    if(n%i == 0){
        isPrime = false;
        break;
    }
}

What the above loop does is this : for a given 1 < i < n, it checks if n/i is an integer (leaves remainder 0). If there exists an i for which n/i is an integer, then we can be sure that n is not a prime number, at which point the loop terminates. If for no i, n/i is an integer, then n is prime.

As with every algorithm, we ask : Can we do better ?

Let us see what is going on in the above loop.

The sequence of i goes : i = 2, 3, 4, ... , n-1

And the sequence of integer-checks goes : j = n/i, which is n/2, n/3, n/4, ... , n/(n-1)

If for some i = a, n/a is an integer, then n/a = k (integer)

or n = ak, clearly n > k > 1 (if k = 1, then a = n, but i never reaches n; and if k = n, then a = 1, but i starts form 2)

Also, n/k = a, and as stated above, a is a value of i so n > a > 1.

So, a and k are both integers between 1 and n (exclusive). Since, i reaches every integer in that range, at some iteration i = a, and at some other iteration i = k. If the primality test of n fails for min(a,k), it will also fail for max(a,k). So we need to check only one of these two cases, unless min(a,k) = max(a,k) (where two checks reduce to one) i.e., a = k , at which point a*a = n, which implies a = sqrt(n).

In other words, if the primality test of n were to fail for some i >= sqrt(n) (i.e., max(a,k)), then it would also fail for some i <= n (i.e., min(a,k)). So, it would suffice if we run the test for i = 2 to sqrt(n).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文