Here you can find the source of gcd(int p, int q)
Calculates the greatest common divisor by using the <a href="https://en.wikipedia.org/wiki/Euclidean_algorithm">Euclidean algorithm</a>.
Parameter | Description |
---|---|
p | the numerator |
q | the denominator |
public static int gcd(int p, int q)
//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; } }