# Thread: very new... Biginteger problems

1. Member 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; 05-02-2010 at 03:18 PM. Reason: SOLVED  Reply With Quote

2. ## 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
Does this help?

kind regards,

Jos  Reply With Quote

3. Member Join Date
Apr 2010
Location
london UK
Posts
53
Rep Power
0

## 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];}

}```
position:
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)
else if ((white & t) > 0)
}
return r;
}

public String ids(){
BigInteger r = this.id();
return r.toString();
}
}```
More questions (I am a bit brainpicking)

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.

Last edited by willemien; 05-02-2010 at 12:14 AM. Reason: major code editing  Reply With Quote

4. ##  Originally Posted by willemien Feel free to comment exhaustivly
I find your questions extremely confusing at best and I don't understand one iota from them. Please try to be clear and ask one question at a time ...

kind regards,

Jos  Reply With Quote

5. Member Join Date
Apr 2010
Location
london UK
Posts
53
Rep Power
0

## 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  Reply With Quote

6. ##  Originally Posted by willemien 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)
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;
As above: you only have to 'new' a new object if there isn't such an object already. The q.id object already exists (I guess) so there is no reason to make a new one.

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
If q.ids is not a BigInteger then there is no need to import that class: only import what you need.

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
First try to make it all run correctly; don't think of any optimization yet.

kind regards,

Jos  Reply With Quote

7. Member Join Date
Apr 2010
Location
london UK
Posts
53
Rep Power
0

##   Reply With Quote