Results 1 to 7 of 7
  1. #1
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Question 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

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

    kind regards,

    Jos

  3. #3
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default

    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];}
        
    }
    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)
                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();
         }
    }
    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.

    Thanks again for the comments you allready made
    Last edited by willemien; 05-02-2010 at 12:14 AM. Reason: major code editing

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by willemien View Post
    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

  5. #5
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default

    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

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by willemien View Post
    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

  7. #7
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

Similar Threads

  1. How to convert a string into a BigInteger
    By valery in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 01:32 PM
  2. BigInteger
    By windows.login in forum New To Java
    Replies: 8
    Last Post: 07-13-2010, 01:10 PM
  3. My own BigInteger class. Need help.
    By Dinde in forum New To Java
    Replies: 2
    Last Post: 01-27-2010, 08:49 PM
  4. how to convert from BigInteger to Hex
    By nanaji in forum Advanced Java
    Replies: 10
    Last Post: 05-22-2008, 12:44 PM
  5. BigInteger remainder results in zero
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •