Java BigInteger素数 [英] Java BigInteger prime numbers

查看:621
本文介绍了Java BigInteger素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一个BigInteger类型的随机素数,它介于我提供的最小值和最大值之间。

I am trying to generate a random prime number of type BigInteger, that is between a min and max value which I supply.

我知道BigInteger。 probablePrime(int bitlength,random),但我不确定比特长度是如何转换为输出素数的最大值/最小值。

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.

谢谢,
Steven1350

Thanks, Steven1350

推荐答案

BigInteger.probablePrime(bitLength,random)是将返回指定位长的(可能的)素数。这转换为最大值2 ^ bitlength - 1和最小值2 ^(bitlength - 1)。尽管我讨厌它作为答案,但它可能是你最好的选择,除非你想开始深入研究数论。

BigInteger.probablePrime(bitLength, random) is going to return a (probable) prime of the specified bit length. That translates into a maximum value of 2^bitlength - 1 and a minimum of 2^(bitlength - 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.

你需要做的就是弄清楚你的范围要求的比特长度,然后将它们传递给 probablePrime(),直到你得到一个在正确范围内的素数。

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.

这篇关于Java BigInteger素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆