Results 1 to 7 of 7
Thread: very new... Biginteger problems
 05012010, 10:39 AM #1Member
 Join Date
 Apr 2010
 Location
 london UK
 Posts
 53
 Rep Power
 0
SOLVED very new... Biginteger problems
Hello
I am very new to Java (this is my first java programming) and am puzzeling how to work with Big integers and i am not able to work it out
My problems reduced to simple statements are the following
Declaration
I need to declare a new BigInteger with a value of 1
Multiply a named biginteger by 3
Add one Biginteger to an other (someting like a = a+b but then for Big Integers)
That is all for the moment. (Can it be more simple?)
But even this is above my grasp of Java
I just don't seem to grasp object oriented programming
Can somebody help me work this out?Last edited by willemien; 05022010 at 03:18 PM. Reason: SOLVED
 05012010, 11:57 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
BigIntegers (as well as ordinary ints) are immutable things, e.g. you can't change 4 to 5 or whatever. Have a look at this example and read the API documentation for the BigInteger class:
Java Code:BigInteger one= BigInteger.ONE; // the class itself has a '1' object. BigInteger three= new BigInteger("3"); BigInteger a= three.add(one) // a= 4; BigInteger b= a.multiply(new BigInteger("5")); // b=20 BigInteger b= b.multiply(one.add(one)); // b= 40 BigInteger b= b.add(one).add(one); // b= 42 now
kind regards,
Jos
 05012010, 09:01 PM #3Member
 Join Date
 Apr 2010
 Location
 london UK
 Posts
 53
 Rep Power
 0
Thanks for your quick help
I guess this solves a bit of my troubles. But i don't understand all of it yet. The api information is not very clear to me.
When do i need new and when not. and what is the value of a and three after :
BigInteger three= new BigInteger("3");
BigInteger a= three.add(one) // a= 4;
BigInteger b= a.multiply(new BigInteger("5")); // b=20
It isn't at all clear to me.
What would help is can you can tell me if the following is correct?
I have a class power that contains the powers of 2 as longs and the powers of 3 as BigIntegers (3 ^50 is around 2^80 so it doesn't fit in a long integer)
And a class position that that has as private members 2 long integers white and black.
What i want is to have a function that results in a big integer that gives a single combined value of the long integers.
If bit in long white is set it cannot be set in black and v.v.
I want that the class position to be quick (but still very readable, so i know what i am doing)
Power class:
Java Code:package hippogo; import java.math.*; import static hippogo.constants.SIZE2; final class power { static private long[] powerofTwo = new long[SIZE2+1]; static private BigInteger[] powerofThree = new BigInteger[SIZE2+1]; static private BigInteger[] powerofThree2 = new BigInteger[SIZE2+1]; // fill private arrays static{ long v = 1; BigInteger v3 = BigInteger.ONE; BigInteger THREE = new BigInteger("3"); for(int i = 0; i <= 50; ++i) { powerofTwo[i] = v; // i ^ 2 powerofThree[i] = v3; // i ^ 3 powerofThree2[i] = v3.add(v3); // 2 x (i ^ 3) v = v*2; v3 = v3.multiply(THREE); } } private power(){throw new AssertionError();} // no objects; public final static long Two(int i){return powerofTwo[i];} public final static BigInteger Three(int i){return powerofThree[i];} public final static BigInteger Three2(int i){return powerofThree2[i];} }
Java Code:package hippogo; import java.math.*; import static hippogo.power.*; import static hippogo.constants.SIZE2; public class position { long black = 0; long white = 0; // constructor position (long a, long b){ black = a; white = b; } //... public BigInteger id(){ BigInteger r = BigInteger.ZERO; long t; for(int i = 1; i <= SIZE2+1; i++){ t = Two(i); if ((black & t) > 0) r = r.add(Three2(i)); else if ((white & t) > 0) r = r.add(Three(i)); } return r; } public String ids(){ BigInteger r = this.id(); return r.toString(); } }
In classes that use position.id do i need to import java.math.*; (I think so)
and import hippogo.power.* (I think not)
And in classes that use position.ids but not position.id? (I think not)
Am i correct that the power class can be imported as static? (it only has methods no objects)
Feel free to comment exhaustivly
and i hope i did not make to much mistakes.
Thanks again for the comments you allready madeLast edited by willemien; 05022010 at 12:14 AM. Reason: major code editing
 05022010, 07:55 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
 05022010, 11:07 AM #5Member
 Join Date
 Apr 2010
 Location
 london UK
 Posts
 53
 Rep Power
 0
sorry for my chaotic reply
the power class seem to work although i don't understand it.
To keep my questions to BigInteger:
When do i use new and when not?
why is it
BigInteger v3 = BigInteger.ONE;
and
BigInteger THREE = new BigInteger("3");
(The first without new the second with new)
if in another class have something like
position q = new position;
BigInteger a = q.id;
is that good or do i need to use
position q = new position;
BigInteger a = new q.id;
I guess in this class i need to import java.math.BigInteger
if i have in another class
position q = new position;
String s= q.ids
I don't need to import java.math.BigInteger
a final question is if my code is efficient, are there tricks to make it quicker. (without impeding readability)
the rest of my questions is not so about BigInteger so maybe better in another tread
 05022010, 11:34 AM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
The BigInteger object ONE already exists as a static member of the BigInteger class. There doesn't exist a THREE or Three object yet so you have to make one using the 'new' operator.
if in another class have something like
position q = new position;
BigInteger a = q.id;
is that good or do i need to use
position q = new position;
BigInteger a = new q.id;
I guess in this class i need to import java.math.BigInteger
if i have in another class
position q = new position;
String s= q.ids
I don't need to import java.math.BigInteger
a final question is if my code is efficient, are there tricks to make it quicker. (without impeding readability)
the rest of my questions is not so about BigInteger so maybe better in another tread
kind regards,
Jos
 05022010, 03:17 PM #7Member
 Join Date
 Apr 2010
 Location
 london UK
 Posts
 53
 Rep Power
 0
Similar Threads

How to convert a string into a BigInteger
By valery in forum New To JavaReplies: 4Last Post: 09132011, 01:32 PM 
BigInteger
By windows.login in forum New To JavaReplies: 8Last Post: 07132010, 01:10 PM 
My own BigInteger class. Need help.
By Dinde in forum New To JavaReplies: 2Last Post: 01272010, 09:49 PM 
how to convert from BigInteger to Hex
By nanaji in forum Advanced JavaReplies: 10Last Post: 05222008, 12:44 PM 
BigInteger remainder results in zero
By perito in forum New To JavaReplies: 1Last Post: 03212008, 05:07 PM
Bookmarks