factorgen
factorgen is a fast, resumable generator that enumerates integers together with their prime factorizations without factoring the integers.
Instead of calling factorint(n) for each n, factorgen constructs each next value by
multiplying allowed primes in a canonical order, carrying the factorization along as it goes.
This gives you complete coverage, strictly increasing output,
and no duplicates for the chosen factor set.
Features
- Enumerate factorizations without factoring: yields
(n, {prime: exponent, ...}) - Strictly increasing
n(deterministic order), no repeats - Configurable factor base via a
nextprimecallback (e.g., “only primes $\equiv 1 \bmod 4$”) - Checkpointing:
save()/load()using pickle - Extendable bounds: finish up to
N, save, then reload and continue up toM > Nwithout repeating work
Installation
pip install factorgen
Dependencies:
- Python 3.9+
sympy(used by default for prime stepping)
Quickstart
Enumerate all integers 2..limit with their prime factorizations:
from factorgen import Factorizations
limit = 20_000
gen = Factorizations(limit, min_i=2)
for n, factors in gen:
# factors is a dict: {prime: exponent}
# e.g. 12 -> {2: 2, 3: 1}
...
Examples
Using cyPARI for faster prime stepping
The Factorizations class accepts a nextprime argument for customization of the prime set.
When given a prime (or any number) it should return the next relevant prime.
This can also be used to provide a faster backend, for example by use of cyPARI.
from functools import lru_cache
from cypari import pari
from factorgen import Factorizations
@lru_cache(maxsize=2_000_000)
def nextprime(p: int) -> int:
return int(pari(p).nextprime())
limit = 20_000
gen = Factorizations(limit, min_i=2, nextprime=nextprime)
for n, factors in gen:
# factors is a dict: {prime: exponent}
# e.g. 12 -> {2: 2, 3: 1}
...
Constraining the primes (example: only primes $\equiv 1 \bmod 4$)
As mentioned, you can restrict the prime set using the nextprime argument to numbers whose prime factors come
from a subset by returning the next allowed prime strictly greater than p.
Example: only primes $p$ where $p \bmod 4 \equiv 1$:
from sympy import nextprime
from factorgen import Factorizations
def next_prime_1mod4(p: int) -> int:
q = int(nextprime(p))
while q % 4 != 1:
q = int(nextprime(q))
return q
limit = 20_000
gen = Factorizations(limit, min_i=2, nextprime=next_prime_1mod4)
for n, factors in gen:
# Every yielded n has only primes ≡ 1 (mod 4) in its factorization
assert all(p % 4 == 1 for p in factors)
Checkpoint / Resume (and extend the limit)
Long runs can be checkpointed:
from factorgen import Factorizations
gen = Factorizations(10_000_000, min_i=2)
for idx, (n, factors) in enumerate(gen, start=1):
...
gen.save("state.pkl")
Resume later (optionally extending the limit):
from factorgen import Factorizations
gen = Factorizations.load("state.pkl", max_i=20_000_000)
for n, factors in gen:
...
Important: if you were using a custom
nextprime=..., pass the same function toload(...)as well, so the enumeration continues under the same constraints.
API
Factorizations(max_i: int, min_i: int = 0, nextprime: callable | None = None)
-
max_i: inclusive upper bound on yielded integers, required. -
min_i: lower bound filter (values smaller thanmin_iare skipped) -
nextprime(p): callback returning the next allowed prime >p- If omitted, a cached wrapper around
sympy.nextprimeis used by default
- If omitted, a cached wrapper around
Methods
save(path): checkpoint internal frontier state to a pickle fileload(path, max_i=None, nextprime=None): restore a checkpoint and optionally overridemax_i- Attribute:
yielded(count of yielded values so far)
Background (terminology)
Mathematically, this is constructive enumeration of integers over a factor base (often discussed in terms of smooth-number generation).
factorgen focuses on the practical side: enumerating factorizations without factoring,
with a resumable frontier and a customizable “allowed prime” stream.
Release files for factorgen 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| factorgen-0.0.2.tar.gz | 8.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| factorgen-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.3 kB
Release files / factorgen-0.0.2.tar.gz
| Download URL | factorgen-0.0.2.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e2bf876a78c57ed44c1695cbf3fd7b0f484bf50574f3025288e272a513dfdfd5
|
|
BLAKE2b-256 checksum How to use checksums |
1603520160a057756e2d976b8342656535db3ac75755a366eb8f4940e969feab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 29, 2026.
Transparency logRelease files / factorgen-0.0.2-py3-none-any.whl
| Download URL | factorgen-0.0.2-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
366155374ccb5ee8f3358d75b5f5dbd1e3bb952870b75066bc501fc900abb638
|
|
BLAKE2b-256 checksum How to use checksums |
77be8a81e721abbb5266d0115f87f4e5ad0c4a6452e217d9df23724c3b72b93c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 29, 2026.
Transparency log