要检验一个数是不是质数,为什么我们要检验它是否只能被这个数的平方根整除?


当前回答

对于任意数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.

其他回答

假设m =根号n,那么m × m = n。如果n不是质数,那么n可以写成n = a × b,所以m × m = a × b。注意,m是实数,而n、a和b是自然数。

现在有三种情况:

A > m⇒b < m ⇒A = m, b = m A < m⇒b > m

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

设n是非素数。因此,它至少有两个大于1的整数因子。设f是n个这样的因子中最小的。设f >√n,则n/f是一个≤√n的整数,因此小于f,因此f不可能是n的最小因子。反证法;N的最小因子必须≤根号N。

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

假设n = p1 * p2,其中p2 > p1,它们都是质数。

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

如果n % p2 === 0,那么猜猜n % p1 === 0 !

因此,如果n % p2 === 0,同时n % p1 !== 0,这是不可能的。 换句话说,如果一个合数n能被 p2、p3……PI(较大因子)也要除以最小因子p1。 事实证明,最低因子p1 <= Math.square(n)总是正确的。

因为如果一个因子大于根号n,那么与它相乘等于n的另一个因子必然小于根号n。

假设n不是一个质数(大于1),那么有数字a和b满足

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

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

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

因此:(注意n=ab)

a^2 <= n <= b^2

因此:(注意a和b是正的)

a <= sqrt(n) <= b

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