简体   繁体   English

Java BigInteger素数

[英]Java BigInteger prime numbers

I am trying to generate a random prime number of type BigInteger, that is between a min and max value which I supply. 我正在尝试生成BigInteger类型的随机素数,即我提供的最小值和最大值之间。

I am aware of the BigInteger.probablePrime(int bitlength, random), but I am not sure how or even if the bitlength translates into a max/min value of the outputted prime. 我知道BigInteger.probablePrime(int bitlength,random),但我不确定比特长度是如何转换为输出素数的最大值/最小值。

Thanks, Steven1350 谢谢,Steven1350

jprete's answer is fine if your ratio max/min is not close to 1. 如果您的比率max / min不接近1,则jprete的答案很好。

If you have a narrow range, your best bet is probably just to do something like the following: 如果你有一个狭窄的范围,你最好的选择可能只是做以下事情:

// this is pseudocode:
//
// round min down to multiple of 6, max up to multiple of 6
min6 = floor(min/6);
max6 = ceil(max/6);
maybePrimeModuli = [1,5];
do 
{
   b = generateRandom(maybePrimeModuli.length);
   // generate a random offset modulo 6 which could be prime
   x = 6*(min6 + generateRandom(max6-min6)) + b;
   // generate a random number which is congruent to b modulo 6
   // from 6*min6 to 6*max6-1
   // (of the form 6k+1 or 6k+5)

   // the other choices 6k, 6k+2, 6k+3, 6k+4 are composite 
} while not isProbablePrime(x);

The density of primes is fairly high overall, it's basically 1 in log(x), so you shouldn't have to repeat too many times to find a prime number. 素数密度总体上相当高,基本上是log(x)中的1,所以你不必重复太多次来找到素数。 (just as an example: for numbers around 10 23 , one in every 52 integers on average is a prime number. The above code only bothers with 2 out of every 6 numbers, so you'd end up looping an average of 17 times for numbers around 10 23 .) (仅作为一个例子:对于10 23左右的数字,平均每52个整数中就有一个是素数。上面的代码只有6个数字中的2个,所以你最终会平均循环17次数字大约10 23。

Just make sure you have a good primality test, and the Java BigInteger has one. 只要确保你有一个很好的素性测试,Java BigInteger就有一个。

As an exercise for the reader, extend the above function so it filters out more composite numbers ahead of time by using 30k + x (modulo 30, there are 22 moduli that are always composite, only 8 remaining that could be prime), or 210k + x. 作为读者的练习,扩展上述功能,以便通过使用30k + x(模30,有22个总是复合的模数,只剩下8个可能是素数)或者210k,提前过滤出更多的复合数。 + x。

edit: see also US patent #7149763 (OMFG!!!) 编辑:另见美国专利#7149763 (OMFG !!!)

BigInteger.probablePrime(bitLength, random) is going to return a (probable) prime of the specified bit length. BigInteger.probablePrime(bitLength, random)将返回指定位长度的(可能)素数。 That translates into a maximum value of 2^bitlength - 1 and a minimum of 2^(bitlength - 1). 这转换为最大值2 ^比特长度-1和最小值2 ^(比特长度-1)。 As much as I hate it as an answer, it's probably your best bet unless you want to start delving into number theory. 尽管我讨厌它作为答案,但它可能是你最好的选择,除非你想开始钻研数论。

What you would have to do is figure out the bit lengths that your range calls for, then pass them to probablePrime() until you get back a prime that's in the right range. 你需要做的是找出你的范围所要求的位长,然后将它们传递给probablePrime()直到你找回一个在正确范围内的素数。

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

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