且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

素数在Java中 - 算法

更新时间:2023-02-26 17:25:43

你可以做的就是找​​到 Tn的最低的约数。假如是 P ,再次找到最低的除数 TN / P> / code>等等。

What you can do is find the lowest divisor of Tn. Suppose that is p, find the lowest divisor again for Tn/p and so on.

现在,每走一步 P 为素数[以下说明。因此,收集他们,他们是 Tn的的素因子。

Now, at every step p is prime[explanation below]. So collect them and they are the prime divisors of Tn.

有关更好的时间复杂度,可以以检查约数高达 CEIL(开方(TN))只,而不是 TN-I

For better time-complexity, you can check for divisors up to upto ceil(sqrt(Tn)) only, instead of Tn-1.

而当你开始检查素数为 Tn的,你可以用 2 启动。而一旦你得到一个素因子 P 不要再从 2 开始的 TN / P 。因为, TN / P> / code>也是 Tn的的除数,自 Tn的没有除数小于 P TN / P> / code>不具备这一点。因此,开始与 P 再次[ P 可以有多个功率 Tn的。如果 P 不分 Tn的,移动到 P + 1

And when you start checking for prime divisor for Tn, you can start with 2. And once you get a prime divisor p don't start again from 2 for Tn/p. Because, Tn/p is also a divisor of Tn and since Tn does not have divisors less than p, Tn/p does not have it too. So start with p again[p can have multiple power in Tn]. If p does not divide Tn, move to p+1.

例如:

Tn的= 45
1.先从2.2不把45
2.下一个考验是3。45整除3 SO 3是它的一个素因子。
3.现在检查素因子从45/3 = 15,但重新开始与3不2。 4.好了,15是被3整除所以先从15/3 = 5 5.注意事项5,CEIL(开方(5))为3。5,但不能整除3。但是,由于4> CEIL(开方(5)) 我们可以说5是毫无疑问的黄金。

Tn = 45
1. start with 2. 2 does not divides 45.
2. next test is for 3. 45 is divisible by 3. So 3 is a prime divisor of it.
3. Now check prime divisors from 45/3 = 15, but start with 3. not from 2 again. 4. Well, 15 is divisible by 3. So start with 15/3 = 5 5. Note for 5, ceil(sqrt(5)) is 3. But 5 is not divisible by 3. But since 4 > ceil(sqrt(5)) and we can say 5 is a prime without any doubt.

所以45的主要因子为3和5。

So the prime divisor of 45 are 3 and 5.

的一个数为什么最小约数(除1)是素?

Why smallest divisor(except 1) of a number is a prime ?

假设上面的说法是错误的。然后,数N有一个最小的尚未复合因子,例如c。

Suppose above statement is false. Then a number N has a smallest yet composite divisor, say C.

所以C | N 现在C是复合所以,它具有除数比本身,但大于1。
例如c这样的除数是P.
因此P | C,但我们有C | N =>点| N,其中1< P< C.

So C|N Now C is composite so, it has divisor less than itself but greater than one.
Say such a divisor of C is P.
So P|C , but we have C|N => P|N where 1 < P < C.

这与我们的假设,即C是N的最小公约数,所以一些最小公约数始终是一个素数。

This contradicts our assumption that C is the smallest divisor of N, so smallest divisors of a number is always a prime.