java.math.BigInteger

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shana07
    Contributor
    • Jan 2007
    • 280

    java.math.BigInteger

    I don't understand about this program is doing. Could someone please tell me what it is about. Because I have gcd function in one program that calls this function. Thanks a lot
    Code:
     
    switch (opcode) {
    case GCD:       out = b1.gcd(b2).toString();
                                break;
    Code:
    /**
         * Returns a BigInteger whose value is the greatest common divisor of
         * <tt>abs(this)</tt> and <tt>abs(val)</tt>.  Returns 0 if
         * <tt>this==0 &amp;&amp; val==0</tt>.
         *
         * @param  val value with with the GCD is to be computed.
         * @return <tt>GCD(abs(this), abs(val))</tt>
         */
        public BigInteger gcd(BigInteger val) {
            if (val.signum == 0)
    	    return this.abs();
    	else if (this.signum == 0)
    	    return val.abs();
    
            MutableBigInteger a = new MutableBigInteger(this);
            MutableBigInteger b = new MutableBigInteger(val);
    
            MutableBigInteger result = a.hybridGCD(b);
    
            return new BigInteger(result, 1);
        }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shana07
    I don't understand about this program is doing. Could someone please tell me what it is about. Because I have gcd function in one program that calls this function. Thanks a lot
    Code:
     
    switch (opcode) {
    case GCD: out = b1.gcd(b2).toString();
    break;
    Code:
    /**
    * Returns a BigInteger whose value is the greatest common divisor of
    * <tt>abs(this)</tt> and <tt>abs(val)</tt>. Returns 0 if
    * <tt>this==0 &amp;&amp; val==0</tt>.
    *
    * @param val value with with the GCD is to be computed.
    * @return <tt>GCD(abs(this), abs(val))</tt>
    */
    public BigInteger gcd(BigInteger val) {
    if (val.signum == 0)
    	 return this.abs();
    	else if (this.signum == 0)
    	 return val.abs();
     
    MutableBigInteger a = new MutableBigInteger(this);
    MutableBigInteger b = new MutableBigInteger(val);
     
    MutableBigInteger result = a.hybridGCD(b);
     
    return new BigInteger(result, 1);
    }
    which part specifically do you not understand?

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #3
      Originally posted by r035198x
      which part specifically do you not understand?
      The java package code. From my reading, signum is either '+', '-' or 0.
      Then, mutableBigInteg er is for what?
      As for gcd (a, b) calculation actually is about
      a.gcd(b)
      Then it involves with operator 'mod' to get remainder.
      Where can I check those operators?

      Code:
      public BigInteger gcd(BigInteger val) {
      if (val.signum == 0)
      	 return this.abs();
      	else if (this.signum == 0)
      	 return val.abs();
       
      MutableBigInteger a = new MutableBigInteger(this);
      MutableBigInteger b = new MutableBigInteger(val);
       
      MutableBigInteger result = a.hybridGCD(b);
       
      return new BigInteger(result, 1);
      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by shana07
        The java package code. From my reading, signum is either '+', '-' or 0.
        Then, mutableBigInteg er is for what?
        As for gcd (a, b) calculation actually is about
        a.gcd(b)
        Then it involves with operator 'mod' to get remainder.
        Where can I check those operators?

        Code:
        public BigInteger gcd(BigInteger val) {
        if (val.signum == 0)
        	 return this.abs();
        	else if (this.signum == 0)
        	 return val.abs();
         
        MutableBigInteger a = new MutableBigInteger(this);
        MutableBigInteger b = new MutableBigInteger(val);
         
        MutableBigInteger result = a.hybridGCD(b);
         
        return new BigInteger(result, 1);
        }
        MutableBigInteg er is another class in the java.math package. It's implementation of hybridGCD is

        Code:
         
        MutableBigInteger hybridGCD(MutableBigInteger b) {
        		// Use Euclid's algorithm until the numbers are approximately the
        		// same length, then use the binary GCD algorithm to find the GCD.
        		MutableBigInteger a = this;
        		MutableBigInteger q = new MutableBigInteger(),
        						  r = new MutableBigInteger();
        		while (b.intLen != 0) {
        			if (Math.abs(a.intLen - b.intLen) < 2)
        				return a.binaryGCD(b);
        			a.divide(b, q, r);
        			MutableBigInteger swapper = a;
        			a = b; b = r; r = swapper;
        		}
        		return a;
        	}

        Comment

        • shana07
          Contributor
          • Jan 2007
          • 280

          #5
          Originally posted by r035198x
          MutableBigInteg er is another class in the java.math package. It's implementation of hybridGCD is

          Code:
           
          MutableBigInteger hybridGCD(MutableBigInteger b) {
          		// Use Euclid's algorithm until the numbers are approximately the
          		// same length, then use the binary GCD algorithm to find the GCD.
          		MutableBigInteger a = this;
          		MutableBigInteger q = new MutableBigInteger(),
          						  r = new MutableBigInteger();
          		while (b.intLen != 0) {
          			if (Math.abs(a.intLen - b.intLen) < 2)
          				return a.binaryGCD(b);
          			a.divide(b, q, r);
          			MutableBigInteger swapper = a;
          			a = b; b = r; r = swapper;
          		}
          		return a;
          	}
          Oh it's there...alright . TQ
          One more question I need to consult you.Is it possible for me to replace any operators inside this class (only gcd subroutines)? Seems I can't do mutation on my program (that calls gcd class) .
          * Aim - to do mutation to this gcd function. Because I remember last time, I tried on other java default pakcages and from netbean I can't open that package anymore once I changed it (unzipped the packages and zipped back).....sorry If this confusing you....

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by shana07
            Oh it's there...alright . TQ
            One more question I need to consult you.Is it possible for me to replace any operators inside this class (only gcd subroutines)? Seems I can't do mutation on my program (that calls gcd class) .
            * Aim - to do mutation to this gcd function. Because I remember last time, I tried on other java default pakcages and from netbean I can't open that package anymore once I changed it (unzipped the packages and zipped back).....sorry If this confusing you....
            Do not change any classes in your java directories.
            If you need to use any of them, copy them somewhere else and edit them there if you need to.

            Comment

            • shana07
              Contributor
              • Jan 2007
              • 280

              #7
              Originally posted by r035198x
              Do not change any classes in your java directories.
              If you need to use any of them, copy them somewhere else and edit them there if you need to.
              Meaning to say that this is possible for me to do so isn't.
              I need your advice regarding this please..
              1. I have copied those two default java classes in my program under test
              2. I renamed classes names as below:

              java.math.BigIn teger >>> jonelo.sugar.ma th.MutableBigIn tegerMutant
              java.math.Mutab leBigInteger >>> jonelo.sugar.ma th.BigIntMutant

              3. And I have renamed all methods/variables/constructors with BigIntMutant
              and MutableBigInteg erMutant

              4. Is that possible for me to ask my program to refer to these two new classes when it calls gcd (I changed gcd method >>> gcdMutant).
              Code:
              case GCD:       out = b1.gcdMutant(b2).toString();
                                          break;
              When I compile my main program - these 3 errors occured, Please advise what should I do..thank you

              Main.java:31: cannot find symbol
              symbol : class BigIntMutant
              location: package jonelo.sugar.ma th
              import jonelo.sugar.ma th.BigIntMutant ;
              ^
              Main.java:32: cannot find symbol
              symbol : class MutableBigInteg erMutant
              location: package jonelo.sugar.ma th
              import jonelo.sugar.ma th.MutableBigIn tegerMutant;
              ^
              Main.java:528: cannot find symbol
              symbol : method gcdMutant(java. math.BigInteger )
              location: class java.math.BigIn teger
              case GCD: out = b1.gcdMutant(b2 ).toString();
              ^
              3 errors

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by shana07
                Meaning to say that this is possible for me to do so isn't.
                I need your advice regarding this please..
                1. I have copied those two default java classes in my program under test
                2. I renamed classes names as below:

                java.math.BigIn teger >>> jonelo.sugar.ma th.MutableBigIn tegerMutant
                java.math.Mutab leBigInteger >>> jonelo.sugar.ma th.BigIntMutant

                3. And I have renamed all methods/variables/constructors with BigIntMutant
                and MutableBigInteg erMutant

                4. Is that possible for me to ask my program to refer to these two new classes when it calls gcd (I changed gcd method >>> gcdMutant).
                Code:
                case GCD: out = b1.gcdMutant(b2).toString();
                break;
                When I compile my main program - these 3 errors occured, Please advise what should I do..thank you

                Main.java:31: cannot find symbol
                symbol : class BigIntMutant
                location: package jonelo.sugar.ma th
                import jonelo.sugar.ma th.BigIntMutant ;
                ^
                Main.java:32: cannot find symbol
                symbol : class MutableBigInteg erMutant
                location: package jonelo.sugar.ma th
                import jonelo.sugar.ma th.MutableBigIn tegerMutant;
                ^
                Main.java:528: cannot find symbol
                symbol : method gcdMutant(java. math.BigInteger )
                location: class java.math.BigIn teger
                case GCD: out = b1.gcdMutant(b2 ).toString();
                ^
                3 errors
                First you will need to make sure that the classes you copied are not referencing other classes that you may not have copied.
                Then you need to make sure that your import statements are correct e.g you cannot import a class that you are actually writting code for.

                Comment

                • shana07
                  Contributor
                  • Jan 2007
                  • 280

                  #9
                  Originally posted by r035198x
                  First you will need to make sure that the classes you copied are not referencing other classes that you may not have copied.
                  Then you need to make sure that your import statements are correct e.g you cannot import a class that you are actually writting code for.
                  good example. Help me take a look at this import of the Main class. Why when I compile the Main class comes that errors: can't find class from that import statement....

                  Code:
                  package jonelo.bigal;
                  import java.io.*;
                  import java.math.BigInteger;
                  import java.math.BigDecimal;
                  import jonelo.sugar.math.GeneralMath; // ** Check: This default class from this program
                  
                  import jonelo.sugar.math.BigIntMutant; //** Check: That's why I put these altered class in here
                  import jonelo.sugar.math.MutableBigIntegerMutant; //** Check: same as this

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by shana07
                    good example. Help me take a look at this import of the Main class. Why when I compile the Main class comes that errors: can't find class from that import statement....

                    Code:
                    package jonelo.bigal;
                    import java.io.*;
                    import java.math.BigInteger;
                    import java.math.BigDecimal;
                    import jonelo.sugar.math.GeneralMath; // ** Check: This default class from this program
                     
                    import jonelo.sugar.math.BigIntMutant; //** Check: That's why I put these altered class in here
                    import jonelo.sugar.math.MutableBigIntegerMutant; //** Check: same as this
                    Did you set up the directory structure correctly?

                    Comment

                    • shana07
                      Contributor
                      • Jan 2007
                      • 280

                      #11
                      Originally posted by r035198x
                      Did you set up the directory structure correctly?
                      I think yes, because I just paste the two classes in that directory.
                      So in the src\jonelo\suga r\math directory contains classes:

                      GeneralMath
                      MutableBigInteg erMutant
                      BigIntMutant

                      What else should be done you think...

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by shana07
                        I think yes, because I just paste the two classes in that directory.
                        So in the src\jonelo\suga r\math directory contains classes:

                        GeneralMath
                        MutableBigInteg erMutant
                        BigIntMutant

                        What else should be done you think...
                        and you put the package name correctly in the classes in the package statement.

                        Comment

                        • shana07
                          Contributor
                          • Jan 2007
                          • 280

                          #13
                          Originally posted by r035198x
                          and you put the package name correctly in the classes in the package statement.
                          Hurm, I need to consult you about this naming directory/package which I am not clear about it. I have located my program such this:

                          D:\Assignments\ GOOD\BigAl\BigA l\bigal-src\
                          inside there contains:
                          jonelo\bigal\ * 3 classes: Main, Algorithm, BigAlException
                          jonelo\sugar\ma th\ * classes: GeneralMath, BigIntMutant, MutableBigInteg erMutant
                          jonelo\sugar\ut il\ *1 class: General

                          So, from where should I put import statement in Main class?
                          This is my import statements in Main class, please correct if I made wrong

                          package jonelo.bigal;

                          import java.io.*;
                          import java.math.BigIn teger;
                          import java.math.BigDe cimal;

                          import jonelo.sugar.ma th.GeneralMath;
                          import jonelo.sugar.ma th.BigIntMutant ;
                          import jonelo.sugar.ma th.MutableBigIn tegerMutant;

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by shana07
                            Hurm, I need to consult you about this naming directory/package which I am not clear about it. I have located my program such this:

                            D:\Assignments\ GOOD\BigAl\BigA l\bigal-src\
                            inside there contains:
                            jonelo\bigal\ * 3 classes: Main, Algorithm, BigAlException
                            jonelo\sugar\ma th\ * classes: GeneralMath, BigIntMutant, MutableBigInteg erMutant
                            jonelo\sugar\ut il\ *1 class: General

                            So, from where should I put import statement in Main class?
                            This is my import statements in Main class, please correct if I made wrong

                            package jonelo.bigal;

                            import java.io.*;
                            import java.math.BigIn teger;
                            import java.math.BigDe cimal;

                            import jonelo.sugar.ma th.GeneralMath;
                            import jonelo.sugar.ma th.BigIntMutant ;
                            import jonelo.sugar.ma th.MutableBigIn tegerMutant;
                            Why don't you try to put all your classes in one package first.

                            Comment

                            • shana07
                              Contributor
                              • Jan 2007
                              • 280

                              #15
                              Originally posted by r035198x
                              Why don't you try to put all your classes in one package first.
                              This porblem is solved. I created one package like javah/math/BigInteger.
                              Then I used netbeans to build and run.
                              About this, i need to ask you: build in netbeans is same as compile?
                              Then I can see there is one jar created with same program name and build folder?....What happened was, I used prompt before to compile them one by one.

                              One more thing, what is this statement means?
                              Code:
                               int k = (s1 < s2) ? s1 : s2;

                              Comment

                              Working...