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_SOLVER,
    node_count=1, node_id=0,
    gear_factorization_level=11,
    wheel_factorization_level=11,
    sieving_bound_multiplier=1.0,
    smoothness_bound_multiplier=1.0,
    gaussian_elimination_row_offset=3,
    check_small_factors=False
)

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_SOLVER/0): PRIME_SOLVER/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 the assumption that the number has at least two nontrivial factors.
  • 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: 1): This is the value up to which "wheel (and gear) factorization" are applied to "brute force." A value of 11 includes all prime factors of 11 and below and works well for PRIME_PROVER, though significantly higher might be preferred in certain cases.
  • wheel_factorization_level (default value: 1): "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. Optimized implementation for wheels is only available up to 13. The primes above "wheel" level, up to "gear" level, are the primes used specifically for "gear" factorization. Wheel factorization is also applied to map the sieving intervale of FACTOR_FINDER mode onto non-multiples on the wheel, if the level is set above 1 (which might not actually pay dividends in practical complexity, but we leave it for your experimentation). In FACTOR_FINDER mode, wheel factorization multiples are systematically avoided by construction, while gear factorization multiples are just rejected after-the-fact, without special construction to avoid them. (It is possible in theory to implement handling for gear factorization by construction, though, and that might be added in a future release.)
  • 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.

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)

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!

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.1.0.tar.gz (6.0 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.1.0-cp313-cp313-macosx_15_0_arm64.whl (118.0 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

FindAFactor-6.1.0-cp313-cp313-macosx_14_0_arm64.whl (117.9 kB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

FindAFactor-6.1.0-cp313-cp313-macosx_13_0_x86_64.whl (133.0 kB view details)

Uploaded CPython 3.13macOS 13.0+ x86-64

FindAFactor-6.1.0-cp312-cp312-win_amd64.whl (152.0 kB view details)

Uploaded CPython 3.12Windows x86-64

FindAFactor-6.1.0-cp312-cp312-manylinux_2_39_x86_64.whl (138.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

FindAFactor-6.1.0-cp310-cp310-manylinux_2_35_x86_64.whl (143.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.35+ x86-64

FindAFactor-6.1.0-cp38-cp38-manylinux_2_31_x86_64.whl (140.1 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.31+ x86-64

File details

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

File metadata

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

File hashes

Hashes for findafactor-6.1.0.tar.gz
Algorithm Hash digest
SHA256 b9bcbc3474f09018fe8d40775c95266681a864917bd6ab6b50253dc10ea1a707
MD5 867a3bce5c3d6fc73918715e06789020
BLAKE2b-256 1985f380c729b2a064bea570e4b21f5a0971a0611007f9cce35a0425d7c436bc

See more details on using hashes here.

File details

Details for the file FindAFactor-6.1.0-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for FindAFactor-6.1.0-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 d4c952baaadc4eef79e01a68bb31d1ff3c6c37f992d2b3806dd6861fca413cf6
MD5 ce331a535811ba1f0d0cf58dfa292be4
BLAKE2b-256 35ed1eb7f3f84af543c9b570ff65f599aaaf25b2af794034a09bda5e773f8f04

See more details on using hashes here.

File details

Details for the file FindAFactor-6.1.0-cp313-cp313-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for FindAFactor-6.1.0-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 975b57ad512f96fceab99f5ec5f435a4e9f3bfc04e8d1173c00d040c1c1e395d
MD5 2650d34b79ea3f7e6c63bc67ebbd54e1
BLAKE2b-256 9fc7e7b8ff1ab17c64aac99d289adf408994feb0c2af76b6c89224ea3d6c8a64

See more details on using hashes here.

File details

Details for the file FindAFactor-6.1.0-cp313-cp313-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for FindAFactor-6.1.0-cp313-cp313-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 9f09bd3c9ecb839fdc92f235380e69e970e35cb87672bdec962f6ea24751faa2
MD5 1999169b24ae6ae1fd57468e8c1de725
BLAKE2b-256 3b726d8ff18c3a8275ab8c17d008f85594c612c05479c78b57f6ba88ed9cf958

See more details on using hashes here.

File details

Details for the file FindAFactor-6.1.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for FindAFactor-6.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9ba1c2b91572e7be57514caa1acc21cf9aff563eb0bec218ebb8148847c0cf87
MD5 d9315ede21bbcf415e864ce3d52017e5
BLAKE2b-256 d7bbc3f3024e61c79ae530e3059aa3b83b45fd00f657c6474b12b341a22f493b

See more details on using hashes here.

File details

Details for the file FindAFactor-6.1.0-cp312-cp312-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for FindAFactor-6.1.0-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 be23be123a55204a02a61dfd8087a7235a11fa6e1b08690aaf0b934492758923
MD5 feadd8bd4594dcc0c54488d1190e7f42
BLAKE2b-256 d4c6265398a50a4a4ca26ba08f46b3c5f5a7be252d80cae173d2cb03f6e27de9

See more details on using hashes here.

File details

Details for the file FindAFactor-6.1.0-cp310-cp310-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for FindAFactor-6.1.0-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 9d271bc3375bc8d27f45e9581fcd9401648653ea1754226a2d642f86b3467db7
MD5 7809f2bc4f67fb797b437f4dcc43bc97
BLAKE2b-256 d334b8b48d23682439673fe9974eecf53349edd847ba1b10764f56cf4c4b4b57

See more details on using hashes here.

File details

Details for the file FindAFactor-6.1.0-cp38-cp38-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for FindAFactor-6.1.0-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 7e4f123f96a1829faeeec14ee9db77bec1766090e2091e65390d44dadf44e289
MD5 a7cabe16e002037e08181d15dadb3ca8
BLAKE2b-256 2bf38fd4c2f6f6e93844c41edf84d4d7e08cb773c6ae84bac9965e430a8b1691

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