Java gcd gcd(long u, long v)

Here you can find the source of gcd(long u, long v)

Description

Returns the greatest common divisor of a pair of numbers.

License

GNU General Public License

Declaration

public static final long gcd(long u, long v) 

Method Source Code

//package com.java2s;
// Licensed under the terms of the GNU GPL; see COPYING for details.

public class Main {
    /** Lowest bit set in a byte. */
    static final byte bytelsb[] = { 0, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2,
            1, 3, 1, 2, 1, 6, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1,
            2, 1, 7, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 6,
            1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 8, 1, 2, 1,
            3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 6, 1, 2, 1, 3, 1, 2,
            1, 4, 1, 2, 1, 3, 1, 2, 1, 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 7, 1, 2, 1, 3, 1, 2, 1, 4, 1,
            2, 1, 3, 1, 2, 1, 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 6, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3,
            1, 2, 1, 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1 };

    /** Returns the greatest common divisor of a pair of numbers. */
    public static final long gcd(long u, long v) { // long version.
        assert u > 0 && v > 0;
        int u2s = ffs(u) - 1, v2s = ffs(v) - 1;
        u >>= u2s;//from   w w  w . j a va2s.  c  om
        v >>= v2s; // cast out twos.
        // binary gcd algorithm; u and v must be odd at this point.
        while (u != v) {
            while (u < v) {
                v -= u;
                v >>= (ffs(v) - 1);
            }
            long t = u;
            u = v;
            v = t;
        }
        // u,v have gcd
        return u << Math.min(u2s, v2s); // restore cast out twos.
    }

    /** Returns the greatest common divisor of a pair of numbers. */
    public static final int gcd(int u, int v) { // integer version.
        assert u > 0 && v > 0;
        int u2s = ffs(u) - 1, v2s = ffs(v) - 1;
        u >>= u2s;
        v >>= v2s; // cast out twos.
        // binary gcd algorithm; u and v must be odd at this point.
        while (u != v) {
            while (u < v) {
                v -= u;
                v >>= (ffs(v) - 1);
            }
            int t = u;
            u = v;
            v = t;
        }
        // u,v have gcd
        return u << Math.min(u2s, v2s); // restore cast out twos.
    }

    /** Find first set (least significant bit).
     *  @return the first bit set in the argument.  
     *          <code>ffs(0)==0</code> and <code>ffs(1)==1</code>. */
    public static final int ffs(int v) {
        if ((v & 0x0000FFFF) != 0)
            if ((v & 0x000000FF) != 0)
                return bytelsb[v & 0xFF];
            else
                return 8 + bytelsb[(v >> 8) & 0xFF];
        else if ((v & 0x00FFFFFF) != 0)
            return 16 + bytelsb[(v >> 16) & 0xFF];
        else
            return 24 + bytelsb[(v >> 24) & 0xFF];
    }

    /** Find first set (least significant bit).
     *  @return the first bit set in the argument.  
     *          <code>ffs(0)==0</code> and <code>ffs(1)==1</code>. */
    public static final int ffs(long v) {
        if ((v & 0xFFFFFFFFL) != 0)
            return ffs((int) (v & 0xFFFFFFFFL));
        else
            return 32 + ffs((int) (v >> 32));
    }
}

Related

  1. GCD(long a, long b)
  2. gcd(long a, long b)
  3. gcd(long a, long b)
  4. GCD(long a, long b)
  5. gcd(long a, long b)
  6. gcd(long x, long y)
  7. gcd(long x, long y)
  8. gcd(long[] array)
  9. gcd(long[] array)