calculates the Greatest Common Divisor of a and b. - Java java.lang

Java examples for java.lang:int prime

Description

calculates the Greatest Common Divisor 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.// w w w .  j  a  va  2s  .  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{
    /**
     * calculates the Greatest Common Divisor of a and b.
     * if no gcd bigger than 1 is found, return 1
     */
    public static long getGCD(long a, long b) {
        /*
         * get the prime factors of the given values
         */
        Long[] afacs = getPrimFactors(a);
        Long[] bfacs = getPrimFactors(b);

        /*
         * initialize the variable for the gcd with one. it
         * will then be multiplied by the prime factors which appear
         * in both a and b prime factors.
         * if there aren't any equal factors it remains 1;
         */
        long gcd = 1;

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

            for (int j = 0; j < bfacs.length; j++) {
                /*
                 * if bfacs[j] is null, skip to next
                 * if both a and b factors are equal, 
                 * multiplie it to gcd and set the bfacs[j] to zero,
                 * so it can't be used twice
                 */
                if (bfacs[j] != 0 && afacs[i] == bfacs[j]) {
                    gcd = Math.mul(gcd, afacs[i]);
                    bfacs[j] = 0L;
                    break;
                }
            }

        }

        /*
         * return gcd
         */
        return gcd;
    }
    /**
     * 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