Divisibility testing by multiplication
- Cost
- One low product and one comparison per test; two words of precomputed data per divisor.
Testing by computing is wasteful when only a yes or no answer is needed. Granlund and Montgomery [GraMon1994, §9] observed that for odd the test reduces to a multiplication modulo . FLINT uses it for trial division in n_is_prime and n_factor, and exposes it as n_divisible_odd_gm.
Theorem 1. Let be odd, and . For ,
Moreover, if then .
Proof. Since is odd, is a bijection of . The multiples of in this range are for , and . So maps the multiples of onto . Being a bijection, it maps no other into this set. The second claim is .
For even , test that the lowest bits of are zero and that .
Use in FLINT#
n_is_prime stores the pairs for the 34 odd primes from 3 to 149 in a table. For the 32 tests after 3 and 5 are written as a branch-free loop, which compilers vectorise. Trial division in n_factor uses the same test, and when it succeeds divides out the prime with the exact quotient from the theorem.
int n_divisible_odd_gm(ulong n, ulong inv1, ulong inv2)int n_is_prime(ulong n)void n_factor(n_factor_t * factors, ulong n, int proved)Referenced from#
References#
- [GraMon1994] Törbjorn Granlund and Peter L. Montgomery : Division by Invariant Integers using Multiplication https://gmplib.org/~tege/divcnst-pldi94.pdf
The FLINT team, “Divisibility testing by multiplication,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/arithmetic/divisibility-test.html (generated 2026-11-02).
@misc{flint-doc-algorithms-arithmetic-divisibility-test,
author = {{The FLINT team}},
title = {{Divisibility testing by multiplication}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/algorithms/arithmetic/divisibility-test.html},
note = {Generated 2026-11-02}
}