returns the Lowest Common Multiple of a and b - Java java.lang

Java examples for java.lang:int prime

Description

returns the Lowest Common Multiple of a and b

Demo Code

/* libj-lossless-math - a java library for calculating numerical terms without any loss of precision

   Copyright (C) 2011 Matthias Kolb <jauntyjackalope.mmource@gmx.at>

   libj-lossless-math is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.// www .ja  v a2 s . c  o  m

   libj-lossless-math is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
import java.util.*;

public class Main{
    /**
     * returns the Lowest Common Multiple of a and b
     */
    public static long getLCM(long a, long b) {
        /*
         * get prime factors of a and b
         */
        Long[] afacs = getPrimFactors(a);
        Long[] bfacs = getPrimFactors(b);

        /*
         * initialize lcm with 1
         */
        long lcm = 1;

        for (int i = 0; i < afacs.length; i++) {

            for (int j = 0; j < bfacs.length; j++) {
                /*
                 * if both factors aren't 0 and are equal, 
                 * one can be let out (always b)
                 */
                if (bfacs[j] != 0 && afacs[i] == bfacs[j]) {
                    bfacs[j] = 0L;
                    break;
                }

            }

            /*
             * multiply all afacs
             */
            lcm = Math.mul(lcm, afacs[i]);

        }

        /*
         * now multiplie the remaining b factors to the lcm
         */
        for (int i = 0; i < bfacs.length; i++) {
            if (bfacs[i] != 0) {
                lcm = Math.mul(lcm, bfacs[i]);
            }

        }

        return lcm;

    }
    /**
     * this method calculates the prime factors of the given x
     * when x is 0 or +/-1, an empty array is returned, else an Array
     * of integers
     * @param x value to factorize
     * @return prime factors
     */
    public static Long[] getPrimFactors(long x) {

        /*
         * when x is 0 or +/-1, an empty array is returned
         */
        if (x == 0 || java.lang.Math.abs(x) == 1) {
            return new Long[0];
        }

        /*
         * else, create counter for the primes, the list which
         * contains prime numbers 
         */
        int primecounter = 0;

        /*
         * the prime factors 
         */
        ArrayList<Long> factors = new ArrayList<Long>();

        /*
         * x is divided after testing with %, until it's +/-1.
         */
        while (java.lang.Math.abs(x) > 1) {
            /*
             * get the next prime number to test if it's a prime factor of x
             */
            long currentprime = primes.get(primecounter);

            /*
             * if yes, divide x by the current prime number
             * and reinitialize the prime counter
             */
            if (x % currentprime == 0) {
                factors.add(currentprime);
                x /= currentprime;
                primecounter = 0;
            }

            /*
             * else, set the primecounter for the next prime number, and
             * calculate it if it's necessary
             */
            else {
                if (primecounter + 1 >= primes.size()) {
                    getNextPrime(primes);
                }
                primecounter++;
            }
        }

        /*
         * return an int[] array containing the prime factors
         */
        return factors.toArray(new Long[] {});

    }
    private static long getNextPrime(ArrayList<Long> primes) {
        /*
         * toTest is the number which is tested whether it's a prime number,
         * if it's detected not being, toTest + 1 is tested
         */
        long toTest = primes.get(primes.size() - 1);
        boolean isPrime;

        do {
            toTest++;
            isPrime = true;
            for (int i = 0; i < primes.size(); i++) {
                if (toTest % primes.get(i) == 0) {
                    isPrime = false;
                    break;
                }
            }

        } while (!isPrime);

        primes.add(toTest);

        return toTest;
    }
}

Related Tutorials