Skip to main content

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 nextprime callback (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 to M > N without 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 to load(...) 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 than min_i are skipped)

  • nextprime(p): callback returning the next allowed prime > p

    • If omitted, a cached wrapper around sympy.nextprime is used by default

Methods

  • save(path): checkpoint internal frontier state to a pickle file
  • load(path, max_i=None, nextprime=None): restore a checkpoint and optionally override max_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

factorgen-0.0.1.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

factorgen-0.0.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

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

Hashes for factorgen-0.0.1.tar.gz
Algorithm Hash digest
SHA256 39962075a14b7daa089009c6e234129253d4344b20ae60969cf76bb61bfed5ec
MD5 9ad60bb9e56360df0de7bf80df36bd56
BLAKE2b-256 562afe82273a85bb2c7e9ea77956b45adc46c704b0176548f6c9c9727b684641

See more details on using hashes here.

Provenance

The following attestation bundles were made for factorgen-0.0.1.tar.gz:

Publisher: ci.yml on rheard/factorgen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for factorgen-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3385a69132c9c3f62a8981cfc478894b18660968375ea921408662f3af145274
MD5 797fcb560616bf60804f8729351005b8
BLAKE2b-256 53942cdb350d7321928f9f96be81e846669ab092b86a6731abccc8ad5bc920d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for factorgen-0.0.1-py3-none-any.whl:

Publisher: ci.yml on rheard/factorgen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page