Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way using BigInteger for iteration?

Does BigInteger only have equals for comparison?
Are there substitutes for math notation like < (greater than) or < (less than)? ^Answered!

now i want to know, is there a way using BigInteger in iteration such as while and for?

like image 321
monkry Avatar asked Dec 14 '25 09:12

monkry


2 Answers

You can use the compareTo method.

like image 150
kgiannakakis Avatar answered Dec 17 '25 00:12

kgiannakakis


You can't use math notation, in fact I wouldn't get in the habit of using == either, I'm fairly sure that unless they used some serious trickery it will fail.

a = new BigInteger(500);

b = a;
if( a == b ) 
    will always be true

b=new BigInteger(500);
if( a == b )
    will never be true

if( a.equals(b) )
    will always work fine.

Java isn't a great language for this kind of stuff--I really love Java but ended up having a lot of trouble implementing a Complex class and then implementing a matrix that could hold and manipulate the complex class.

My solution was to use Java to create the core classes then use Groovy to create the classes that used the core classes. If you follow certain naming patterns, then you can use any operators on any class.

Or if you just want to mess with big numbers simply use groovy and don't even declare a type for your variables--it will automatically promote them to whatever you need.

like image 40
Bill K Avatar answered Dec 16 '25 23:12

Bill K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!