Gets the lcm of a number from the parameter to 1 - Java java.lang

Java examples for java.lang:Math Number

Description

Gets the lcm of a number from the parameter to 1

Demo Code


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main{
    /**/*from ww  w .  j  av  a 2 s .  co m*/
     * Gets the lcm of a number from the parameter to 1
     *
     * @param num Number to test
     * @return The lcm of the number to 1
     */
    public static long recursiveLcm(long num) {
        if (num == 1 || num == 0) {
            return num;
        } else {
            return HelperFunctions.lcm(num, recursiveLcm(num - 1));
        }
    }
    /**
     * 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