BigInteger Modulo'%'操作和少于/多于运营 [英] BigInteger Modulo '%' Operation & Less Than / More Than Operations

查看:73
本文介绍了BigInteger Modulo'%'操作和少于/多于运营的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个算法,需要对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.

推荐答案

要比较BigInteger,请使用

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屋!

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