Java lcm lcm(int a, int b)

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

Description

lcm

License

Open Source License

Declaration

public static int lcm(int a, int b) 

Method Source Code

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

public class Main {
    public static int lcm(int a, int b) {
        int c = gcd(a, b);
        if (c == 0) {
            return 0;
        }//w  w w. j a  v  a  2s .  co m
        return a / c * b;
    }

    public static long lcm(long a, long b) {
        long c = gcd(a, b);
        if (c == 0) {
            return 0;
        }
        return a / c * b;
    }

    public static int gcd(int a, int b) {
        if (a > b) {
            int tmp = a;
            a = b;
            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. LCM(double a, double b)
  2. lcm(int a, int b)
  3. lcm(int a, int b)
  4. lcm(int a, int b)
  5. lcm(int a, int b)
  6. lcm(int a, int b)