Skip to content

ChineseRemainder ​

The smallest non-negative integer congruent to each ri modulo the corresponding mi.

✓ checked against that system · ! checked, with disagreements (hover a chip)WikipediaChinese remainder theoremMathWorldChineseRemainderTheoremWikidataQ193878Encyclopedia of MathematicsChinese_remainder_theoremBritannicatopic/Chinese-remainder-theoremWolfram LanguageChineseRemainder✓Rosetta CodeChinese remainder theorem
ChineseRemainder([r1, r2, …], [m1, m2, …])smallest non-negative xx with x≡ri(modmi)x\equiv r_i\pmod{m_i} for every ii.
ChineseRemainder([r1, r2, …], [m1, m2, …], d)the smallest such xx with x≥dx \ge d.
ChineseRemainder(ResidueClass(r1, m1), ResidueClass(r2, m2), …)the ResidueClass class mod lcm⁡(mi)\operatorname{lcm}(m_i) that reduces to each
SageMathCRT_list(r1, m1)SymPycrt(m1, r1)[0]
Typed signatures
TypePackageApplies
(collection<any>, collection<any>) -> integercompute-enginealways
(value+) -> valueresiduesreplaces compute-enginealways

Domain: Modular arithmetic

Details
  • When the moduli are pairwise coprime, the result is unique modulo m1m2⋯mnm_1m_2\cdots m_n by the Chinese remainder theorem.
  • A solution exists for non-coprime moduli only when the remainders agree on every shared factor; otherwise the system is inconsistent.
  • An inconsistent system is left unevaluated, with a ChineseRemainder::nsol message naming the two congruences that clash.
  • A third argument dd asks for the smallest solution x≥dx \ge d instead, as Wolfram's does.

Examples

Basic #
live ↗
InChineseRemainder([3,4],[4,5])\mathrm{ChineseRemainder}(\bigl\lbrack3, 4\bigr\rbrack, \bigl\lbrack4, 5\bigr\rbrack)Out1919
live ↗
InChineseRemainder([2,3,5],[3,5,7])\mathrm{ChineseRemainder}(\bigl\lbrack2, 3, 5\bigr\rbrack, \bigl\lbrack3, 5, 7\bigr\rbrack)Out6868
Scope #

five congruences, unique modulo 7⋅11⋅13⋅17⋅19=3233237 \cdot 11 \cdot 13 \cdot 17 \cdot 19 = 323323

live ↗
InChineseRemainder([1,2,3,4,5],[7,11,13,17,19])\mathrm{ChineseRemainder}(\bigl\lbrack1, 2, 3, 4, 5\bigr\rbrack, \bigl\lbrack7, 11, 13, 17, 19\bigr\rbrack)Out180 391180\,391

moduli sharing a factor, with remainders that agree on it: unique mod lcm⁡=12\operatorname{lcm} = 12

live ↗
InChineseRemainder([1,3],[4,6])\mathrm{ChineseRemainder}(\bigl\lbrack1, 3\bigr\rbrack, \bigl\lbrack4, 6\bigr\rbrack)Out99

the least solution ≥200\ge 200: 52+2⋅10552 + 2 \cdot 105

live ↗
InChineseRemainder([1,2,3],[3,5,7],200)\mathrm{ChineseRemainder}(\bigl\lbrack1, 2, 3\bigr\rbrack, \bigl\lbrack3, 5, 7\bigr\rbrack, 200)Out262262

over classes, the answer is a class — and reading ResidueNumerals([3, 5, 7]) digits back with FromDigits is the same computation

live ↗
InChineseRemainder(2‾3,3‾5,2‾7)\mathrm{ChineseRemainder}(\overline{2}_{3}, \overline{3}_{5}, \overline{2}_{7})Out23‾105\overline{23}_{105}

A third argument asks for the smallest solution x≥dx \ge d: here x≡7(mod15)x \equiv 7 \pmod{15}, so 112

live ↗
InChineseRemainder([1,2],[3,5],100)\mathrm{ChineseRemainder}(\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack3, 5\bigr\rbrack, 100)Out112112
Applications #

recover 123456 mod 1001123456 \bmod 1001 from its residues mod 7, 11 and 13. See Mod

live ↗
InChineseRemainder(123 456 mod [7,11,13],[7,11,13])\mathrm{ChineseRemainder}(123\,456\bmod\bigl\lbrack7, 11, 13\bigr\rbrack, \bigl\lbrack7, 11, 13\bigr\rbrack)Out333333
Properties #

Verifies the solution satisfies both congruences via Mod

live ↗
InChineseRemainder([3,4],[4,5]) mod 4=3∧ChineseRemainder([3,4],[4,5]) mod 5=4\mathrm{ChineseRemainder}(\bigl\lbrack3, 4\bigr\rbrack, \bigl\lbrack4, 5\bigr\rbrack)\bmod4=3∧\mathrm{ChineseRemainder}(\bigl\lbrack3, 4\bigr\rbrack, \bigl\lbrack4, 5\bigr\rbrack)\bmod5=4OutTrue\mathrm{True}

Solves x≡1(mod3)x\equiv1 \pmod3, x≡2(mod5)x\equiv2 \pmod5 for the smallest x=7x=7

live ↗
InChineseRemainder([1,2],[3,5])\mathrm{ChineseRemainder}(\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack3, 5\bigr\rbrack)Out77
Possible issues #

No solution exists when the remainders are inconsistent at gcd⁡(6,10)=2\gcd(6,10)=2, so the call stays unevaluated and says why

live ↗
InChineseRemainder([1,2],[6,10])\mathrm{ChineseRemainder}(\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack6, 10\bigr\rbrack)OutChineseRemainder([1,2],[6,10])\mathrm{ChineseRemainder}(\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack6, 10\bigr\rbrack)
About
Examples11
Checked againstWolfram Language 10 agreeSageMath 6 agree1 errorSymPy 7 agree1 error
CrosswalkWikipediaMathWorldWikidataEncyclopedia of MathematicsBritannicaWolfram LanguageSageMathSymPyRosetta Code

Implementation

mappedsagepackages/oracle/src/mappings.ts
mappedsympypackages/oracle/src/mappings.ts

crt answers None where no solution exists; the third argument, a least solution at or above a bound, is not mapped.

See also: Mod, ResidueClass, FromDigits