Java BigInteger Calculate gcd(BigInteger a, BigInteger b)

Here you can find the source of gcd(BigInteger a, BigInteger b)

Description

gcd

License

Open Source License

Declaration

public static BigInteger gcd(BigInteger a, BigInteger b) 

Method Source Code


//package com.java2s;
import java.math.BigInteger;

public class Main {
    public static int gcd(int a, int b) {
        if (a < b) {
            int t = b;
            b = a;//  w  ww  .j  a  v a2 s  .co  m
            a = t;
        }
        int r;
        do {
            r = a % b;
            a = b;
            b = r;
        } while (r != 0);
        return a;
    }

    public static long gcd(long a, long b) {
        if (a < b) {
            long t = b;
            b = a;
            a = t;
        }
        long r;
        do {
            r = a % b;
            a = b;
            b = r;
        } while (r != 0);
        return a;
    }

    public static BigInteger gcd(BigInteger a, BigInteger b) {
        if (a.compareTo(b) < 0) {
            BigInteger t = b;
            b = a;
            a = t;
        }
        BigInteger r = null;
        do {
            r = a.mod(b);
            a = b;
            b = r;
        } while (!r.equals(BigInteger.ZERO));
        return a;
    }
}

Related

  1. extractFloat(BigInteger bi, int start, int end, int decimal)
  2. extractInt(BigInteger bi, int start, int end)
  3. fill(BigInteger[] array, BigInteger value)
  4. first(BigInteger i, BigInteger j)
  5. firstLtSecond(BigInteger first, BigInteger second)
  6. GCD(BigInteger x, BigInteger y)
  7. gcd(BigInteger... values)
  8. gcd(Iterable nums)
  9. gcdEuclides(BigInteger a, BigInteger b)