Documentation
¶
Overview ¶
Package prime is a pure-Go (no cgo) reimplementation of Ruby's `prime` standard library — the deterministic, interpreter-independent core of MRI 4.0.5's Prime class and the Integer#prime? / Integer#prime_division refinements.
It exposes the prime generator (Prime.each / Prime.take / Prime.first), the primality test (Prime.prime? / Integer#prime?), and the prime factorisation (Prime.prime_division / Integer#prime_division) together with its inverse (Prime.int_from_prime_division), all matching MRI byte-for-byte on the integer value model.
Primality trial-divides by the small primes up to 37 for tiny inputs. Any value that fits in a uint64 is then settled by a deterministic Miller–Rabin test over the smallest witness set proven to have no composite counterexample for that magnitude ({2,7,61} below ~4.76e9, growing to the twelve-base set {2,3,5,7,…,37} for the whole 64-bit range). Each round runs in machine-word arithmetic with no heap allocation: below 2^32 a plain uint64 multiply suffices, and for the full range Montgomery multiplication replaces the per-step 64-bit division with a multiply, add and shift. Genuinely larger values fall through to a deterministic Baillie–PSW test (a strong base-2 Miller–Rabin combined with a strong Lucas test) evaluated with math/big. Both paths agree exactly on the 64-bit range (each is proven exact there) and BPSW remains a correct probable-prime test beyond it — exactly the guarantee MRI's own generator relies on.
Index ¶
- func Each(ubound int64, yield func(p *big.Int) bool)
- func EachPrime() func() *big.Int
- func First(n int) []*big.Int
- func Int(pairs [][2]*big.Int) *big.Int
- func IsPrime(n *big.Int) bool
- func Next(n *big.Int) *big.Int
- func Prev(n *big.Int) *big.Int
- func PrimeDivision(n *big.Int) [][2]*big.Int
- func PrimeDivisionErr(n *big.Int) ([][2]*big.Int, error)
- func Take(n int) []*big.Int
- type ZeroError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Each ¶
Each yields every prime p with p <= ubound, in ascending order, calling yield for each. If yield returns false, iteration stops early. This is the bounded form of Ruby's Prime.each(ubound) { |p| ... }; the unbounded generator is served by Take / First / EachPrime, which never need an upper bound.
A non-positive ubound yields nothing.
func EachPrime ¶
EachPrime returns a stateful generator: each call returns the next prime, starting at 2 and continuing forever (it is the unbounded Prime.each enumerator). It is the building block behind the Prev / Next cursor and reads from the shared memoized sieve, so successive calls are amortised O(1).
func Int ¶
Int reconstructs an integer from a prime-division slice, matching Ruby's Prime.int_from_prime_division: it multiplies prime**exponent over the pairs (a leading [-1, 1] applies the sign). Exponents are taken as non-negative integers; the result is exact for the integer case MRI's Integer model uses.
func IsPrime ¶
IsPrime reports whether n is prime, matching Ruby's Prime.prime?(n) / Integer#prime?. Numbers < 2 (including negatives and zero) are not prime; 2 and 3 are prime; every Carmichael number and Miller–Rabin/Lucas pseudoprime in the 64-bit range is correctly rejected.
n is not mutated.
func Prev ¶
Prev returns the largest prime strictly less than n, or nil when none exists (n <= 2). n is not mutated.
func PrimeDivision ¶
PrimeDivision returns the prime factorisation of n as Ruby's Prime.prime_division(n) / Integer#prime_division does: a slice of [prime, exponent] pairs in ascending prime order. For negative n a leading [-1, 1] pair carries the sign (matching MRI). It panics with ZeroError for n == 0, mirroring MRI's ZeroDivisionError; PrimeDivisionErr is the non-panicking form.
n is not mutated.
func PrimeDivisionErr ¶
PrimeDivisionErr is PrimeDivision without the panic: it returns a ZeroError for n == 0 instead of panicking. The factorisation is otherwise identical.
