Java gcd gcd(Integer... values)

Here you can find the source of gcd(Integer... values)

Description

Calculates the greatest common divisor of the specified integer numbers.

License

BSD License

Parameter

Parameter Description
values values for which the gcd is to be computed

Return

gcd of all values, negative if all values negative, zero if all values zero, positive otherwise

Declaration

public static int gcd(Integer... values) 

Method Source Code

//package com.java2s;
/*//from  w  ww.j  a  va2s .  c  o  m
 * =============================================================================
 * Simplified BSD License, see http://www.opensource.org/licenses/
 * -----------------------------------------------------------------------------
 * Copyright (c) 2008-2009, Marco Terzer, Zurich, Switzerland
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright 
 *       notice, this list of conditions and the following disclaimer in the 
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Swiss Federal Institute of Technology Zurich 
 *       nor the names of its contributors may be used to endorse or promote 
 *       products derived from this software without specific prior written 
 *       permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 * =============================================================================
 */

public class Main {
    /**
     * Calculates the greatest common divisor of the specified integer numbers.
     * This method might be useful to scale down a vector of integer numbers. 
     * <p>
     * If all numbers are negative, the resulting gcd is also negative. If all 
     * numbers are zero, the result is zero. Otherwise, the result is positive.
     * 
     * @param values   values for which the gcd is to be computed
     * @return         gcd of all values, negative if all values negative, zero
     *                if all values zero, positive otherwise
     */
    public static int gcd(Integer... values) {
        if (values.length == 0)
            return 1;
        int allSgn = signum(values[0].intValue());
        int gcd = values[0].intValue();
        for (int i = 1; i < values.length; i++) {
            if (allSgn != signum(values[i].intValue()))
                allSgn = 1;
            if (gcd == 0 || gcd == 1)
                break;
            gcd = gcd(gcd, values[i].intValue());
        }
        return allSgn == 0 ? 0 : allSgn * gcd;
    }

    /**
     * Calculates the greatest common divisor of the specified integer numbers.
     * This method might be useful to scale down a vector of integer numbers. 
     * <p>
     * If all numbers are negative, the resulting gcd is also negative. If all 
     * numbers are zero, the result is zero. Otherwise, the result is positive.
     * 
     * @param values   values for which the gcd is to be computed
     * @return         gcd of all values, negative if all values negative, zero
     *                if all values zero, positive otherwise
     */
    public static int gcd(int... values) {
        if (values.length == 0)
            return 1;
        int allSgn = signum(values[0]);
        int gcd = values[0];
        for (int i = 1; i < values.length; i++) {
            if (allSgn != signum(values[i]))
                allSgn = 1;
            if (gcd == 0 || gcd == 1)
                break;
            gcd = gcd(gcd, values[i]);
        }
        return allSgn == 0 ? 0 : allSgn * gcd;
    }

    /**
     * Calculates the greatest common divisor of the specified long numbers.
     * This method might be useful to scale down a vector of long numbers. 
     * <p>
     * If all numbers are negative, the resulting gcd is also negative. If all 
     * numbers are zero, the result is zero. Otherwise, the result is positive.
     * 
     * @param values   values for which the gcd is to be computed
     * @return         gcd of all values, negative if all values negative, zero
     *                if all values zero, positive otherwise
     */
    public static long gcd(Long... values) {
        if (values.length == 0)
            return 1;
        int allSgn = signum(values[0].longValue());
        long gcd = values[0].longValue();
        for (int i = 1; i < values.length; i++) {
            if (allSgn != signum(values[i].longValue()))
                allSgn = 1;
            if (gcd == 0 || gcd == 1)
                break;
            gcd = gcd(gcd, values[i].longValue());
        }
        return allSgn == 0 ? 0 : allSgn * gcd;
    }

    /**
     * Calculates the greatest common divisor of the specified long numbers.
     * This method might be useful to scale down a vector of long numbers. 
     * <p>
     * If all numbers are negative, the resulting gcd is also negative. If all 
     * numbers are zero, the result is zero. Otherwise, the result is positive.
     * 
     * @param values   values for which the gcd is to be computed
     * @return         gcd of all values, negative if all values negative, zero
     *                if all values zero, positive otherwise
     */
    public static long gcd(long... values) {
        if (values.length == 0)
            return 1;
        int allSgn = signum(values[0]);
        long gcd = values[0];
        for (int i = 1; i < values.length; i++) {
            if (allSgn != signum(values[i]))
                allSgn = 1;
            if (gcd == 0 || gcd == 1)
                break;
            gcd = gcd(gcd, values[i]);
        }
        return allSgn == 0 ? 0 : allSgn * gcd;
    }

    /**
     * Returns the greatest common divisor of iA and iB using standard euclidian
     * algorithm
     */
    public static int gcd(int iA, int iB) {
        iA = Math.abs(iA);
        iB = Math.abs(iB);
        if (iA == 0)
            return iB;
        if (iB == 0)
            return iA;
        if (iA < 0 || iB < 0) {
            //at least one must be MIN_VALUE, which is a even number
            if (0 != ((iA | iB) & 0x1)) {
                //the other number is not even --> GCD=1
                return 1;
            }
            //both are even numbers, divide by 2
            iA = Math.abs(iA >>> 1);
            iB = Math.abs(iB >>> 1);
        }
        int iMax = Math.max(iA, iB);
        int iMin = Math.min(iA, iB);
        while (iMax != iMin) {
            if (iMax % iMin == 0)
                return iMin;
            int tmp = iMin;
            iMin = iMax - (iMax / iMin) * iMin;
            iMax = tmp;
        }
        return iMin;
    }

    /**
     * Returns the greatest common divisor of iA and iB using standard euclidian
     * algorithm
     */
    public static long gcd(long iA, long iB) {
        iA = Math.abs(iA);
        iB = Math.abs(iB);
        if (iA == 0)
            return iB;
        if (iB == 0)
            return iA;
        if (iA < 0 || iB < 0) {
            //at least one must be MIN_VALUE, which is a even number
            if (0 != ((iA | iB) & 0x1)) {
                //the other number is not even --> GCD=1
                return 1;
            }
            //both are even numbers, divide by 2
            iA = Math.abs(iA >>> 1);
            iB = Math.abs(iB >>> 1);
        }
        long iMax = Math.max(iA, iB);
        long iMin = Math.min(iA, iB);
        while (iMax != iMin) {
            if (iMax % iMin == 0)
                return iMin;
            long tmp = iMin;
            iMin = iMax - (iMax / iMin) * iMin;
            iMax = tmp;
        }
        return iMin;
    }

    /**
     * Returns the signum of the long value, i.e. 1/-1/0 for a positive, 
     * negative or zero value
     * 
     * @see Math#signum(double)
     */
    public static int signum(long value) {
        return value == 0 ? 0 : value > 0 ? 1 : -1;
    }

    /**
     * Returns the signum of the int value, i.e. 1/-1/0 for a positive, 
     * negative or zero value
     * 
     * @see Math#signum(double)
     */
    public static int signum(int value) {
        return value == 0 ? 0 : value > 0 ? 1 : -1;
    }
}

Related

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