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

Uploaded CPython 3.13macOS 15.0+ ARM64

FindAFactor-6.5.1-cp313-cp313-macosx_14_0_arm64.whl (254.1 kB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

FindAFactor-6.5.1-cp313-cp313-macosx_13_0_x86_64.whl (266.6 kB view details)

Uploaded CPython 3.13macOS 13.0+ x86-64

FindAFactor-6.5.1-cp312-cp312-win_amd64.whl (276.7 kB view details)

Uploaded CPython 3.12Windows x86-64

FindAFactor-6.5.1-cp312-cp312-manylinux_2_39_x86_64.whl (272.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

FindAFactor-6.5.1-cp310-cp310-manylinux_2_35_x86_64.whl (278.6 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.35+ x86-64

FindAFactor-6.5.1-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.1.tar.gz.

File metadata

  • Download URL: findafactor-6.5.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.5.1.tar.gz
Algorithm Hash digest
SHA256 7e63f0eac89ad5afbc7b3e2c80975eccf8f26471c3af907d45d7de8772108ec0
MD5 e27fa3ddf5b7cdae112074694e977937
BLAKE2b-256 0540ce3465f899f9c89dc398a132fe082989c3a2cac639aa17091784b2758e2a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.1-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 703ba4435ecc21a26cb8d0e52987d5445817de52591f028424c4f05a4f973ab1
MD5 87e30e3254d76333c80e9433700a9b55
BLAKE2b-256 a30cd1191d98dc0c2322b71da4a566288e08fa6eb5c7646e3edc13f4a2e5cbf5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.1-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 8f7039ee0d9d5a9fe6946126563f4a62fd1172c01db1615b83f799a13bc47c2d
MD5 509f461a421ae6f3dcb14bad9b54b0b5
BLAKE2b-256 298db83c2c0bd0003457dc05fb3efac3fdc18103f8f347c85ab1a43b484e2201

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.1-cp313-cp313-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 ab5909e9a96f24d3c343eb44b0b8c9979a20b0a68c684d426bf3cbb2db233f5c
MD5 0f552e6f1017ca6f4bc9930a09104577
BLAKE2b-256 f72bcc070f8836cca398296aefe8551fb6c78e7df39682cbdc1d2e4245817d18

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 26e119ac0966cf7f86a590db45f210eae8282a36af34d0c387737f5fcca65920
MD5 23e2f83c1c180d85181c74335c25d13b
BLAKE2b-256 b1cc60ba4708ee94ea8a536b0945a5c7600dc8b46ca9826787a7d3b87921d68f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.1-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 c16b15e8aacafafa075d3a322ad5772f487dc5054a8d2063e9a0c2cfd5e9ec7c
MD5 da1cdc717919e455825d7b2003799dc9
BLAKE2b-256 13fcb1cf9504a9a95ecde22d8b0e2376cba06460883b14b1354fe5d3e0fab76a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.1-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 e670698d9a19c37f177d4cbb0e6d59a15ddfd685c16851957c0669b7f94032d6
MD5 16801f6214508fdee1a2916f0f1aa041
BLAKE2b-256 73d55b83e906139939f6418b7992a3312d19050b52501534431a1e665f49e77c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.1-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 235e3015b4c80d1968fde98f43a7990c08398487733024c9001a2a7be5094d8e
MD5 b2c1017b00862cf8f0e3eae9e7c053e9
BLAKE2b-256 b74037086152d99ef9678674f6474e5c04b33f6ea743444bfeaef2332b69be50

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