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=19,
    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: 29): This is the value up to which "wheel (and gear) factorization" are applied to "brute force." A value of 29 includes all prime factors of 29 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: 17): "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.0

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

Uploaded CPython 3.13macOS 15.0+ ARM64

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

Uploaded CPython 3.13macOS 14.0+ ARM64

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

Uploaded CPython 3.13macOS 13.0+ x86-64

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

Uploaded CPython 3.12Windows x86-64

FindAFactor-6.5.0-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.0-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.0-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.0.tar.gz.

File metadata

  • Download URL: findafactor-6.5.0.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.0.tar.gz
Algorithm Hash digest
SHA256 d60d274c7db3fab69c6fb2d9a7f9cc33887b4ba0a2a64a164222fd6c7cd9c7ba
MD5 7ce7b6a462777ab94c574f6e2d31cf12
BLAKE2b-256 5f4a64c9051c74d3f3567b00d892b22dd20d9c18a4f10544b80987db8ab50468

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.0-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 db4fc535f296af9a8ab86db2ac3afeb5896726086c9146e436730ce065216b83
MD5 27fd5c9317b63c7aac493bf7e70c441f
BLAKE2b-256 77a5528754ddcd85fa154dc170955d2870dda42bc65d652388dcba8f7ae8448f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.0-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 a8cfba3de6928738f747c0b4aca853ed9612aa864b1773b9f6a94779cad8fb3d
MD5 2347c84adb4723d2924a768a1ff6ce1e
BLAKE2b-256 1f5d09847d1fe7461e77528ed6003e569977fc2019bfa84e6ca7034ddfeb70e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.0-cp313-cp313-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 3641fe94b0ab2cc2530b3f3c2e286a79ae23807c19d1f6ee0246342ac8fae8fc
MD5 9abed3918be0ed4b301c1a9c23bb7974
BLAKE2b-256 1e4b35c4d169ab91f8c78839f0e25d615cd245001e90712e7756c98fecab17cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 1d5b8e62bd6b972317f95be8888c6c643e390eae5d24689078ac99eac134dd19
MD5 10350cd8666f382c919ec8b030b38fc2
BLAKE2b-256 5bebc16eb375109abcfdec121f004e3180bf51ad556e7039dac515369d8acc23

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.0-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 4b10e912f7a36ab002427e653cecbbe9f95636afd882e3aed34caee6c04d3bb3
MD5 4fb32c1e12232bb8aa4f250809b3d623
BLAKE2b-256 5891ca514649265ec53cafa45646decbb596f09f887eb0238f76bf6a73b76b5c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.0-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 81260f492c8b25bd397212f8c8eeac73af5867b8c832929ca1d071584eeabcc2
MD5 d116b1dbc32fd312c940bba97f3777e8
BLAKE2b-256 a28fbc08c03f40ecfccab1adaffd0112f3d9b6df456c88b8e1eece24a07f59f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.5.0-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 9284079759c8afff01c8adfdefdb0e922ff2a73370fb52dd1aca875d735f4d8d
MD5 78d388e6d01943e6566d0c5d8a7c3c32
BLAKE2b-256 09766eec61cf2e9fb6f17aa78f8e8584eda02e333aceeb3ded15cdcc1ecd0e3e

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