java.math.BigInteger

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #16
    Originally posted by shana07
    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;
    It's a short cut way of writing the if-else construct. Play around with it and you will understand it better then.

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #17
      Again about java/math package ...
      1. I do need help to understand what is this program for.
      2. In what situation then it calls binaryGCD (different binaryGCD & hybridGCD)
      please..thanks
      Code:
       /**
           * Calculate GCD of this and b. This and b are changed by the computation.
           */
          MutableBigInteger [B]hybridGCD(MutableBigInteger b) [/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();
      
              [B]while (b.intLen != 0) 
              {
                  if (Math.abs(a.intLen - b.intLen) < 2)
                      return a.binaryGCD(b);[/B]
                  a.divide(b, q, r);
                  MutableBigInteger swapper = a;
                  a = b; b = r; r = swapper;
              }
              return a;
          }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #18
        Originally posted by shana07
        Again about java/math package ...
        1. I do need help to understand what is this program for.
        2. In what situation then it calls binaryGCD (different binaryGCD & hybridGCD)
        please..thanks
        Code:
         /**
        * Calculate GCD of this and b. This and b are changed by the computation.
        */
        MutableBigInteger [b]hybridGCD(MutableBigInteger b) [/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();
         
        [b]while (b.intLen != 0) [/b]
        [b]{[/b]
        [b]if (Math.abs(a.intLen - b.intLen) < 2)[/b]
        [b]return a.binaryGCD(b);[/b]
        a.divide(b, q, r);
        MutableBigInteger swapper = a;
        a = b; b = r; r = swapper;
        }
        return a;
        }

        What it's used for can be anything where one fells they need the gcd and when it calls which method is actually explained by the comment there :

        // Use Euclid's algorithm until the numbers are approximately the
        // same length, then use the binary GCD algorithm to find the GCD.

        Comment

        • shana07
          Contributor
          • Jan 2007
          • 280

          #19
          Originally posted by r035198x
          What it's used for can be anything where one fells they need the gcd and when it calls which method is actually explained by the comment there :

          // Use Euclid's algorithm until the numbers are approximately the
          // same length, then use the binary GCD algorithm to find the GCD.
          meaning to say, if I need to write one calculation program to test :
          45.gcd(5) or gcd (45, 5) >>> it will call hybrid function and binary both?
          Sorry, I need to paste long codes here to show my query .....thanks
          Code:
          while (b.intLen != 0) 
                  {
                      [B]if (Math.abs(a.intLen - b.intLen) < 2)
                          return a.binaryGCD(b);[/B]
                      a.divide(b, q, r);
                      MutableBigInteger swapper = a;
                      a = b; b = r; r = swapper;
                  }
                  return a;
              }
          
          
              /**
               * Calculate GCD of this and v.
               * Assumes that this and v are not zero.
               */
            [B]  private MutableBigInteger binaryGCD(MutableBigInteger v) [/B]     {
                  // Algorithm B from Knuth section 4.5.2
                  MutableBigInteger u = this;
                  MutableBigInteger q = new MutableBigInteger(),
                      r = new MutableBigInteger();
          
                  // step B1
                  int s1 = u.getLowestSetBit();
                  int s2 = v.getLowestSetBit();
                  int k = (s1 < s2) ? s1 : s2;  //
                  if (k != 0) {
                      u.rightShift(k);
                      v.rightShift(k);
                  }
          
                  // step B2
                  boolean uOdd = (k==s1);
                  MutableBigInteger t = uOdd ? v: u;
                  int tsign = uOdd ? -1 : 1;
          
                  int lb;
                  while ((lb = t.getLowestSetBit()) >= 0) {
                      // steps B3 and B4
                      t.rightShift(lb);
                      // step B5
                      if (tsign > 0)
                          u = t;
                      else
                          v = t;
          
                      // Special case one word numbers
                      if (u.intLen < 2 && v.intLen < 2) {
                          int x = u.value[u.offset];
                          int y = v.value[v.offset];
                          x  = binaryGcd(x, y);
                          r.value[0] = x;
                          r.intLen = 1;
                          r.offset = 0;
                          if (k > 0)
                              r.leftShift(k);
                          return r;
                      }
                          
                      // step B6
                      if ((tsign = u.difference(v)) == 0)
                          break;
                      t = (tsign >= 0) ? u : v;
                  }
          
                  if (k > 0)
                      u.leftShift(k);
                  return u;
              }
          
              /**
               * Calculate GCD of a and b interpreted as unsigned integers.
               */
              static int binaryGcd(int a, int b) {
                  if (b==0)
                      return a;
                  if (a==0)
                      return b;
          
                  int x;
                  int aZeros = 0;
                  while ((x = (int)a & 0xff) == 0) {
                      a >>>= 8;
                      aZeros += 8;
                  }
                  int y = BigInteger.trailingZeroTable[x];
                  aZeros += y;
                  a >>>= y;
          
                  int bZeros = 0;
                  while ((x = (int)b & 0xff) == 0) {
                      b >>>= 8;
                      bZeros += 8;
                  }
                  y = BigInteger.trailingZeroTable[x];
                  bZeros += y;
                  b >>>= y;
          
                  int t = (aZeros < bZeros ? aZeros : bZeros);
          
                  while (a != b) {
                      if ((a+0x80000000) > (b+0x80000000)) {  // a > b as unsigned
                          a -= b;
          
                          while ((x = (int)a & 0xff) == 0)
                              a >>>= 8;
                          a >>>= BigInteger.trailingZeroTable[x];
                      } else {
                          b -= a;
          
                          while ((x = (int)b & 0xff) == 0)
                              b >>>= 8;
                          b >>>= BigInteger.trailingZeroTable[x];
                      }
                  }
                  return a<<t;
              }

          Comment

          • shana07
            Contributor
            • Jan 2007
            • 280

            #20
            Need help, If someone has experience in core java class - MutableBigInteg er, please share some with me. BinaryGCD here is what for...Let say I have gcd(45, 5) is that involves with binaryGCD(Mutab leBigInteger v)) too?
            Thankss

            Comment

            Working...