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.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.4.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.4.1-cp313-cp313-macosx_15_0_arm64.whl (118.7 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

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

Uploaded CPython 3.13macOS 14.0+ ARM64

FindAFactor-6.4.1-cp313-cp313-macosx_13_0_x86_64.whl (133.6 kB view details)

Uploaded CPython 3.13macOS 13.0+ x86-64

FindAFactor-6.4.1-cp312-cp312-win_amd64.whl (153.6 kB view details)

Uploaded CPython 3.12Windows x86-64

FindAFactor-6.4.1-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.1-cp310-cp310-manylinux_2_35_x86_64.whl (143.7 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.35+ x86-64

FindAFactor-6.4.1-cp38-cp38-manylinux_2_31_x86_64.whl (137.5 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.31+ x86-64

File details

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

File metadata

  • Download URL: findafactor-6.4.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.4.1.tar.gz
Algorithm Hash digest
SHA256 c5b38df20eb4e0077c1c124fb73ece9c4e7686f77390fbf6a0f96acb78df3066
MD5 2607a491099d2093014d1a8bab49e295
BLAKE2b-256 08eb9efd9bc15f69d0da8a543fedecdf300d90851905cb299e98c9333c6dc0bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.4.1-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 e2b9a8b6285cfc2a6f0b2b1924ec71f084a3eac24568778ad4af4ab7819407b3
MD5 591411cb3c3a048fbcb823471d0325e9
BLAKE2b-256 301b3a71ee5c041fd00308563c2b18670c24beaab1f746d97a9ce1b6d5fe428f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.4.1-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 3f4fdc64a9e35d3cd95e1ed20e23b4c760eba279ee05a774924ec7396a291961
MD5 33ac3e340091d66a0e4fbc533df7c5df
BLAKE2b-256 313fb89f49e46a169a099d5ecfb6034e8d271eb588412abef16281aa155d8f75

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.4.1-cp313-cp313-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 8462589a992194ef6b072cea2a704a0d8b58ecf1db3250a4b1d2f37f91bda217
MD5 a437cad7de2fbc6603fc57bc71af1c81
BLAKE2b-256 a178bf2d97d1a55bfa31e1244d5c5baae5e487cedf7ff4a5f3879a5707e1fedb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.4.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e462fc263a8758cf5de0e8566b5a0e88920f04f6f24785bf6a3d53379912bc98
MD5 8ac002fc05bddd4a924d8650bb8d724f
BLAKE2b-256 c47781d13e41ff81a1ec89248b067478b9451b582a54095c581656c2c9fb2aa2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.4.1-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 41a8dd13af983a38ad36f94060e1782a8fa63573d6b2ef490646668886346249
MD5 18d1ac17cbe1d92c960e0beb02ff3ce1
BLAKE2b-256 e409696d1858721651d08032c854e4d6822e653891f43f2232c284c1f236d374

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.4.1-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 946b8fe8a79cedadb3658d31e1f21b16813b7442ee4ea89f9f56b8b067d22d60
MD5 759d123b5d45a012906581da280e4059
BLAKE2b-256 460d2cd0416fd1ead455a244cd3fe515d094f70321de154306940e1f30175589

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for FindAFactor-6.4.1-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 ad5b44dbc2e10778102b528ffc8c7713e9caa52c263c0d281fc020f1d2bfe6e0
MD5 7bf98d418923bbbb7e6f4ab072d68e85
BLAKE2b-256 b8a87aa7b8fa6f31943c763c80339dffbfa44f85f548ba74439da224c9388cfb

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