Is there a way using BigInteger for iteration?

798 Views Asked by At

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?

3

There are 3 best solutions below

0
On

You can use the compareTo method.

4
On

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.

0
On

Java operators are only designed to only operate on primitive data types; they do not act on classes. Since BigInteger is a class, arithmetic comparisons and operations can only be done through class methods.