要检验一个数是不是质数,为什么我们要检验它是否只能被这个数的平方根整除?
当前回答
对于任意数n,求因数的一种方法是求根号p:
sqrt(n) = p
当然,如果我们用p乘以它自己,就会得到n:
p*p = n
可以改写为:
a*b = n
其中p = a = b,如果a增加,则b减少,以保持a*b = n,因此p为上限。
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.
其他回答
为了检验一个数N是不是质数。 我们只需要检查N是否能被<=SQROOT(N)的数整除。这是因为,如果我们把N分解成任意两个因子比如X和Y。N = XY。 X和Y都不能小于SQROOT(N)因为XY < N X和Y都不能大于SQROOT(N)因为X*Y >n
因此,一个因子必须小于或等于SQROOT(N)(而另一个因子大于或等于SQROOT(N))。 因此,要检查N是否为质数,我们只需要检查那些<= SQROOT(N)的数字。
设n是非素数。因此,它至少有两个大于1的整数因子。设f是n个这样的因子中最小的。设f >√n,则n/f是一个≤√n的整数,因此小于f,因此f不可能是n的最小因子。反证法;N的最小因子必须≤根号N。
对于任意数n,求因数的一种方法是求根号p:
sqrt(n) = p
当然,如果我们用p乘以它自己,就会得到n:
p*p = n
可以改写为:
a*b = n
其中p = a = b,如果a增加,则b减少,以保持a*b = n,因此p为上限。
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.
其实就是基本的因式分解和平方根。
它可能看起来很抽象,但实际上它只是在于这样一个事实,即一个非质数的最大可能阶乘必须是它的平方根,因为:
sqrroot(n) * sqrroot(n) = n。
鉴于此,如果任何大于1且小于或大于√(n)的整数能被n整除,则n不可能是质数。
伪代码示例:
i = 2;
is_prime = true;
while loop (i <= sqrroot(n))
{
if (n % i == 0)
{
is_prime = false;
exit while;
}
++i;
}
假设我们有一个数字“a”,它不是质数[非质数/合数的意思是-一个可以被除1或它本身以外的数字整除的数字。例如,6可以被2或3整除,也可以被1或6整除。
6 = 1 × 6或6 = 2 × 3
如果a不是质数那么它可以被另外两个数除我们设这两个数是b和c。这意味着
a = b * c。
现在如果b或c,它们中的任何一个大于a的平方根那么b和c的乘积就会大于a。
因此,“b”或“c”总是<=“a”的平方根来证明方程“a=b*c”。
由于上述原因,当我们测试一个数字是否是质数时,我们只检查到该数字的平方根。