Java gcd gcd(int a, int b)

Here you can find the source of gcd(int a, int b)

Description

gcd

License

Open Source License

Declaration

public static int gcd(int a, int b) 

Method Source Code

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

public class Main {
    public static int gcd(int a, int b) {
        if (a > b) {
            int tmp = a;
            a = b;//from w  ww .  j  a va  2s. c  o  m
            b = tmp;
        }
        if (a == 0) {
            return 0;
        }
        if (b % a == 0) {
            return a;
        }
        return gcd(b % a, a);
    }

    public static long gcd(long a, long b) {
        if (a > b) {
            long tmp = a;
            a = b;
            b = tmp;
        }
        if (a == 0) {
            return 0;
        }
        if (b % a == 0) {
            return a;
        }
        return gcd(b % a, a);
    }
}

Related

  1. GCD(double a, double b)
  2. gcd(final int a, final int b)
  3. gcd(final int p, final int q)
  4. gcd(int a, int b)
  5. gcd(int a, int b)
  6. gcd(int a, int b)
  7. gcd(int a, int b)
  8. gcd(int a, int b)
  9. gcd(int a, int b)