|
| 1 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 2 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 3 | + |
| 4 | +import java.util.*; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +public class SegmentedSieveTest { |
| 8 | + |
| 9 | + // The segmented sieve algorithm implementation |
| 10 | + public static void fillPrime(List<Integer> chprime, int high) { |
| 11 | + boolean[] ck = new boolean[high + 1]; |
| 12 | + Arrays.fill(ck, true); |
| 13 | + ck[1] = false; |
| 14 | + ck[0] = false; |
| 15 | + |
| 16 | + for (int i = 2; (i * i) <= high; i++) { |
| 17 | + if (ck[i]) { |
| 18 | + for (int j = i * i; j <= high; j = j + i) { |
| 19 | + ck[j] = false; |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + for (int i = 2; i <= high; i++) { |
| 24 | + if (ck[i]) { |
| 25 | + chprime.add(i); |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public static List<Integer> segmentedSieve(int low, int high) { |
| 31 | + if (low < 2) throw new IllegalArgumentException("Low must be greater than or equal to 2."); |
| 32 | + if (low > high) throw new IllegalArgumentException("Low cannot be greater than high."); |
| 33 | + |
| 34 | + List<Integer> chprime = new ArrayList<>(); |
| 35 | + fillPrime(chprime, (int) Math.sqrt(high)); |
| 36 | + |
| 37 | + boolean[] prime = new boolean[high - low + 1]; |
| 38 | + Arrays.fill(prime, true); |
| 39 | + |
| 40 | + for (int primeNum : chprime) { |
| 41 | + int lower = (low / primeNum) * primeNum; |
| 42 | + if (lower < low) lower += primeNum; |
| 43 | + if (lower == primeNum) lower += primeNum; |
| 44 | + |
| 45 | + for (int j = lower; j <= high; j += primeNum) { |
| 46 | + prime[j - low] = false; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + List<Integer> primesInRange = new ArrayList<>(); |
| 51 | + for (int i = low; i <= high; i++) { |
| 52 | + if (prime[i - low]) { |
| 53 | + primesInRange.add(i); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return primesInRange; |
| 58 | + } |
| 59 | + |
| 60 | + // JUnit test cases |
| 61 | + @Test |
| 62 | + public void testSegmentedSieveSmallRange() { |
| 63 | + List<Integer> expectedPrimes = Arrays.asList(2, 3, 5, 7); |
| 64 | + assertEquals(expectedPrimes, segmentedSieve(2, 10)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testSegmentedSieveLargeRange() { |
| 69 | + List<Integer> expectedPrimes = Arrays.asList(99991, 99997); |
| 70 | + assertEquals(expectedPrimes, segmentedSieve(99990, 100000)); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testSegmentedSieveSingleNumberRange() { |
| 75 | + List<Integer> expectedPrime = Arrays.asList(7); |
| 76 | + assertEquals(expectedPrime, segmentedSieve(7, 7)); |
| 77 | + |
| 78 | + List<Integer> expectedEmpty = new ArrayList<>(); |
| 79 | + assertEquals(expectedEmpty, segmentedSieve(8, 8)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testSegmentedSieveWithInvalidRange() { |
| 84 | + assertThrows(IllegalArgumentException.class, () -> segmentedSieve(-10, 10)); |
| 85 | + assertThrows(IllegalArgumentException.class, () -> segmentedSieve(20, 10)); |
| 86 | + } |
| 87 | +} |
0 commit comments