简体   繁体   English

Java BigInteger nextProbablePrime方法如何工作?

[英]How Java BigInteger nextProbablePrime method works?

I'm working with Java BigInteger Class and curious about the Algorithm behind nextProbablePrime method. 我正在使用Java BigInteger Class并对nextProbablePrime方法背后的算法感到好奇。 I know about some efficient primality testing algorithm like Miller-Rabin but not sure about which algorithm was implemented here. 我知道一些有效的素性测试算法,如Miller-Rabin但不确定这里实现了哪种算法。

Trying the following code for a good time and still no response. 尝试使用以下代码并且仍然没有响应。

BigInteger number = BigInteger.ZERO;
number = number.setBit(82589933);
number = number.nextProbablePrime();

I have gone through with the source code of BigInteger . 我已经完成了BigInteger 的源代码 It is internally using the MillerRabin algorithm for the nextProbablePrime method. 它在内部使用MillerRabin算法用于nextProbablePrime方法。

Why your example runs and runs without returning: 为什么您的示例运行并运行而不返回:

Your number is 82million bits long, and (by prime number th'm) such primes are ab out 82million / log_e(2) numbers apart. 你的电话号码是82million 长,和(通过素数th'm)这样的素数是AB出82million / log_e(2)数字分开。 So you're asking Miller-Rabin to test about 15million-ish candidates, where each candidate involves 82million bits, and each check is non-trivial. 因此,你要求Miller-Rabin测试大约1500万名候选人,其中每个候选人涉及8200万比特,每次检查都是非平凡的。 So yeah, even efficient algorithms like Miller-Rabin will take a while on such beyond-mind-bogglingly-big inputs. 所以是的,即使像米勒 - 拉宾这样的高效算法也需要花费一些时间来处理这些令人难以置信的超级大输入。

(I remember once running raising one number to another, having it take too long, and complaining to the language-developer that they should use repeated squaring for faster exponentiation ... before I stepped back and realized that my test-number also had millions of digits.) (我记得曾经把一个号码提到另一个号码,花了太长时间,并向语言开发者抱怨他们应该使用重复的平方来加快取幂...在我退回之前意识到我的测试号码也有数百万数字。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM