1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 | package agents.anac.y2019.harddealer.math3.primes;
|
---|
18 |
|
---|
19 | import agents.anac.y2019.harddealer.math3.exception.MathIllegalArgumentException;
|
---|
20 | import agents.anac.y2019.harddealer.math3.exception.util.LocalizedFormats;
|
---|
21 |
|
---|
22 | import java.util.List;
|
---|
23 |
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Methods related to prime numbers in the range of <code>int</code>:
|
---|
27 | * <ul>
|
---|
28 | * <li>primality test</li>
|
---|
29 | * <li>prime number generation</li>
|
---|
30 | * <li>factorization</li>
|
---|
31 | * </ul>
|
---|
32 | *
|
---|
33 | * @since 3.2
|
---|
34 | */
|
---|
35 | public class Primes {
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Hide utility class.
|
---|
39 | */
|
---|
40 | private Primes() {
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Primality test: tells if the argument is a (provable) prime or not.
|
---|
45 | * <p>
|
---|
46 | * It uses the Miller-Rabin probabilistic test in such a way that a result is guaranteed:
|
---|
47 | * it uses the firsts prime numbers as successive base (see Handbook of applied cryptography
|
---|
48 | * by Menezes, table 4.1).
|
---|
49 | *
|
---|
50 | * @param n number to test.
|
---|
51 | * @return true if n is prime. (All numbers < 2 return false).
|
---|
52 | */
|
---|
53 | public static boolean isPrime(int n) {
|
---|
54 | if (n < 2) {
|
---|
55 | return false;
|
---|
56 | }
|
---|
57 |
|
---|
58 | for (int p : SmallPrimes.PRIMES) {
|
---|
59 | if (0 == (n % p)) {
|
---|
60 | return n == p;
|
---|
61 | }
|
---|
62 | }
|
---|
63 | return SmallPrimes.millerRabinPrimeTest(n);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Return the smallest prime greater than or equal to n.
|
---|
68 | *
|
---|
69 | * @param n a positive number.
|
---|
70 | * @return the smallest prime greater than or equal to n.
|
---|
71 | * @throws MathIllegalArgumentException if n < 0.
|
---|
72 | */
|
---|
73 | public static int nextPrime(int n) {
|
---|
74 | if (n < 0) {
|
---|
75 | throw new MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL, n, 0);
|
---|
76 | }
|
---|
77 | if (n == 2) {
|
---|
78 | return 2;
|
---|
79 | }
|
---|
80 | n |= 1;//make sure n is odd
|
---|
81 | if (n == 1) {
|
---|
82 | return 2;
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (isPrime(n)) {
|
---|
86 | return n;
|
---|
87 | }
|
---|
88 |
|
---|
89 | // prepare entry in the +2, +4 loop:
|
---|
90 | // n should not be a multiple of 3
|
---|
91 | final int rem = n % 3;
|
---|
92 | if (0 == rem) { // if n % 3 == 0
|
---|
93 | n += 2; // n % 3 == 2
|
---|
94 | } else if (1 == rem) { // if n % 3 == 1
|
---|
95 | // if (isPrime(n)) return n;
|
---|
96 | n += 4; // n % 3 == 2
|
---|
97 | }
|
---|
98 | while (true) { // this loop skips all multiple of 3
|
---|
99 | if (isPrime(n)) {
|
---|
100 | return n;
|
---|
101 | }
|
---|
102 | n += 2; // n % 3 == 1
|
---|
103 | if (isPrime(n)) {
|
---|
104 | return n;
|
---|
105 | }
|
---|
106 | n += 4; // n % 3 == 2
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Prime factors decomposition
|
---|
112 | *
|
---|
113 | * @param n number to factorize: must be ≥ 2
|
---|
114 | * @return list of prime factors of n
|
---|
115 | * @throws MathIllegalArgumentException if n < 2.
|
---|
116 | */
|
---|
117 | public static List<Integer> primeFactors(int n) {
|
---|
118 |
|
---|
119 | if (n < 2) {
|
---|
120 | throw new MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL, n, 2);
|
---|
121 | }
|
---|
122 | // slower than trial div unless we do an awful lot of computation
|
---|
123 | // (then it finally gets JIT-compiled efficiently
|
---|
124 | // List<Integer> out = PollardRho.primeFactors(n);
|
---|
125 | return SmallPrimes.trialDivision(n);
|
---|
126 |
|
---|
127 | }
|
---|
128 |
|
---|
129 | }
|
---|