为什么我们要检查一个数的平方根来确定该数是否是素数?
为了测试一个数是否是素数,为什么我们必须测试它是否只能被该数的平方根整除?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
如果数字
n
不是素数,它可以分解为两个因子a
和b
:现在
a
和b
不能同时大于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 factorsa
andb
:Now
a
andb
can't be both greater than the square root ofn
, since then the producta * b
would be greater thansqrt(n) * sqrt(n) = n
. So in any factorization ofn
, at least one of the factors must be less than or equal to the square root ofn
, and if we can't find any factors less than or equal to the square root,n
must be a prime.假设
m = sqrt(n)
,然后m × m = n
。现在,如果n
不是素数,则n
可以写成n = a × b
,因此m × m = a × b。请注意,
m
是实数,而n
、a
和b
是自然数。现在可以有3种情况:
在所有 3 种情况下,
min(a, b) ≤ m
。因此,如果我们搜索到m
,我们一定会找到n
的至少一个因子,这足以证明n
不是素数。Let's say
m = sqrt(n)
thenm × m = n
. Now ifn
is not a prime thenn
can be written asn = a × b
, som × m = a × b
. Notice thatm
is a real number whereasn
,a
andb
are natural numbers.Now there can be 3 cases:
In all 3 cases,
min(a, b) ≤ m
. Hence if we search tillm
, we are bound to find at least one factor ofn
, which is enough to show thatn
is not prime.因为如果一个因子大于 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.
假设 n 不是质数(大于 1)。因此,存在数字
a
和b
,通过将关系
a<=b
乘以a
和b
我们得到:因此:(注意
n=ab
)因此:(注意
a
和b
是正数)因此,如果一个数字(大于 1)不是素数,并且我们测试该数字的平方根的整除性,我们将找到其中一个因子。
Suppose
n
is not a prime number (greater than 1). So there are numbersa
andb
such thatBy multiplying the relation
a<=b
bya
andb
we get:Therefore: (note that
n=ab
)Hence: (Note that
a
andb
are positive)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.
这实际上只是因式分解和平方根的基本用法。
它可能看起来很抽象,但实际上它只是基于这样一个事实:非素数的最大可能阶乘必须是其平方根,因为:
sqrroot(n) * sqrroot(n) = n
。鉴于此,如果任何大于
1
且小于或等于sqrroot(n)
的整数均分为 < em>n
,则n
不能是素数。伪代码示例:
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 tosqrroot(n)
divides evenly inton
, thenn
cannot be a prime number.Pseudo-code example:
假设给定的整数
N
不是素数,那么 N 可以分解为两个因子
a
和b
,2
2
2
使得2
。 =a,b< NN = a*b
。显然,它们不能同时大于
sqrt(N)
。不失一般性,我们假设 a 较小。
现在,如果您找不到属于
[2, sqrt(N)]
范围内的N
的任何约数,这意味着什么?这意味着
N
在[2, a]
中没有任何除数,如a <= sqrt(N)
。因此,
a = 1
和b = n
,因此根据定义,N
是质数。...
如果您不满意,请进一步阅读:
(a, b)
的许多不同组合都是可能的。假设它们是:(a1, b1), (a2, b2), ( a3, b3), ..... , (ak, bk).不失一般性,假设 ai < bi,
1<= 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
andb
,2 <= a, b < N
such thatN = 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]
asa <= sqrt(N)
.Therefore,
a = 1
andb = 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 tillsqrt(N)
which will cover all ai. And hence you will be able to conclude whether or notN
is prime....
所以要检查一个数N是否是质数。
我们只需要检查 N 是否可以被数字<=SQROOT(N)整除。这是因为,如果我们将 N 分解为任意 2 个因子,即 X 和 Y,即。 N=XY。
X 和 Y 中的每一个都不能小于 SQROOT(N),因为这样,XY <氮
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, 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).
假设我们有一个数字“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.
给定任何数字
n
,找到其因子的一种方法是求其平方根p
:当然,如果我们将
p
乘以它本身,然后我们得到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 rootp
:Of course, if we multiply
p
by itself, then we get backn
:It can be re-written as:
Where
p = a = b
. Ifa
increases, thenb
decreases to maintaina*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, thenn
would not be a prime. So,p
could be a real number (ie, with fractions). And instead of going through the whole range ofn
, now we only need to go through the whole range ofp
. The otherp
is a mirror copy so in effect we halve the range. And then, now I am seeing that we can actually continue re-doing thesquare root
and doing it top
to further half the range.令 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.
任何合数都是素数的乘积。
假设
n = p1 * p2
,其中p2 > p1
并且它们是素数。如果
n % p1 === 0
则 n 是一个合数。如果
n % p2 === 0
,那么也猜一下n % p1 === 0
!所以不可能同时 if
n % p2 === 0
但n % p1 !== 0
。换句话说,如果合数n可以被整除
p2,p3...pi(它的较大因子)也必须除以它的最小因子p1。
事实证明,最低因子
p1 <= Math.square(n)
始终为真。Any composite number is a product of primes.
Let say
n = p1 * p2
, wherep2 > p1
and they are primes.If
n % p1 === 0
then n is a composite number.If
n % p2 === 0
then guess whatn % p1 === 0
as well!So there is no way that if
n % p2 === 0
butn % 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.是的,正如上面正确解释的那样,迭代到数字平方根的 Math.floor 来检查其素数就足够了(因为
sqrt
涵盖了所有可能的除法情况;而Math. Floor
,因为任何高于sqrt
的整数都已经超出了其范围)。这是一个可运行的 JavaScript 代码片段,它代表了这种方法的简单实现 - 并且它的“运行时友好性”足以处理相当大的数字(我尝试检查素数和非素数10**12,即1万亿,与 质数在线数据库,即使在我的廉价手机上也没有遇到错误或滞后):
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; andMath.floor
, because any integer abovesqrt
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):
要测试数字 n 的素性,首先需要一个如下所示的循环:
上面的循环的作用是:对于给定的 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 :
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).