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


当前回答

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

其他回答

如果一个数n不是质数,它可以被分解成两个因子a和b:

n = a * b

现在a和b不可能都大于根号n,因为这样a * b就会大于根号n *根号n = n,所以在n的任何因式分解中,至少有一个因子必须小于根号n,如果我们找不到任何小于或等于根号的因子,n一定是质数。

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

则N可分解为a和b两个因子,2 <= a, b < N使N = a*b。 显然,它们不能同时大于根号N。

让我们不失一般性地假设a更小。

现在,如果你找不到N的任何除数在[2,根号(N)]范围内,这意味着什么?

这意味着当N <=√(N)时,N在[2,a]中没有任何除数。

因此,a = 1且b = n,因此根据定义,n是素数。

...

如果您不满意,请继续阅读:

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

(a1, b1), (a2, b2), (a3, b3), ....., (ak, bk)。在不失一般性的前提下,假设ai < bi, 1<= i <=k。

现在,为了证明N不是质数它足以证明ai都不能被进一步分解。我们还知道ai <=根号N,因此你需要检查根号N,这将涵盖所有ai。这样你就能得出N是不是质数。

...

是的,正如上面所解释的,迭代到Math就足够了。对一个数的平方根取底,以检查其质数(因为SQRT涵盖了所有可能的除法情况;和数学。因为任何高于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>

因为如果一个因子大于根号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.