The generator ready
Take(n) / First(n) return the first n primes (Prime.take / Prime.first); Each(ubound, yield) enumerates every prime p <= ubound (Prime.each(ubound)); EachPrime() is the unbounded cursor.
Ruby's Prime generation, primality testing & factorisation in pure Go — MRI-compatible, no cgo.
go-ruby-prime is a pure-Go (no cgo) reimplementation of Ruby's prime standard library — the deterministic core of MRI 4.0.5's Prime class and the Integer#prime? / Integer#prime_division refinements. It generates the primes, tests primality, factorises an integer and reconstructs it — matching MRI byte-for-byte on the integer value model, with no Ruby runtime. Primality is exact, not probabilistic: small inputs use trial division, everything larger a deterministic Baillie–PSW test (strong base-2 Miller–Rabin + strong Lucas) with no counterexample below 2⁶⁴, so every Carmichael number and strong pseudoprime is rejected; factorisation strips small primes then splits the cofactor with Pollard's rho. It was extracted from rbgo into a reusable standalone library: no dependency on the Ruby runtime, the dependency runs the other way. It is the prime backend for go-embedded-ruby, bound by rbgo as a native module just like go-ruby-regexp, go-ruby-yaml and go-ruby-marshal — differential-tested against MRI, 100% coverage, CI green across 6 arches.
Take(n) / First(n) return the first n primes (Prime.take / Prime.first); Each(ubound, yield) enumerates every prime p <= ubound (Prime.each(ubound)); EachPrime() is the unbounded cursor.
IsPrime(n) mirrors Prime.prime? / Integer#prime? exactly: numbers < 2 are not prime, and every Carmichael number (561, 1105, …) and strong pseudoprime (2047, 3215031751, …) is correctly rejected. Small inputs use trial division; the rest use a deterministic Baillie–PSW test, exact across the whole 64-bit range.
PrimeDivision(n) returns [[p, exp], …] in ascending prime order (Prime.prime_division / Integer#prime_division), with a leading [-1, 1] for negative n and a ZeroError panic (MRI’s ZeroDivisionError) for 0. Large cofactors fall back to Pollard’s rho (Brent’s variant).
Int(pairs) multiplies prime**exp back to the integer (Prime.int_from_prime_division), the inverse of PrimeDivision; Next(n) / Prev(n) step to the adjacent prime.
Every integer flows through *big.Int, so a host can map its own Integer to and from this package without precision loss — the only dependency is math/big.
Deterministic golden tables (which alone hold coverage at 100%) plus a differential oracle: a corpus computed both here and by the system ruby (Prime.prime?, Prime.take, Prime.prime_division, …) and compared; 100% coverage, gofmt + go vet clean, green across all six 64-bit Go arches.
A faithful port of Ruby's Prime in pure Go, cgo disabled, so it cross-compiles and embeds anywhere. It generates primes, tests primality exactly via deterministic Baillie–PSW, factorises with Pollard's rho, and reconstructs the integer — every integer flowing through *big.Int, the only dependency being math/big. Validated differentially against the system ruby binary — Prime.prime?, Prime.take, Prime.prime_division compared on the integer value model. It is a standalone, reusable module extracted from rbgo's internals, and the prime backend for the sibling org github.com/go-embedded-ruby.