Return the greatest common divisor of two long values - Android java.lang

Android examples for java.lang:Math

Description

Return the greatest common divisor of two long values

Demo Code


//package com.java2s;

public class Main {
    /**/*from w  ww . j  ava  2s  .c  o  m*/
     * Return the greatest common divisor of two long values
     * 
     * @param m
     * @param n
     * @return
     */
    public static long gcd(long m, long n) {
        while (n != 0) {
            long rem = m % n;
            m = n;
            n = rem;
        }
        return m;
    }

    /**
     * Return the greatest common divisor of two int values
     * 
     * @param m
     * @param n
     * @return
     */
    public static int gcd(int m, int n) {
        while (n != 0) {
            int rem = m % n;
            m = n;
            n = rem;
        }
        return m;
    }
}

Related Tutorials