Skip to main content

Find any nontrivial factor of a number

Project description

FindAFactor

Find any nontrivial factor of a number

PyPI Downloads

Copyright and license

(c) Daniel Strano and the Qrack contributors 2017-2025. All rights reserved.

Installation

From PyPi:

pip3 install FindAFactor

From Source: install pybind11, then

pip3 install .

in the root source directory (with setup.py).

Windows users might find Windows Subsystem Linux (WSL) to be the easier and preferred choice for installation.

Usage

from FindAFactor import find_a_factor, FactoringMethod

to_factor = 1000

factor = find_a_factor(
    to_factor,
    method=FactoringMethod.PRIME_PROVER,
    node_count=1, node_id=0,
    gear_factorization_level=23,
    wheel_factorization_level=13,
    sieving_bound_multiplier=1.0,
    smoothness_bound_multiplier=1.0,
    gaussian_elimination_row_offset=3,
    check_small_factors=False,
    wheel_primes_excluded=[]
)

The find_a_factor() function should return any nontrivial factor of to_factor (that is, any factor besides 1 or to_factor) if it exists. If a nontrivial factor does not exist (i.e., the number to factor is prime), the function will return 1 or the original to_factor.

  • method (default value: PRIME_PROVER/0): PRIME_PROVER/0 will prove that a number is prime (by failing to find any factors with wheel and gear factorization). FACTOR_FINDER/1 is optimized for large numbers the assumption that the number has at least two nontrivial factors. POLLARD_RHO/2 handles the middle size range (and is automatically applied as a pre-check during FACTOR_FINDER).
  • node_count (default value: 1): FindAFactor can perform factorization in a distributed manner, across nodes, without network communication! When node_count is set higher than 1, the search space for factors is segmented equally per node. If the number to factor is semiprime, and brute-force search is used instead of congruence of squares, for example, all nodes except the one that happens to contain the (unknown) prime factor less than the square root of to_factor will ultimately return 1, while one node will find and return this factor. For best performance, every node involved in factorization should have roughly the same CPU throughput capacity. For FACTOR_FINDER mode, this splits the sieving range between nodes, but it does not actually coordinate Gaussian elimination rows between nodes.
  • node_id (default value: 0): This is the identifier of this node, when performing distributed factorization with node_count higher than 1. node_id values start at 0 and go as high as (node_count - 1).
  • gear_factorization_level (default value: 23): This is the value up to which "wheel (and gear) factorization" are applied to "brute force." A value of 23 includes all prime factors of 23 and below and works well for PRIME_PROVER, though significantly higher might be preferred in certain cases. In FACTOR_FINDER, one probably wants to avoid setting a different gear level than wheel level.
  • wheel_factorization_level (default value: 13): "Wheel" vs. "gear" factorization balances two types of factorization wheel ("wheel" vs. "gear" design) that often work best when the "wheel" is only a few prime number levels lower than gear factorization. For PRIME_PROVER, optimized implementation for wheels is only available up to 17; for FACTOR_FINDER, wheels are constructed programmatically while avoiding wheel_primes_excluded entries, so there is no fixed ceiling. The primes above "wheel" level, up to "gear" level, are the primes used specifically for "gear" factorization. For FACTOR_FINDER method, wheel factorization is applied to map the sieving interval onto non-multiples on the wheel, if the level is set above 1.
  • sieving_bound_multiplier (default value: 1.0): This controls the sieving bound and is calibrated such that it linearly multiplies the number to factor minus its square root (for a full 1.0 increment, which is maximum). While this might be a huge bound, remember that sieving termination is primarily controlled by when gaussian_elimination_row_multiplier is exactly satisfied.
  • smoothness_bound_multiplier (default value: 1.0): This controls smoothness bound and is calibrated such that it linearliy multiplies pow(exp(0.5 * sqrt(log(N) * log(log(N)))), sqrt(2.0)/4) for N being the number to factor (for each 1.0 increment). This was a heuristic suggested by Elara (an OpenAI custom GPT).
  • gaussian_elimination_row_offset (default value: 1): This controls the number of rows greater than the count of smooth primes that are sieved before Gaussian elimination. Basically, for each increment starting with 1, the chance of finding at least one solution in Gaussian elimination goes like (1 - 2^(-m)) for a setting value of m: 1 value is a 50% chance of success, and the chance of failure is halved for each unit of 1 added. So long as this setting is appropriately low enough, sieving_bound_multiplier can be set basically arbitrarily high.
  • check_small_factors (default value: False): True performs initial-phase trial division up to the smoothness bound, and False skips it.
  • wheel_primes_excluded (default value: []): If using FACTOR_FINDER method, these specific primes are excluded from wheel and gear factorization (up to wheel_factorization_level and gear_factorization_level). (See wheel_tuner.py in the project root for guidance on which primes to exclude and include, based empirically upon a sample list of smooth numbers for your particular number to factor.)

All variables defaults can also be controlled by environment variables:

  • FINDAFACTOR_METHOD (integer value)
  • FINDAFACTOR_NODE_COUNT
  • FINDAFACTOR_NODE_ID
  • FINDAFACTOR_GEAR_FACTORIZATION_LEVEL
  • FINDAFACTOR_WHEEL_FACTORIZATION_LEVEL
  • FINDAFACTOR_SIEVING_BOUND_MULTIPLIER
  • FINDAFACTOR_SMOOTHNESS_BOUND_MULTIPLIER
  • FINDAFACTOR_GAUSSIAN_ELIMINATION_ROW_OFFSET
  • FINDAFACTOR_CHECK_SMALL_FACTORS (True if set at all, otherwise False)
  • FINDAFACTOR_WHEEL_PRIMES_EXCLUDED (comma-separated prime numbers)

About

This library was originally called "Qimcifa" and demonstrated a (Shor's-like) "quantum-inspired" algorithm for integer factoring. It has since been developed into a general factoring algorithm and tool.

Special thanks to OpenAI GPT "Elara," for help with indicated region of contributed code! Anthropic Claude also contributed Pollard's Rho!

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

findafactor-6.10.0.tar.gz (266.7 kB view details)

Uploaded Source

Built Distributions

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

findafactor-6.10.0-cp314-cp314-macosx_15_0_arm64.whl (550.3 kB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

findafactor-6.10.0-cp314-cp314-macosx_14_0_arm64.whl (550.8 kB view details)

Uploaded CPython 3.14macOS 14.0+ ARM64

findafactor-6.10.0-cp312-cp312-win_amd64.whl (577.4 kB view details)

Uploaded CPython 3.12Windows x86-64

findafactor-6.10.0-cp312-cp312-manylinux_2_39_x86_64.whl (555.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

findafactor-6.10.0-cp310-cp310-manylinux_2_35_x86_64.whl (308.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.35+ x86-64

File details

Details for the file findafactor-6.10.0.tar.gz.

File metadata

  • Download URL: findafactor-6.10.0.tar.gz
  • Upload date:
  • Size: 266.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for findafactor-6.10.0.tar.gz
Algorithm Hash digest
SHA256 341946d7537a05b8c24ff0af0bcec83f684203a064c23d26c6441443c0ea4537
MD5 4d5337aba46ed825e7b2b711422e2b32
BLAKE2b-256 cac1411c4edb5331b097d8907a7f6ec965bf273a640fb333a1957435a0ba4b5f

See more details on using hashes here.

File details

Details for the file findafactor-6.10.0-cp314-cp314-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for findafactor-6.10.0-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 5dde889c497e36ce5be25a80fe795d32b8bd8466e2487240a3decd3137b42300
MD5 bbc06dddbc0e01f8e54e3af7343043d5
BLAKE2b-256 a1d8ae02d97675da821833dc241f45ca5c4698e6d6cebda535f880e43d387f18

See more details on using hashes here.

File details

Details for the file findafactor-6.10.0-cp314-cp314-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for findafactor-6.10.0-cp314-cp314-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 fe2655317c390f749b1ef569a52f56bdf313d3c7befc5e98d0c37dbd8878e4e0
MD5 6d6a0ee90601444f83b8615e4c08f5a6
BLAKE2b-256 a6122296170af86566e227b0829e1da3dfb0b8d2873b3dfe60e8850ae783d4c5

See more details on using hashes here.

File details

Details for the file findafactor-6.10.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for findafactor-6.10.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 546bf1ad8030c1712da642170934f495af0c9a64148d3986f352d406ab433b15
MD5 757f3515c9cae22b3e9ba3276e4ac088
BLAKE2b-256 246e280452eb60f17bcfbe7425d4190aea9a29f49fd5a0e59e925c0324f11d11

See more details on using hashes here.

File details

Details for the file findafactor-6.10.0-cp312-cp312-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for findafactor-6.10.0-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 8e927fa66e7380fc9fd771b70ca9254cac1e9197fc91f748b824f0ba2a91f28e
MD5 de4f86de4e900a265be5d9114f20a7fe
BLAKE2b-256 a73e96c1a14543992f4dba0715cfe5214454c436ed19d54bfeb4c46944ff2351

See more details on using hashes here.

File details

Details for the file findafactor-6.10.0-cp310-cp310-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for findafactor-6.10.0-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 b84389151e3a48dae22db72e734206c381414006ef9370a228ff85521e10c90d
MD5 b785f1839ce2011600a8fa355d15d421
BLAKE2b-256 c0f22f6b7ce9ca8db1ba277288dac7d6eb82ef8c48d8bb51f0dda2c3d622473f

See more details on using hashes here.

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