Gets the lowest number divisible by a and b - Java java.lang

Java examples for java.lang:Math Number

Description

Gets the lowest number divisible by a and b

Demo Code


//package com.java2s;

public class Main {
    /**//w  w w .j a  v  a 2  s  .  c o m
     * Gets the lowest number divisible by a and b
     *
     * @param a Number 1
     * @param b Number 2
     * @return The lowest number divisible by a and b
     */
    public static long lcm(long a, long b) {
        return (a * b) / gcd(a, b);
    }

    /**
     * Gets the greatest number divisible by both parameters
     *
     * @param a Number 1
     * @param b Number 2
     * @return The GCD of the two parameters
     */
    public static long gcd(long a, long b) {
        while (b != 0) {
            long tmp = b;
            b = a % b;
            a = tmp;
        }

        return a;
    }
}

Related Tutorials