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. In FACTOR_FINDER, one probably wants to avoid setting a different gear level than wheel level.
  • 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. For PRIME_PROVER, optimized implementation for wheels is only available up to 13; for FACTOR_FINDER, wheels are constructed programmatically while avoiding smooth primes with quadratic residues, 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 (which might not actually pay dividends in practical complexity, but we leave it for your experimentation).
  • 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

This version

6.3.1

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.3.1.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.3.1-cp313-cp313-macosx_15_0_arm64.whl (118.8 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

FindAFactor-6.3.1-cp313-cp313-macosx_14_0_arm64.whl (118.3 kB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

FindAFactor-6.3.1-cp313-cp313-macosx_13_0_x86_64.whl (133.9 kB view details)

Uploaded CPython 3.13macOS 13.0+ x86-64

FindAFactor-6.3.1-cp312-cp312-win_amd64.whl (153.2 kB view details)

Uploaded CPython 3.12Windows x86-64

FindAFactor-6.3.1-cp312-cp312-manylinux_2_39_x86_64.whl (139.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

FindAFactor-6.3.1-cp310-cp310-manylinux_2_35_x86_64.whl (144.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.35+ x86-64

FindAFactor-6.3.1-cp38-cp38-manylinux_2_31_x86_64.whl (140.2 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.31+ x86-64

File details

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

File metadata

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

File hashes

Hashes for findafactor-6.3.1.tar.gz
Algorithm Hash digest
SHA256 b6675133c215174c0892862952df6209973f4a8a79428f858b7ea1b8b38e9fdf
MD5 e810dfd86eb1402fa63d56048ba90932
BLAKE2b-256 025038b083241a737db9fb329970a5d2a1f26287490f5a7ec10c2bd26595ae70

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.3.1-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 8f5e705a91f213b4a406a218db896d2749f710be084675c616bae0d0142a5a96
MD5 5b19687dbb5c0c44876fda6a88023059
BLAKE2b-256 02d45e872c8bedcbf60fe7b3e1e8433bf4864b20622345effc17d5b01d7b97a2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.3.1-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 ae5a3c2397b3f99558bbc414a7076fc427b1201855656f37dcfed82cca4930d5
MD5 1a84d46cf55a70532c0dcb548761396f
BLAKE2b-256 a318e9cd2c358d0c15b523fcfc108191c4f8744202b04e11259481a4465b34b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.3.1-cp313-cp313-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 d857cc2b6ebbc150f3ad2b4fbc3d3296e8bbb4cc6ea272d6e038e0625c26823e
MD5 f23175c2a1049448dcc6691e32d2fb0b
BLAKE2b-256 d71bcd2afcc1c5f7720a2ebccb67333446ef66886f3b8448dfd87772cd6feb0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.3.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 3d0bc3a1e5c616dcddbd9c994003a8e54543c6c175540e92e08b2dde6ac963ed
MD5 31bac5f96a5aa5d28c91af6eb702c2f0
BLAKE2b-256 429bbdd1b14eeec0a4b868138ab1560eee7c04cd840cf5839f5040bf7f39eef0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.3.1-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 d9be6793869697d44fabf5eb5c7dc672321a11656daee2f2b998951448bd9c5f
MD5 2cc50353eb2f00349f240b3f038a8b50
BLAKE2b-256 6b8df1b73b390032da2063b50bacc0be6a1e40631e8a6b173dbc36d5227dda10

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.3.1-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 d904d6cf51baab00c7f68c538c2fff4112dc9dc81bbc65fe7eb5d0cc024cf6da
MD5 a8802f5c3b20be62e9a4353c75e97be7
BLAKE2b-256 d7adc3f917fd55d05619cdb6ddb5c18ccf41aefe1a03841d893d28b3bc4d2ef7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.3.1-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 c8562707062877ad1d751dc48a6f754d71765db3322834385896687e3829a29f
MD5 36ab3a9978aae743672668d716225113
BLAKE2b-256 1948e13b38a6ac49878bb52e1f9e52d89c15ca15471f5efb841dd8b001975b8a

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