A package for generating all factorizations sequentially given some primes. Can be paused and resumed.
Project description
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 cypari import pari
from factorgen import Factorizations
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 ≡ 1 mod 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file factorgen-0.0.1.tar.gz.
File metadata
- Download URL: factorgen-0.0.1.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39962075a14b7daa089009c6e234129253d4344b20ae60969cf76bb61bfed5ec
|
|
| MD5 |
9ad60bb9e56360df0de7bf80df36bd56
|
|
| BLAKE2b-256 |
562afe82273a85bb2c7e9ea77956b45adc46c704b0176548f6c9c9727b684641
|
Provenance
The following attestation bundles were made for factorgen-0.0.1.tar.gz:
Publisher:
ci.yml on rheard/factorgen
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
factorgen-0.0.1.tar.gz -
Subject digest:
39962075a14b7daa089009c6e234129253d4344b20ae60969cf76bb61bfed5ec - Sigstore transparency entry: 927065930
- Sigstore integration time:
-
Permalink:
rheard/factorgen@1d86bf3237d4a775bd5399615c9837ee7448b470 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/rheard
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@1d86bf3237d4a775bd5399615c9837ee7448b470 -
Trigger Event:
push
-
Statement type:
File details
Details for the file factorgen-0.0.1-py3-none-any.whl.
File metadata
- Download URL: factorgen-0.0.1-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3385a69132c9c3f62a8981cfc478894b18660968375ea921408662f3af145274
|
|
| MD5 |
797fcb560616bf60804f8729351005b8
|
|
| BLAKE2b-256 |
53942cdb350d7321928f9f96be81e846669ab092b86a6731abccc8ad5bc920d2
|
Provenance
The following attestation bundles were made for factorgen-0.0.1-py3-none-any.whl:
Publisher:
ci.yml on rheard/factorgen
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
factorgen-0.0.1-py3-none-any.whl -
Subject digest:
3385a69132c9c3f62a8981cfc478894b18660968375ea921408662f3af145274 - Sigstore transparency entry: 927065934
- Sigstore integration time:
-
Permalink:
rheard/factorgen@1d86bf3237d4a775bd5399615c9837ee7448b470 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/rheard
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@1d86bf3237d4a775bd5399615c9837ee7448b470 -
Trigger Event:
push
-
Statement type: