我在 Java玩.,想看到我能做些什么。 我的理解是,只要我的计算机有足够的记忆来持有这样的数字,大脑能够保持一定的规模?
我的问题是BigInteger。 pow 只接受一只 in子,而不是另一个大脑,这意味着我只能使用多达2 147 483 647个大体。 是否可能使用大负债类?
BigInteger.pow(BigInteger)
感谢。
我在 Java玩.,想看到我能做些什么。 我的理解是,只要我的计算机有足够的记忆来持有这样的数字,大脑能够保持一定的规模?
我的问题是BigInteger。 pow 只接受一只 in子,而不是另一个大脑,这意味着我只能使用多达2 147 483 647个大体。 是否可能使用大负债类?
BigInteger.pow(BigInteger)
感谢。
http://en.wikipedia.org/wiki/Exponentiation_by_squaring”rel=“noretinger”>repeated squaring :
BigInteger pow(BigInteger base, BigInteger exponent) {
BigInteger result = BigInteger.ONE;
while (exponent.signum() > 0) {
if (exponent.testBit(0)) result = result.multiply(base);
base = base.multiply(base);
exponent = exponent.shiftRight(1);
}
return result;
}
可能不会为消极基础或权宜之计而工作。
您只能在Java上通过模块式算法做到这一点,意思是:你可以做<><>>>>>> ab mod c,a,b,c> > 。
采用:
BigInteger modPow(BigInteger exponent, BigInteger m)
BigInteger的基本实施限于(2^31-1),即32比值。 接近2°36轨道。 你们将需要8 GB的记忆来储存它,并需要many倍的记忆,以便开展与String(String)这样的行动。
BTW:你永远无法读到这样的数字。 如果你试图将其印成册,就需要一生时间阅读。
请了解以前的答复和评论,并理解为什么不应在生产水平应用上尝试这样做。 下面是一份只能用于测试的工作解决办法:
BigInteger pow(BigInteger base, BigInteger exponent) {
BigInteger result = BigInteger.ONE;
for (BigInteger i = BigInteger.ZERO; i.compareTo(exponent) != 0; i = i.add(BigInteger.ONE)) {
result = result.multiply(base);
}
return result;
}
这将有利于积极和消极的基础。 您不妨处理0至。 根据你们的需要,因为从技术上来说,这并不明确。
BigDecimal allIntegersPow(BigInteger base, BigInteger exponent) {
if (BigInteger.ZERO.compareTo(exponent) > 0) {
return BigDecimal.ONE.divide(new BigDecimal(pow(base, exponent.negate())), 2, RoundingMode.HALF_UP);
}
return new BigDecimal(pow(base, exponent));
}
这重新使用了第一种方法,即把2个小数点的比戈麦尔重新归来,你可以按照你的需求确定比额表和环绕模式。
同样,在现实生活中,在生产层面,你不应这样做。
java获胜,请你做大白。 Pow(BigInteger)但是,你只能把它放在一只 lo子里,看一片 Ar子被扔出,或发现由于失去记忆而出现其他一些错误。
I can suggest you make use of BigInteger modPow(BigInteger exponent, BigInteger m)
附录一
• 选择一个大型的P >>>X^Y和 do Z = X.modPow(Y,P);
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...