Originally posted by shana07
java.math.BigInteger
Collapse
X
-
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
-
Originally posted by shana07Again 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
-
meaning to say, if I need to write one calculation program to test :Originally posted by r035198xWhat 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.
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
Comment