MarcoGervasoni / gwt-math

Automatically exported from code.google.com/p/gwt-math
0 stars 0 forks source link

Implementation of BigInteger.gcd(BigInteger) #23

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hello,
thank you for this project!
I like it, but missed the operation BigInteger.gcd(BigInteger). I
implemented it like this:

public BigInteger gcd(BigInteger b) {
    if (ZERO.equals(this))
        return b;
    BigInteger a = this;
    while (! ZERO.equals(b))
        if (a.compareTo(b) > 0) // a > b ?
            a = a.subtract(b);
        else
            b = b.subtract(a);
    return a;
}

If you find it useful, you could include it for the next one who needs this
operation.
Greets
Hendrik

Original issue reported on code.google.com by hhelw...@googlemail.com on 6 Jan 2010 at 9:18

GoogleCodeExporter commented 9 years ago
forgot about negative numbers... Fixed version is:

public BigInteger gcd(BigInteger b) {
    if (ZERO.equals(this))
        return b;
    BigInteger a = abs();
    b = b.abs();
    while (! ZERO.equals(b))
        if (a.compareTo(b) > 0) // a > b ?
            a = a.subtract(b);
        else
            b = b.subtract(a);
    return a;   
}

Original comment by hhelw...@googlemail.com on 15 Jan 2010 at 2:13

GoogleCodeExporter commented 9 years ago
http://code.google.com/p/gwt-java-math supports BigInteger.gcd(BigInteger)

Original comment by rich...@zschech.net on 27 May 2010 at 2:37