Skip to content

Commit 9f985b2

Browse files
saahilmahatoalxkm
andauthored
feat: add euler primality test (#6680)
* feat: add euler primality test * refactor: use fixed seed * fix: more unit test coverage * fix: add mock tests for edge cases * fix: styling issues * refactor: remove duplicate tests * refactor: reduce static imports * refactor: remove unnecessary tests * refactor: move to maths package --------- Co-authored-by: a <[email protected]>
1 parent 8ca2d9f commit 9f985b2

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.thealgorithms.maths;
2+
3+
import java.math.BigInteger;
4+
import java.util.Random;
5+
6+
/**
7+
* The {@code EulerPseudoprime} class implements the Euler primality test.
8+
*
9+
* It is based on Euler’s criterion:
10+
* For an odd prime number {@code n} and any integer {@code a} coprime to {@code n}:
11+
* a^((n-1)/2) ≡ (a/n) (mod n)
12+
* where (a/n) is the Jacobi symbol.
13+
*
14+
* This algorithm is a stronger probabilistic test than Fermat’s test.
15+
* It may still incorrectly identify a composite as “probably prime” (Euler pseudoprime),
16+
* but such cases are rare.
17+
*/
18+
public final class EulerPseudoprime {
19+
20+
private EulerPseudoprime() {
21+
// Private constructor to prevent instantiation.
22+
}
23+
24+
private static final Random RANDOM = new Random(1);
25+
26+
/**
27+
* Performs the Euler primality test for a given number.
28+
*
29+
* @param n number to test (must be > 2 and odd)
30+
* @param trials number of random bases to test
31+
* @return {@code true} if {@code n} passes all Euler tests (probably prime),
32+
* {@code false} if composite.
33+
*/
34+
public static boolean isProbablePrime(BigInteger n, int trials) {
35+
if (n.compareTo(BigInteger.TWO) < 0) {
36+
return false;
37+
}
38+
if (n.equals(BigInteger.TWO) || n.equals(BigInteger.valueOf(3))) {
39+
return true;
40+
}
41+
if (n.mod(BigInteger.TWO).equals(BigInteger.ZERO)) {
42+
return false;
43+
}
44+
45+
for (int i = 0; i < trials; i++) {
46+
BigInteger a = uniformRandom(BigInteger.TWO, n.subtract(BigInteger.TWO));
47+
BigInteger jacobi = BigInteger.valueOf(jacobiSymbol(a, n));
48+
if (jacobi.equals(BigInteger.ZERO)) {
49+
return false;
50+
}
51+
52+
BigInteger exp = n.subtract(BigInteger.ONE).divide(BigInteger.TWO);
53+
BigInteger modExp = a.modPow(exp, n);
54+
55+
// Euler's criterion: a^((n-1)/2) ≡ (a/n) (mod n)
56+
if (!modExp.equals(jacobi.mod(n))) {
57+
return false; // definitely composite
58+
}
59+
}
60+
return true; // probably prime
61+
}
62+
63+
/**
64+
* Computes the Jacobi symbol (a/n).
65+
* Assumes n is positive and odd.
66+
*/
67+
public static int jacobiSymbol(BigInteger a, BigInteger n) {
68+
if (n.signum() <= 0 || n.mod(BigInteger.TWO).equals(BigInteger.ZERO)) {
69+
throw new IllegalArgumentException("n must be positive and odd.");
70+
}
71+
72+
int result = 1;
73+
a = a.mod(n);
74+
75+
while (a.compareTo(BigInteger.ZERO) != 0) {
76+
while (a.mod(BigInteger.TWO).equals(BigInteger.ZERO)) {
77+
a = a.divide(BigInteger.TWO);
78+
BigInteger nMod8 = n.mod(BigInteger.valueOf(8));
79+
if (nMod8.equals(BigInteger.valueOf(3)) || nMod8.equals(BigInteger.valueOf(5))) {
80+
result = -result;
81+
}
82+
}
83+
84+
BigInteger temp = a;
85+
a = n;
86+
n = temp;
87+
88+
if (a.mod(BigInteger.valueOf(4)).equals(BigInteger.valueOf(3)) && n.mod(BigInteger.valueOf(4)).equals(BigInteger.valueOf(3))) {
89+
result = -result;
90+
}
91+
92+
a = a.mod(n);
93+
}
94+
95+
return n.equals(BigInteger.ONE) ? result : 0;
96+
}
97+
98+
/**
99+
* Generates a random BigInteger between {@code min} and {@code max}, inclusive.
100+
*/
101+
private static BigInteger uniformRandom(BigInteger min, BigInteger max) {
102+
BigInteger result;
103+
do {
104+
result = new BigInteger(max.bitLength(), RANDOM);
105+
} while (result.compareTo(min) < 0 || result.compareTo(max) > 0);
106+
return result;
107+
}
108+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.mockito.ArgumentMatchers.any;
7+
8+
import java.math.BigInteger;
9+
import org.junit.jupiter.api.Test;
10+
import org.mockito.MockedStatic;
11+
import org.mockito.Mockito;
12+
13+
class EulerPseudoprimeTest {
14+
15+
@Test
16+
void testPrimeNumbers() {
17+
assertTrue(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(7), 5));
18+
assertTrue(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(13), 5));
19+
assertTrue(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(101), 5));
20+
}
21+
22+
@Test
23+
void testCompositeNumbers() {
24+
assertFalse(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(9), 5));
25+
assertFalse(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(21), 5));
26+
assertFalse(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(221), 5));
27+
}
28+
29+
@Test
30+
void testEvenNumbers() {
31+
assertFalse(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(4), 5));
32+
assertFalse(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(100), 5));
33+
}
34+
35+
@Test
36+
void testEdgeCases() {
37+
assertFalse(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(0), 5));
38+
assertFalse(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(1), 5));
39+
assertTrue(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(2), 5));
40+
assertTrue(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(3), 5));
41+
}
42+
43+
@Test
44+
void testIsProbablePrimeWhenJacobiSymbolIsZero() {
45+
try (MockedStatic<EulerPseudoprime> mockedPrimality = Mockito.mockStatic(EulerPseudoprime.class, Mockito.CALLS_REAL_METHODS)) {
46+
47+
// Mock jacobiSymbol to return 0 to test the branch
48+
mockedPrimality.when(() -> EulerPseudoprime.jacobiSymbol(any(BigInteger.class), any(BigInteger.class))).thenReturn(0);
49+
50+
boolean result = EulerPseudoprime.isProbablePrime(BigInteger.valueOf(15), 1);
51+
52+
assertFalse(result);
53+
}
54+
}
55+
56+
@Test
57+
void testJacobiSymbolThrowsForEvenOrNonPositiveN() throws Exception {
58+
var method = EulerPseudoprime.class.getDeclaredMethod("jacobiSymbol", BigInteger.class, BigInteger.class);
59+
60+
// Helper lambda to unwrap InvocationTargetException
61+
Runnable invokeJacobi = () -> {
62+
try {
63+
method.invoke(null, BigInteger.valueOf(2), BigInteger.valueOf(8));
64+
} catch (Exception e) {
65+
// unwrap
66+
Throwable cause = e.getCause();
67+
if (cause instanceof IllegalArgumentException) {
68+
throw (IllegalArgumentException) cause;
69+
} else {
70+
throw new RuntimeException(e);
71+
}
72+
}
73+
};
74+
75+
// Now check that it actually throws
76+
assertThrows(IllegalArgumentException.class, invokeJacobi::run);
77+
78+
// Another case: non-positive n
79+
Runnable invokeJacobi2 = () -> {
80+
try {
81+
method.invoke(null, BigInteger.valueOf(5), BigInteger.valueOf(-3));
82+
} catch (Exception e) {
83+
Throwable cause = e.getCause();
84+
if (cause instanceof IllegalArgumentException) {
85+
throw (IllegalArgumentException) cause;
86+
} else {
87+
throw new RuntimeException(e);
88+
}
89+
}
90+
};
91+
assertThrows(IllegalArgumentException.class, invokeJacobi2::run);
92+
}
93+
}

0 commit comments

Comments
 (0)