Example usage for org.apache.commons.math3.primes Primes isPrime

List of usage examples for org.apache.commons.math3.primes Primes isPrime

Introduction

In this page you can find the example usage for org.apache.commons.math3.primes Primes isPrime.

Prototype

public static boolean isPrime(int n) 

Source Link

Document

Primality test: tells if the argument is a (provable) prime or not.

Usage

From source file:com.cloudera.oryx.common.random.RandomUtils.java

/**
 * Finds next-largest "twin primes": numbers p and p+2 such that both are prime. Finds the smallest such p
 * such that the smaller twin, p, is greater than or equal to n. Returns p+2, the larger of the two twins.
 *///from  w  ww  .j a  v a  2  s. c om
public static int nextTwinPrime(int n) {
    if (n > MAX_INT_SMALLER_TWIN_PRIME) {
        throw new IllegalArgumentException();
    }
    if (n <= 3) {
        return 5;
    }
    int next = Primes.nextPrime(n);
    while (!Primes.isPrime(next + 2)) {
        next = Primes.nextPrime(next + 4);
    }
    return next + 2;
}

From source file:org.lightjason.agentspeak.action.buildin.math.CIsPrime.java

@Override
public final IFuzzyValue<Boolean> execute(final IContext p_context, final boolean p_parallel,
        final List<ITerm> p_argument, final List<ITerm> p_return, final List<ITerm> p_annotation) {
    CCommon.flatcollection(p_argument).map(ITerm::<Number>raw).map(i -> Primes.isPrime(i.intValue()))
            .map(CRawTerm::from).forEach(p_return::add);

    return CFuzzyValue.from(true);
}

From source file:org.lightjason.agentspeak.action.builtin.math.CIsPrime.java

@Nonnull
@Override//  ww  w  . jav  a 2s  .  co  m
public final IFuzzyValue<Boolean> execute(final boolean p_parallel, @Nonnull final IContext p_context,
        @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return) {
    CCommon.flatten(p_argument).map(ITerm::<Number>raw).map(i -> Primes.isPrime(i.intValue()))
            .map(CRawTerm::from).forEach(p_return::add);

    return CFuzzyValue.from(true);
}

From source file:org.lightjason.agentspeak.action.builtin.TestCActionMath.java

/**
 * data provider generator for single-value tests
 * @return data// ww w. j  a  va  2 s  .c  om
 */
@DataProvider
public static Object[] singlevaluegenerate() {
    return Stream.concat(

            singlevaluetestcase(

                    Stream.of(2.5, 9.1, 111.7, 889.9),

                    Stream.of(CNextPrime.class),

                    (i) -> (double) Primes.nextPrime(i.intValue())),

            singlevaluetestcase(

                    Stream.of(-2, -6, 4, -1, -5, 3, 49, 30, 6, 5, 1.3, 2.8, 9.7, 1, 8, 180, Math.PI),

                    Stream.of(CAbs.class, CACos.class, CASin.class, CATan.class, CCeil.class, CCos.class,
                            CCosh.class, CDegrees.class, CExp.class, CIsPrime.class, CLog.class, CLog10.class,
                            CFloor.class, CRadians.class, CRound.class, CSignum.class, CSin.class, CSinh.class,
                            CSqrt.class, CTan.class, CTanh.class),

                    (i) -> Math.abs(i.doubleValue()), (i) -> Math.acos(i.doubleValue()),
                    (i) -> Math.asin(i.doubleValue()), (i) -> Math.atan(i.doubleValue()),
                    (i) -> Math.ceil(i.doubleValue()), (i) -> Math.cos(i.doubleValue()),
                    (i) -> Math.cosh(i.doubleValue()), (i) -> Math.toDegrees(i.doubleValue()),
                    (i) -> Math.exp(i.doubleValue()), (i) -> Primes.isPrime(i.intValue()),
                    (i) -> Math.log(i.doubleValue()), (i) -> Math.log10(i.doubleValue()),
                    (i) -> Math.floor(i.doubleValue()), (i) -> Math.toRadians(i.doubleValue()),
                    (i) -> Math.round(i.doubleValue()), (i) -> Math.signum(i.doubleValue()),
                    (i) -> Math.sin(i.doubleValue()), (i) -> Math.sinh(i.doubleValue()),
                    (i) -> Math.sqrt(i.doubleValue()), (i) -> Math.tan(i.doubleValue()),
                    (i) -> Math.tanh(i.doubleValue()))

    ).toArray();
}

From source file:org.moeaframework.util.weights.UniformDesignGeneratorTest.java

@Test
public void testPrimes() {
    int[] primes = new UniformDesignGenerator(3, 100).generateFirstKPrimes(10);

    for (int prime : primes) {
        Assert.assertTrue(Primes.isPrime(prime));
    }/*from w  ww.j  a  v  a2  s.c om*/
}