Skip to content

RationalReconstruction ​

The small fraction n/dn/d whose image in Z/m\mathbb{Z}/m is aa — the inverse of reading n/dn/d as n⋅d−1 mod mn \cdot d^{-1} \bmod m.

RationalReconstruction(a, m)the n/dn/d with n≡ad(modm)n \equiv a d \pmod m and ∣n∣,d≤(m−1)/2|n|, d \le \sqrt{(m-1)/2}, when there is one
SageMathrational_reconstruction(a, m)
RationalReconstruction(a, m, N, D)with explicit bounds ∣n∣≤N|n| \le N, 0<d≤D0 < d \le D, and over a list of residues
Typed signatures
TypePackageApplies
(integer, integer) -> rationalcompute-enginealways
(integer, integer, integer?, integer?) -> rationalnumber-theoryreplaces compute-enginealways

Domain: Number theory

Details
  • Every residue is the image of infinitely many fractions, but at most one with 2ND<m2ND < m — so under the default balanced bounds the answer, when it exists, is unique.
  • Wang's algorithm: the extended Euclidean algorithm on (m,a)(m, a), stopped at the first remainder ≤N\le N; the remainder and its cofactor are nn and dd.
  • Not a Wolfram built-in; the name follows SageMath's rational_reconstruction (Maple: iratrecon).
  • Unevaluated when no fraction within the bounds maps to aa. Threads over lists.

Examples

Basic #

2⋅6=12≡1(mod11)2 \cdot 6 = 12 \equiv 1 \pmod{11}

live ↗
InRationalReconstruction(6,11)\mathrm{RationalReconstruction}(6, 11)Out12\frac{1}{2}

a round trip through Z/1000003\mathbb{Z}/1000003

live ↗
InRationalReconstruction(PowerMod(227,1,1 000 003),1 000 003)\mathrm{RationalReconstruction}(\mathrm{PowerMod}(\frac{22}{7}, 1, 1\,000\,003), 1\,000\,003)Out227\frac{22}{7}
Scope #

explicit bounds: denominators of 1 only

live ↗
InRationalReconstruction(5,11,5,1)\mathrm{RationalReconstruction}(5, 11, 5, 1)Out55
live ↗
InRationalReconstruction([6,9,10],11)\mathrm{RationalReconstruction}(\bigl\lbrack6, 9, 10\bigr\rbrack, 11)Out[12,−2,−1]\bigl\lbrack\frac{1}{2}, -2, -1\bigr\rbrack

the harmonic number H20H_{20}, back from its image mod 261−12^{61} - 1

live ↗
InRationalReconstruction(PowerMod(55 835 13515 519 504,1,261−1),261−1)\mathrm{RationalReconstruction}(\mathrm{PowerMod}(\frac{55\,835\,135}{15\,519\,504}, 1, 2^{61}-1), 2^{61}-1)Out55 835 13515 519 504\frac{55\,835\,135}{15\,519\,504}
Applications #

one word-size prime is not enough for H20H_{20}: 2ND<m2ND < m needs mm past 1.7⋅10151.7 \cdot 10^{15}

live ↗
InRationalReconstruction(PowerMod(55 835 13515 519 504,1,1 000 000 007),1 000 000 007)\mathrm{RationalReconstruction}(\mathrm{PowerMod}(\frac{55\,835\,135}{15\,519\,504}, 1, 1\,000\,000\,007), 1\,000\,000\,007)OutRationalReconstruction(301 316 272,1 000 000 007)\mathrm{RationalReconstruction}(301\,316\,272, 1\,000\,000\,007)

multi-modular arithmetic: glue two images with ChineseRemainder, and the product modulus is large enough

live ↗
InRationalReconstruction(ChineseRemainder([PowerMod(55 835 135/15 519 504,1,1 000 000 007),PowerMod(55 835 135/15 519 504,1,1 000 000 009)],[1 000 000 007,1 000 000 009]),1 000 000 007×1 000 000 009)\mathrm{RationalReconstruction}(\mathrm{ChineseRemainder}(\bigl\lbrack\mathrm{PowerMod}(55\,835\,135/15\,519\,504, 1, 1\,000\,000\,007), \mathrm{PowerMod}(55\,835\,135/15\,519\,504, 1, 1\,000\,000\,009)\bigr\rbrack, \bigl\lbrack1\,000\,000\,007, 1\,000\,000\,009\bigr\rbrack), 1\,000\,000\,007×1\,000\,000\,009)Out55 835 13515 519 504\frac{55\,835\,135}{15\,519\,504}
Possible issues #

mod 11 the bounds are ∣n∣,d≤2|n|, d \le 2, and no such fraction is ≡3\equiv 3

live ↗
InRationalReconstruction(3,11)\mathrm{RationalReconstruction}(3, 11)OutRationalReconstruction(3,11)\mathrm{RationalReconstruction}(3, 11)
About
Examples8
Checked againstSageMath 3 agree3 error
CrosswalkSageMath

Implementation

mappedsagepackages/oracle/src/mappings.ts

See also: PowerModList, ChineseRemainder, Rationalize, ContinuedFraction