Java gcd gcd(int m, int n)

Here you can find the source of gcd(int m, int n)

Description

calculates the greatest common divisor of two numbers

License

Open Source License

Parameter

Parameter Description
m first number
n second number

Return

the gcd of m and n

Declaration

static public int gcd(int m, int n) 

Method Source Code

//package com.java2s;
/**/* ww w.j a  v  a 2  s .  co m*/
 * Copyright (c) 2014-2017 by the respective copyright holders.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * calculates the greatest common divisor of two numbers
     * 
     * @param m
     *            first number
     * @param n
     *            second number
     * @return the gcd of m and n
     */
    static public int gcd(int m, int n) {
        if (m % n == 0)
            return n;
        return gcd(n, m % n);
    }

    /**
     * calculates the greatest common divisor of n numbers
     * 
     * @param numbers
     *            an array of n numbers
     * @return the gcd of the n numbers
     */
    static public int gcd(Integer[] numbers) {
        int n = numbers[0];
        for (int m : numbers) {
            n = gcd(n, m);
        }
        return n;
    }
}

Related

  1. gcd(int a, int b, int... rest)
  2. gcd(int firstNumber, int secondNumber)
  3. gcd(int k, int m)
  4. gcd(int largerNumber, int smallerNumber)
  5. GCD(int m, int n)
  6. gcd(int m, int n)
  7. gcd(int n, int m)
  8. gcd(int n1, int n2)
  9. gcd(int num1, int num2)