Java gcd gcd(int p, int q)

Here you can find the source of gcd(int p, int q)

Description

Calculates the greatest common divisor by using the <a href="https://en.wikipedia.org/wiki/Euclidean_algorithm">Euclidean algorithm</a>.

License

Open Source License

Parameter

Parameter Description
p the numerator
q the denominator

Return

the greatest common divisor

Declaration

public static int gcd(int p, int q) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/* w  w w  . ja  v a 2s .c  om*/
     * Calculates the greatest common divisor by using the
     * <a href="https://en.wikipedia.org/wiki/Euclidean_algorithm">Euclidean
     * algorithm</a>.
     * 
     * @param p the numerator
     * @param q the denominator
     * @return the greatest common divisor
     */
    public static int gcd(int p, int q) {
        while (q != 0) {
            int r = q;
            q = p % q;
            p = r;
        }
        return p;
    }
}

Related

  1. GCD(int m, int n)
  2. gcd(int m, int n)
  3. gcd(int n, int m)
  4. gcd(int n1, int n2)
  5. gcd(int num1, int num2)
  6. gcd(int p, int q)
  7. gcd(int u, int v)
  8. gcd(int u, int v)
  9. gcd(int u, int v)