Java gcd gcd(int[] array)

Here you can find the source of gcd(int[] array)

Description

Computes the greatest absolute common divisor of an integer array.

License

Open Source License

Parameter

Parameter Description
array Input array

Return

The greatest absolute common divisor. By convention, gcd(0, 0) is equal to zero and gcd([]) is equal to one

Declaration

public static int gcd(int[] array) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors:/*w w  w  .java  2s  .c  o m*/
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

import java.util.*;

public class Main {
    /**
     * Computes the greatest absolute common divisor of an integer array.
     *
     * @param array Input array
     * @return The greatest absolute common divisor. By convention, {@code gcd(0, 0)} is equal to zero and {@code gcd([])} is equal to one
     */
    public static int gcd(int[] array) {
        if (array.length == 0) {
            return 1;
        }

        int[] maxMinValue = maxMinValues(array);
        int minAbsValue = Math.min(Math.abs(maxMinValue[0]), Math.abs(maxMinValue[1]));

        for (int i = minAbsValue; i >= 1; i--) {
            int j;

            for (j = 0; j < array.length; ++j) {
                if (Math.abs(array[j]) % i != 0) {
                    break;
                }
            }

            if (j == array.length) {
                return i;
            }
        }

        return 1;
    }

    /**
     * Computes the greatest common divisor of an input collection.
     *
     * @param collection Input collection
     * @return The greatest common divisor. By convention, {@code gcd(0, 0)} is equal to zero and {@code gcd([])} is equal to one
     */
    public static int gcd(Collection<Integer> collection) {
        if (collection.isEmpty())
            throw new NoSuchElementException("Empty collection");

        return gcd(toArray(collection));
    }

    /**
     * Returns the maximum/minimum values of an input collection.
     *
     * @param collection Input collection
     * @return Maximum/minimum values
     */
    public static int[] maxMinValues(Collection<Integer> collection) {
        if (collection.isEmpty())
            throw new NoSuchElementException("Empty collection");

        int maxValue = Integer.MIN_VALUE;
        int minValue = Integer.MAX_VALUE;
        for (int value : collection) {
            if (value > maxValue)
                maxValue = value;
            if (value < minValue)
                minValue = value;
        }

        return arrayOf(maxValue, minValue);
    }

    /**
     * Returns the maximum/minimum values of an input array.
     *
     * @param <A> the map key type
     * @param map Input map
     * @return Maximum/minimum values
     */
    public static <A> int[] maxMinValues(Map<A, Integer> map) {
        return maxMinValues(map.values());
    }

    /**
     * Returns the maximum/minimum values of an input array.
     *
     * @param array Input array
     * @return Maximum/minimum values
     */
    public static int[] maxMinValues(int[] array) {
        if (array.length == 0) {
            throw new NoSuchElementException("Empty array");
        }

        int maxValue = array[0];
        int minValue = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > maxValue)
                maxValue = array[i];

            if (array[i] < minValue)
                minValue = array[i];
        }

        return arrayOf(maxValue, minValue);
    }

    /**
     * Converts an array of {@code String} with the values, into a {@code int} array. Uses {@code Integer.parseInt} to parse the number, an can raise the exceptions 
     * that this method raises
     *
     * @param list Input list
     * @return output array
     */
    public static int[] toArray(String[] list) {
        int[] res = new int[list.length];
        int counter = 0;
        for (String s : list)
            res[counter++] = Integer.parseInt(s);
        return res;
    }

    /**
     * Converts a collection ({@code List}, {@code Set}...) of {@code Integer} objects to an {@code int} array.
     *
     * @param list Input list
     * @return {@code int} array
     *  
     */
    public static int[] toArray(Collection<Integer> list) {
        return asPrimitiveArray(list.toArray(new Integer[list.size()]));
    }

    /**
     * Generates an {@code int[]} from comma-separated values.
     *
     * @param values Comma-separated {@code int} values
     * @return {@code int[]}
     */
    public static int[] arrayOf(int... values) {
        return values;
    }

    /**
     * Converts from an {@code Integer} array to an {@code int} array.
     *
     * @param array {@code Integer} array
     * @return Equivalent {@code int} array
     */
    public static int[] asPrimitiveArray(Integer[] array) {
        int[] primitiveArray = new int[array.length];

        for (int i = 0; i < array.length; i++)
            primitiveArray[i] = array[i];

        return primitiveArray;
    }
}

Related

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