BigInteger Modulo'%'操作和少于/多于运营 [英] BigInteger Modulo '%' Operation & Less Than / More Than Operations
问题描述
我有一个算法,需要对BigInt的算法进行操作.
Hi I have an algorithm in which I need to apply operations to BigInt's.
我了解可以使用Maths类来操纵BigInt,例如:
I understand that BigInt's can be manipulated using the Maths class such as:
import java.math.*;
BigInteger a;
BigInteger b = BigInteger.ZERO;
BigInteger c = BigInteger.ONE;
BigInteger d = new BigInteger ("3");
BigInteger e = BigInteger.valueOf(5);
a.multiply(b);
a.add(b);
a.substract(b);
a.divide(b);
我需要能够申请多于一段时间的条件,例如
I need to be able to apply greater than for a while condition e.g.
while (a > 0) {
这给了我一个语法错误,说二进制运算符'>'的错误操作数类型,第一种类型:java.math.BigInteger,第二种类型:int.
Which gives me a syntax error saying "bad operand types for binary operator '>', first type: java.math.BigInteger, second type: int.
我还需要能够将模(%)运算符应用于BigInteger.
I also need to be able to apply the modulo (%) operator to a BigInteger.
b = a % c;
有人可以建议这样做吗?
Can anyone suggest a way of doing this?
如果没有解决方案,那么我只需要使用reduce函数(不理想)就可以将BigInteger操纵成唯一的Long.
If there isn't a solution then I'm just going to have to somehow manipulate my BigInteger into an unique Long using a reduce function (which is far from ideal).
Silverzx.
推荐答案
To compare BigInteger
, use BigInteger.compareTo
.
while(a.compareTo(BigInteger.ZERO) > 0)
//...
And for modulo (%
), use BigInteger.mod
.
BigInteger blah = a.mod(b);
这篇关于BigInteger Modulo'%'操作和少于/多于运营的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!