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=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_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 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.4.3

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

Uploaded CPython 3.13macOS 15.0+ ARM64

FindAFactor-6.4.3-cp313-cp313-macosx_14_0_arm64.whl (118.8 kB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

FindAFactor-6.4.3-cp313-cp313-macosx_13_0_x86_64.whl (134.2 kB view details)

Uploaded CPython 3.13macOS 13.0+ x86-64

FindAFactor-6.4.3-cp312-cp312-win_amd64.whl (154.6 kB view details)

Uploaded CPython 3.12Windows x86-64

FindAFactor-6.4.3-cp312-cp312-manylinux_2_39_x86_64.whl (139.2 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

FindAFactor-6.4.3-cp310-cp310-manylinux_2_35_x86_64.whl (144.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.35+ x86-64

FindAFactor-6.4.3-cp38-cp38-manylinux_2_31_x86_64.whl (138.7 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.31+ x86-64

File details

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

File metadata

  • Download URL: findafactor-6.4.3.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.4.3.tar.gz
Algorithm Hash digest
SHA256 60a46b7212be1a73b32aaa572baf82a09e975b8df2948c362b2789847e24578b
MD5 b424bdf02f90b13e98898086ce435e04
BLAKE2b-256 3f1e9a5ef790a2dd117c734572911d90a215adc6da9636db0c80bab125c1beec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.4.3-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 5c0fb845edec6f80455a8d400e00aefa1f76178a53ffd2497f751f0341faac7f
MD5 ce7b3865c75c59212070a77c0aa4442a
BLAKE2b-256 e3b94b80968919650c7dac16704349ab20f83c59e8a47220ec3c8d6a30c81060

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.4.3-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 f432830052c44e6568f1aed98d59b2ab1c8374245657d4acb1eedf134220af3f
MD5 73a3df354c512bc17c0c969b0905bb97
BLAKE2b-256 3bd1f210786305b440de131ef99a506feb557c0f48c77915b8701fda7184ac64

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.4.3-cp313-cp313-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 90457ac97207f425a564ecbf1a7fe838d578f9ef14364d9f29677f8acde9d220
MD5 f109ab14f9010e9fe984b7d943d2a640
BLAKE2b-256 086b896c21c9ec97a637761a9b64bda424674e4d0c7d16add474499e55a96d3c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.4.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d2b7cd481137891260041099317ec20c30305ba3852deacaa5fced6a98bb999f
MD5 4c20db636bd9b704c2a5b486543abf4a
BLAKE2b-256 818027c8703860a0cf0f48df1e0bc01cb6fc4ed0e0d70adf42ea0f501a8edc7d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.4.3-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 e5767eb68c5ecaf5b605ff862d8021236dd1de5c8adeef9d8f22aa0e3071eccb
MD5 9e1ff66861485dcf02faaa288d94da5e
BLAKE2b-256 0ace8d985f62658a549a60f0ee337650c49c0f537342a36bfc00539b98b1da2d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.4.3-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 8ecd9a5760c0512057a50f3ca00e780500ac3384e6a93b53f9ad17203eeaf492
MD5 70d5952b365352767e538f48164d7c5c
BLAKE2b-256 347a1ef53f43b26b6763f7e0aad0323ef8807a9a64e99b8d4aa103de8c193a12

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.4.3-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 6df8766c507e99ac1ca4ace63dd9dad003ce5907f171e03c95caba238c6faf96
MD5 4d8da8e897be97bef5f9c96ba01dda27
BLAKE2b-256 5a4f4853cac3cc8e2bed5a3a32da5ebd78652ed8c4710414083bd6e9c87c7ba3

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