Maths & numbers

Rational reconstruction calculator from residue and modulus

Recover a reduced fraction from a modular residue under explicit numerator, denominator and uniqueness bounds.

Your numbers

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

A rational number a/b can be represented modulo m when b is invertible modulo m. Rational reconstruction reverses that representation, but a residue alone is insufficient: many fractions can share it. This calculator also requires bounds on numerator magnitude and positive denominator. Under the strict condition 2AB < m, at most one reduced fraction in the stated box can match. The calculator searches the supported bounded denominator range completely.

THE FORMULAFind gcd(a,b)=1, |a|≤A, 1≤b≤B, gcd(b,m)=1, a≡rb (mod m); require 2AB<m.

A worked example

Residue 17 modulo 101 reconstructs 1/6 with A = 5 and B = 10. The congruence check is 17 × 6 = 102 ≡ 1 modulo 101, and gcd(6,101) = 1. The uniqueness check gives 2 × 5 × 10 = 100 < 101. The answer is not the ordinary fraction 17/101.

Good to know

Requires the strict uniqueness bound rather than returning ambiguous alternatives. Numerator magnitude is at most one million, denominator at most 100,000 and modulus at most one trillion. Denominators must be invertible modulo the modulus, which may be composite. This bounded exhaustive search is not a general-purpose unbounded rational recovery algorithm.

A modular residue represents division through an inverse

Ordinary division of 17 by 101 does not solve this problem. The residue 17 represents the rational 1/6 because 6 has a modular inverse, and that inverse is 17 modulo 101. Multiplying the proposed fraction’s numerator equation by its denominator gives the easily checked relation a − rb divisible by m. The certificate includes this remainder and the denominator’s gcd with m.

Negative numerators are supported. Residue 84 modulo 101 is congruent to −17 and reconstructs −1/6 under the same bounds. Each positive denominator is tested with the centered residue of r times b; under the stated bound there cannot be two possible numerators for that denominator. Fractions are returned in lowest terms, so zero can only appear as 0/1.

Residue seventeen modulo one hundred one reconstructs one sixth17 × 6 = 102 = 101 + 1a = 1, b = 6 → a/b = 1/62 × 5 × 10 = 100 < 101
With |a| ≤ 5 and 1 ≤ b ≤ 10, the strict uniqueness bound is 100 < 101. Since 17 × 6 = 102 ≡ 1, the reconstructed reduced rational is 1/6.

The modular inverse calculator checks whether division by a proposed denominator is defined modulo the supplied modulus.

Why the strict bound proves uniqueness

Suppose two reduced candidate fractions a/b and c/d both match the same residue. Then ad − bc is divisible by m. The bounds imply |ad − bc| ≤ 2AB < m. The only multiple of m in that interval is zero, so ad = bc. Reduced fractions with positive denominators must then be the same fraction. This proves at most one answer, not that an answer necessarily exists.

A no-solution result says that no admissible fraction was found anywhere in the declared bounds. It does not mean the residue is invalid. Increasing a bound can admit a solution, but the uniqueness inequality must still hold. Bounds violating that inequality are rejected instead of silently choosing one fraction from several possibilities. Continued fractions can accelerate rational reconstruction in other implementations; the existing continued-fraction expansion page does not itself perform this bounded inverse problem.

A few questions, answered

Must the modulus be prime?

No. Composite moduli are accepted. The recovered denominator must nevertheless be coprime to the modulus so that its inverse exists.

Why reject bounds with 2AB equal to m?

The uniqueness argument needs a strict inequality. Equality can allow a nonzero multiple of m in the cross-product bound, so this page conservatively requires 2AB < m.

Find your calculator

Start typing to explore the library.