Tests if some given BigInteger values are all prime factors of another BigInteger value. - Java java.lang

Java examples for java.lang:Math Prime Number

Description

Tests if some given BigInteger values are all prime factors of another BigInteger value.

Demo Code

/*//from   www . ja  va  2 s  .  c  om
 * UniCrypt
 *
 *  UniCrypt(tm) : Cryptographical framework allowing the implementation of cryptographic protocols e.g. e-voting
 *  Copyright (C) 2014 Bern University of Applied Sciences (BFH), Research Institute for
 *  Security in the Information Society (RISIS), E-Voting Group (EVG)
 *  Quellgasse 21, CH-2501 Biel, Switzerland
 *
 *  Licensed under Dual License consisting of:
 *  1. GNU Affero General Public License (AGPL) v3
 *  and
 *  2. Commercial license
 *
 *
 *  1. This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program 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 Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *  2. Licensees holding valid commercial licenses for UniCrypt may use this file in
 *   accordance with the commercial license agreement provided with the
 *   Software or, alternatively, in accordance with the terms contained in
 *   a written agreement between you and Bern University of Applied Sciences (BFH), Research Institute for
 *   Security in the Information Society (RISIS), E-Voting Group (EVG)
 *   Quellgasse 21, CH-2501 Biel, Switzerland.
 *
 *
 *   For further information contact <e-mail: unicrypt@bfh.ch>
 *
 *
 * Redistributions of files must retain the above copyright notice.
 */
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;

public class Main{
    public static final int NUMBER_OF_PRIME_TESTS = 40;
    /**
     * Tests if some given BigInteger values are all prime factors of another BigInteger value. The given list of prime
     * factors need not be complete.
     * <p>
     * @param value   The given value
     * @param factors A given array of potential prime factors
     * @return {@literal true} if all values are prime factors, {@literal false} otherwise
     */
    public static boolean arePrimeFactors(final BigInteger value,
            final BigInteger... factors) {
        if (factors == null) {
            return false;
        }
        for (BigInteger factor : factors) {
            if (!isPrimeFactor(value, factor)) {
                return false;
            }
        }
        return isPositive(value);
    }
    private static boolean isPrimeFactor(final BigInteger value,
            final BigInteger factor) {
        return isPositive(value) && isPrime(factor)
                && value.gcd(factor).equals(factor);
    }
    /**
     * Tests if a given BigInteger value is positive.
     * <p>
     * @param value A potential positive number
     * @return {@literal true} if {@literal value} is positive, {@literal false} otherwise
     */
    public static boolean isPositive(final BigInteger value) {
        if (value == null) {
            return false;
        }
        return value.signum() == 1;
    }
    /**
     * Tests if a given BigInteger value is a positive prime number.
     * <p>
     * @param value A potential prime number
     * @return {@literal true} if {@literal value} is prime, {@literal false} otherwise
     */
    public static boolean isPrime(final BigInteger value) {
        return isPositive(value)
                && value.isProbablePrime(MathUtil.NUMBER_OF_PRIME_TESTS);
    }
}

Related Tutorials