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
)

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: 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 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.
  • 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.5.2

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

Uploaded CPython 3.13macOS 15.0+ ARM64

FindAFactor-6.5.2-cp313-cp313-macosx_14_0_arm64.whl (254.2 kB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

FindAFactor-6.5.2-cp313-cp313-macosx_13_0_x86_64.whl (266.7 kB view details)

Uploaded CPython 3.13macOS 13.0+ x86-64

FindAFactor-6.5.2-cp312-cp312-win_amd64.whl (276.8 kB view details)

Uploaded CPython 3.12Windows x86-64

FindAFactor-6.5.2-cp312-cp312-manylinux_2_39_x86_64.whl (272.1 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

FindAFactor-6.5.2-cp310-cp310-manylinux_2_35_x86_64.whl (278.7 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.35+ x86-64

FindAFactor-6.5.2-cp38-cp38-manylinux_2_31_x86_64.whl (271.0 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.31+ x86-64

File details

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

File metadata

  • Download URL: findafactor-6.5.2.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.5.2.tar.gz
Algorithm Hash digest
SHA256 6c42a8ebfbee3a21471a0e5f93af45648a96f59226b77cd8cb6a9c1ba9f1c957
MD5 9bd1bc028d15abec91a6a790b4c4dd64
BLAKE2b-256 a7a6a13dafc766f884d8a5e0c4c45e13b1fe77841adce6b86b27d899b50e3812

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.2-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 e758e1922520888a8c3e0612e323a24216ab20dec389d8522af20941f5956830
MD5 e684d4d68aa50ddf3d2f418927d13b99
BLAKE2b-256 da0f92f3eb68f69794e4c34b3f84c8fb1e5dd40f47474fe746850ef42d0202ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.2-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 8c517dc86e6f01a1c86103e6c1b6ee80f08d17afbd8001fc7894f48d9442594f
MD5 c8e593c7b3ab88dcf6a90f9cc63aca03
BLAKE2b-256 ef668690ee08ad024c25925fc61ce3fe050c1afb8f9283fe8fe5e52976c6ba6c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.2-cp313-cp313-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 40f6ace38b5e45537d7e4c07dc097fc3400216dfad0f76311c8ef1e6a1882ae6
MD5 6d99a659569236e974aa25378f1e9646
BLAKE2b-256 39f762dd48d31231f9a241ef827f43f724195a6c06529dfdd2904c0341d18d60

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ebe37ebf0f99e0c337c48b304d657d27c0a9536524ced312acabb2e129a2fd97
MD5 a35f621f3146337a15f5506a8c979902
BLAKE2b-256 e55a6bcf4fb1eff096cd725ada7f92bdf0055319bb6c75527d21f846b27a3c7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.2-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 d80e0e4c73f40bdbb99c4fd4555df7525e487a6f6d6dfea2684d48951e5b33fb
MD5 f06977d2b439307bd49a80939950482b
BLAKE2b-256 df07b13f5a55787f6a2091cb5191f327d70e84820211040640458e6d50be4cb4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.2-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 2dc09593b62d8aeed1cb15bb6c27db1500983c7b796b3151a29208e146f5dd49
MD5 d4a85bbdc0b20c56dca45ba003d91298
BLAKE2b-256 4cbe884d598b32f0c114797ab6973c2af149cbfb7bdc98282e8fa022eef443da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.2-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 4c8f093e903aea02ea50889e239dc251858f6c5106b95594d42db36b6b6b3bb0
MD5 4a5fbdcfef6b35243b1d509ac6d5382c
BLAKE2b-256 1163de8e05dc5ea2d7c632f8c42641f426afb6a635b19d12c556bf6096350a49

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