Skip to content

perf(core): optimize isPrime with 6k±1 algorithm and add benchmarks#5

Merged
jkyberneees merged 1 commit intomainfrom
improve-isPrime-performance
Jan 18, 2026
Merged

perf(core): optimize isPrime with 6k±1 algorithm and add benchmarks#5
jkyberneees merged 1 commit intomainfrom
improve-isPrime-performance

Conversation

@jkyberneees
Copy link
Contributor

Optimize isPrime function using the 6k±1 optimization, which reduces iterations by ~33% since all primes > 3 are of the form 6k±1.

Changes:

  • Use 6k±1 pattern for trial division instead of checking all odd numbers
  • Add early exit for divisibility by 3
  • Add comprehensive benchmark covering edge cases, project primes, and worst-case scenarios

All cases maintain zero allocations.

Optimize isPrime function using the 6k±1 optimization, which reduces
iterations by ~33% since all primes > 3 are of the form 6k±1.

Changes:
- Use 6k±1 pattern for trial division instead of checking all odd numbers
- Add early exit for divisibility by 3
- Add comprehensive benchmark covering edge cases, project primes,
  and worst-case scenarios

All cases maintain zero allocations.
@jkyberneees jkyberneees merged commit 9457772 into main Jan 18, 2026
3 checks passed
@jkyberneees jkyberneees deleted the improve-isPrime-performance branch January 18, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant