Skip to content

Commit d9872de

Browse files
williamfisetclaude
andcommitted
Refactor IsPrime: rename to PrimalityCheck, add docs and 6k±1 explanation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ba8ae7c commit d9872de

4 files changed

Lines changed: 54 additions & 35 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ $ java -cp classes com.williamfiset.algorithms.search.BinarySearch
248248
- [Greatest Common Divisor (GCD)](src/main/java/com/williamfiset/algorithms/math/Gcd.java) **- ~O(log(a + b))**
249249
- [Fast Fourier transform (quick polynomial multiplication)](src/main/java/com/williamfiset/algorithms/math/FastFourierTransform.java) **- O(nlog(n))**
250250
- [Fast Fourier transform (quick polynomial multiplication, complex numbers)](src/main/java/com/williamfiset/algorithms/math/FastFourierTransformComplexNumbers.java) **- O(nlog(n))**
251-
- [Primality check](src/main/java/com/williamfiset/algorithms/math/IsPrime.java) **- O(√n)**
251+
- [Primality check](src/main/java/com/williamfiset/algorithms/math/PrimalityCheck.java) **- O(√n)**
252252
- [Primality check (Rabin-Miller)](src/main/java/com/williamfiset/algorithms/math/RabinMillerPrimalityTest.py) **- O(k)**
253253
- [Least Common Multiple (LCM)](src/main/java/com/williamfiset/algorithms/math/Lcm.java) **- ~O(log(a + b))**
254254
- [Modular inverse](src/main/java/com/williamfiset/algorithms/math/ModularInverse.java) **- ~O(log(a + b))**

src/main/java/com/williamfiset/algorithms/math/BUILD

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ java_binary(
4242
runtime_deps = [":math"],
4343
)
4444

45-
# bazel run //src/main/java/com/williamfiset/algorithms/math:IsPrime
45+
# bazel run //src/main/java/com/williamfiset/algorithms/math:PrimalityCheck
4646
java_binary(
47-
name = "IsPrime",
48-
main_class = "com.williamfiset.algorithms.math.IsPrime",
47+
name = "PrimalityCheck",
48+
main_class = "com.williamfiset.algorithms.math.PrimalityCheck",
4949
runtime_deps = [":math"],
5050
)
5151

src/main/java/com/williamfiset/algorithms/math/IsPrime.java

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Tests whether a number is prime using trial division. Skips multiples of 2 and 3 by iterating
3+
* over numbers of the form 6k ± 1.
4+
*
5+
* <p>Why 6k ± 1? Every integer falls into one of six residue classes mod 6:
6+
*
7+
* <pre>
8+
* 6k + 0 → divisible by 6 (= 2·3) → not prime
9+
* 6k + 1 → not divisible by 2 or 3 → candidate ✓
10+
* 6k + 2 → divisible by 2 → not prime
11+
* 6k + 3 → divisible by 3 → not prime
12+
* 6k + 4 → divisible by 2 → not prime
13+
* 6k + 5 → not divisible by 2 or 3 → candidate ✓ (same as 6k - 1)
14+
* </pre>
15+
*
16+
* After checking 2 and 3 directly, all remaining primes must be of the form 6k ± 1, so we only
17+
* need to test divisibility by those candidates.
18+
*
19+
* <p>Time: O(√n)
20+
*
21+
* @author Micah Stairs, William Fiset
22+
*/
23+
package com.williamfiset.algorithms.math;
24+
25+
public class PrimalityCheck {
26+
27+
/** Returns true if n is a prime number. */
28+
public static boolean isPrime(long n) {
29+
if (n < 2)
30+
return false;
31+
if (n == 2 || n == 3)
32+
return true;
33+
if (n % 2 == 0 || n % 3 == 0)
34+
return false;
35+
long limit = (long) Math.sqrt(n);
36+
for (long i = 5; i <= limit; i += 6)
37+
if (n % i == 0 || n % (i + 2) == 0)
38+
return false;
39+
return true;
40+
}
41+
42+
public static void main(String[] args) {
43+
System.out.println(isPrime(5)); // true
44+
System.out.println(isPrime(31)); // true
45+
System.out.println(isPrime(1433)); // true
46+
System.out.println(isPrime(8763857775536878331L)); // true
47+
System.out.println(isPrime(4)); // false
48+
System.out.println(isPrime(15)); // false
49+
}
50+
}

0 commit comments

Comments
 (0)