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 - Java java.lang

Java examples for java.lang:int prime

Description

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

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./*  ww w  .j a  v a 2  s.co 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{
    /**
     * 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