Calculates the greatest common divisor of an array using Euclid's method. - Java java.lang

Java examples for java.lang:Math Operation

Description

Calculates the greatest common divisor of an array using Euclid's method.

Demo Code


//package com.java2s;

public class Main {
    /**/* ww w . j a va  2  s .  com*/
     * Calculates the greatest common divisor of an array using Euclid's method. Thank you Euclid.
     * @param integers Iterable of integers, can be negative or positive and in any order
     * @return the greatest common divisor of all elements of the array
     * <p>
     * If the array is all zeros the greatest common divisor will be zero, and not the more mathematically correct infinity
     */
    public static int greatestCommonDivisor(Iterable<Integer> integers) {

        int result = integers.iterator().next();
        for (int integer : integers) {
            result = greatestCommonDivisor(result, integer);
        }
        return result;
    }

    /**
     * Calculates the greatest common divisor of an array using Euclid's method. Thank you Euclid.
     * @param integers array of integers, can be negative or positive and in any order
     * @return the greatest common divisor of all elements of the array
     * <p>
     * If the array is all zeros the greatest common divisor will be zero, and not the more mathematically correct infinity
     */
    public static int greatestCommonDivisor(int[] integers) {

        int result = integers[0];
        for (int integer : integers) {
            result = greatestCommonDivisor(result, integer);
        }
        return result;
    }

    /**
     * Calculates the greatest common divisor using Euclid's method
     * @param firstInteger any integer, can be negative or positive, greater or smaller than second
     * @param secondInteger any integer, can be negative or positive, greater or smaller than first
     * @return the greatest common divisor of firstInteger and secondInteger
     * <p>
     * If both parameters are zero the greatest common divisor will be zero, and not the more mathematically correct infinity
     */
    public static int greatestCommonDivisor(int firstInteger,
            int secondInteger) {

        //convert to positive numbers
        firstInteger = Math.abs(firstInteger);
        secondInteger = Math.abs(secondInteger);

        //find biggest and smallest absolute values
        int biggestPositiveInteger = Math.max(firstInteger, secondInteger);
        int smallestPositiveInteger = Math.min(firstInteger, secondInteger);

        //use Euclid's method (thank you Euclid)
        return greatestCommonDivisorInternal(biggestPositiveInteger,
                smallestPositiveInteger);

    }

    /**
     * Internal method to be used once parameters are cleaned.
     */
    private static int greatestCommonDivisorInternal(
            int biggestPositiveInteger, int smallestPositiveInteger) {

        //use Euclid's algorithm (thank you Euclid)
        if (smallestPositiveInteger == 0) {
            return biggestPositiveInteger;
        }
        return greatestCommonDivisorInternal(smallestPositiveInteger,
                biggestPositiveInteger % smallestPositiveInteger);

    }
}

Related Tutorials