|
| 1 | +package com.thealgorithms.randomized; |
| 2 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | + |
| 5 | +import java.math.BigInteger; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +/* |
| 9 | +* Tests for MillerRabinPrimality |
| 10 | +* @author DomTr (https://github.com/DomTr) |
| 11 | +*/ |
| 12 | + |
| 13 | +public class MillerRabinPrimalityTest { |
| 14 | + static final int iter = 10; |
| 15 | + |
| 16 | + @Test |
| 17 | + public void testComposites() { |
| 18 | + long[] values = {1, 25, 2932021007403L, 4501680375506332L, 6910906992394051L, |
| 19 | + 4887521073398877L, 5577943644815725L, 6085993686552764L}; |
| 20 | + |
| 21 | + for (long v : values) { |
| 22 | + BigInteger val = BigInteger.valueOf(v); |
| 23 | + assertFalse(MillerRabinPrimality.millerRabin(val, iter)); |
| 24 | + } |
| 25 | + } |
| 26 | + @Test |
| 27 | + public void testPrimes() { |
| 28 | + long[] values = {2, 17, 137, 317, 405857, 2932031007403L, 6333369275038567L}; |
| 29 | + for (long v : values) { |
| 30 | + BigInteger val = BigInteger.valueOf(v); |
| 31 | + assertTrue(MillerRabinPrimality.millerRabin(val, iter)); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // Test all primes |
| 36 | + @Test |
| 37 | + public void testBigPrimes() { |
| 38 | + BigInteger b1 = new BigInteger("423726770669791241889982933129"); |
| 39 | + BigInteger b2 = new BigInteger("728801495170617624430641064729"); |
| 40 | + BigInteger b3 = new BigInteger("715069831641887124233793734953"); |
| 41 | + BigInteger b4 = new BigInteger("214668004859466264786404914307"); |
| 42 | + BigInteger b5 = new BigInteger("107280976690907382021651905569"); |
| 43 | + BigInteger b6 = new BigInteger("194139053422804244228680212551"); |
| 44 | + BigInteger b7 = new BigInteger("225220037755960690862092087151"); |
| 45 | + assertTrue(MillerRabinPrimality.millerRabin(b1, iter)); |
| 46 | + assertTrue(MillerRabinPrimality.millerRabin(b2, iter)); |
| 47 | + assertTrue(MillerRabinPrimality.millerRabin(b3, iter)); |
| 48 | + assertTrue(MillerRabinPrimality.millerRabin(b4, iter)); |
| 49 | + assertTrue(MillerRabinPrimality.millerRabin(b5, iter)); |
| 50 | + assertTrue(MillerRabinPrimality.millerRabin(b6, iter)); |
| 51 | + assertTrue(MillerRabinPrimality.millerRabin(b7, iter)); |
| 52 | + } |
| 53 | + // Tests composite numbers which are products of two big primes |
| 54 | + @Test |
| 55 | + public void testBigComposites() { |
| 56 | + BigInteger p1 = new BigInteger("995224294347733"); // all 4 primes |
| 57 | + BigInteger p2 = new BigInteger("990601545052177"); |
| 58 | + BigInteger p3 = new BigInteger("924286031819653"); |
| 59 | + BigInteger p4 = new BigInteger("408464000499539"); |
| 60 | + |
| 61 | + |
| 62 | + assertFalse(MillerRabinPrimality.millerRabin(p1.multiply(p1), iter)); |
| 63 | + assertFalse(MillerRabinPrimality.millerRabin(p1.multiply(p2), iter)); |
| 64 | + assertFalse(MillerRabinPrimality.millerRabin(p1.multiply(p3), iter)); |
| 65 | + assertFalse(MillerRabinPrimality.millerRabin(p1.multiply(p4), iter)); |
| 66 | + |
| 67 | + assertFalse(MillerRabinPrimality.millerRabin(p2.multiply(p2), iter)); |
| 68 | + assertFalse(MillerRabinPrimality.millerRabin(p2.multiply(p3), iter)); |
| 69 | + assertFalse(MillerRabinPrimality.millerRabin(p2.multiply(p4), iter)); |
| 70 | + |
| 71 | + assertFalse(MillerRabinPrimality.millerRabin(p3.multiply(p3), iter)); |
| 72 | + assertFalse(MillerRabinPrimality.millerRabin(p3.multiply(p4), iter)); |
| 73 | + |
| 74 | + assertFalse(MillerRabinPrimality.millerRabin(p4.multiply(p4), iter)); |
| 75 | + } |
| 76 | +} |
0 commit comments