{"id":272,"date":"2026-11-13T12:36:33","date_gmt":"2026-11-13T04:36:33","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-chinese-remainder-theorem-builds-a-residue\/"},"modified":"2026-11-13T21:54:09","modified_gmt":"2026-11-13T13:54:09","slug":"how-the-chinese-remainder-theorem-builds-a-residue","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-chinese-remainder-theorem-builds-a-residue\/","title":{"rendered":"How the Chinese remainder theorem builds a residue"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>Chinese remainder theorem<\/strong> says that if the moduli m1, \u2026, mk are pairwise coprime, then for any residues a1, \u2026, ak there is one solution modulo M = m1\u00b7\u2026\u00b7mk of the system x \u2261 ai (mod mi). The constructive form computes, for each i, the product Mi = M\/mi and the inverse of Mi modulo mi, then sums ai\u00b7Mi\u00b7yi. The solution is unique modulo M. Pairwise coprime is the hypothesis. One modulus that shares a factor with another makes the system dependent.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A set of independent modular constraints determines a unique residue class modulo the product, provided no two moduli share a prime factor. The construction is explicit. Mi is 0 modulo every mj except mi, so multiplying by the inverse of Mi modulo mi turns Mi into a term that is 1 modulo mi and 0 modulo the others. The sum of ai times those terms hits every residue at once.<\/p>\n\n\n<p class=\"wp-block-paragraph\">If gcd(mi, mj) does not divide ai \u2212 aj, the system has no solution. If it does divide, there may still be a solution modulo a smaller product. The pairwise-coprime construction does not cover that case. A successive-merge version that uses the extended gcd at each step does.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Under pairwise coprimality the displayed sum is a solution, and any two solutions differ by a multiple of M. Time is k inverses and O(k) multiplications of integers the size of M, plus the cost of computing M. Extra memory is the Mi and the inverses. The inverse exists because gcd(Mi, mi) = 1, which is the pairwise hypothesis applied to the product of the other moduli.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The theorem is not a statement about prime moduli only. Any pairwise coprime integers work, including composite ones that happen to share no factors. Two even moduli are not allowed in the basic statement.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Verify that gcd(mi, mj) = 1 for i \u2260 j. If not, switch to a merge algorithm or report that the basic theorem does not apply.<\/li>\n<li>Compute M as the product of the moduli, and Mi = M\/mi for each i.<\/li>\n<li>Compute yi, the inverse of Mi modulo mi, with the extended Euclidean algorithm.<\/li>\n<li>Set x = sum over i of ai\u00b7Mi\u00b7yi. Reduce x modulo M into the range the caller wants.<\/li>\n<li>Check x \u2261 ai (mod mi) for each i. A mismatch means an inverse or a product was taken modulo the wrong integer.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">Uniqueness is modulo M, not a unique integer. The construction needs inverses modulo each mi, not modulo M. A system with moduli that are not coprime can still be solvable and needs the extended-gcd merge, which also returns the obstruction when the differences of residues are not divisible by the gcds.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the moduli share a prime factor<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Invert Mi modulo mi when a prime divides both<\/td>\n<td>The inverse does not exist. The product formula cannot be evaluated, and forcing a pseudo-inverse returns a number that misses some equations.<\/td>\n<\/tr>\n<tr>\n<td>Return x without reducing modulo M and call it the only solution<\/td>\n<td>x + M is the same class. The theorem&#8217;s uniqueness is the class.<\/td>\n<\/tr>\n<tr>\n<td>Multiply the congruences instead of mixing them with Mi<\/td>\n<td>A product of residues is a different arithmetic object. The explicit solution is the weighted sum.<\/td>\n<\/tr>\n<tr>\n<td>Use the formula on a solvable system of non-coprime moduli<\/td>\n<td>The formula&#8217;s hypothesis is false. A successive merge may still find the solution, and this sum is not that algorithm.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Each inverse is the extended Euclidean algorithm. The theorem is the way RSA decrypts modulo p and modulo q and combines the results, and the way many counting problems reduce a composite modulus. The merge for non-coprime moduli is the extended-gcd version of the same idea.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The Bash script and the Python script build one residue for x\u22612 (mod 3), x\u22613 (mod 5), x\u22612 (mod 7). Both print <code>23<\/code>.<\/p>\n\n<pre><code class=\"language-bash\"># mods 3,5,7 rems 2,3,2. Inverse of Mi modulo m by extended gcd, iterative.\nmods=(3 5 7)\nrems=(2 3 2)\nM=105\nx=0\nfor ((i=0; i&lt;3; i++)); do\n  m=${mods[i]}\n  Mi=$((M \/ m))\n  a=$Mi\n  b=$m\n  s0=1; s1=0\n  while (( b != 0 )); do\n    q=$((a \/ b))\n    r=$((a % b))\n    a=$b; b=$r\n    ns=$((s0 - q * s1)); s0=$s1; s1=$ns\n  done\n  inv=$(( (s0 % m + m) % m ))\n  x=$((x + rems[i] * Mi * inv))\ndone\necho $((x % M))\n<\/code><\/pre>\n<pre><code class=\"language-python\">def egcd(a, b):\n    if b == 0:\n        return 1, 0\n    x1, y1 = egcd(b, a % b)\n    return y1, x1 - (a \/\/ b) * y1\n\nmods = [3, 5, 7]\nrems = [2, 3, 2]\nM = 1\nfor m in mods:\n    M *= m\nx = 0\nfor a, m in zip(rems, mods):\n    Mi = M \/\/ m\n    inv, _ = egcd(Mi, m)\n    x += a * Mi * (inv % m)\nx %= M\nassert x % 3 == 2 and x % 5 == 3 and x % 7 == 2\nprint(x)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The Chinese remainder theorem is how <strong>RSA-CRT<\/strong> decrypts modulo p and modulo q and combines the halves, which is the private operation in OpenSSL. It also reconstructs an integer from coprime residues. The public RSA operation does not need the split.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop the product formula when the moduli may share factors. Merge with the extended gcd and let it reject inconsistent residues. Keep the explicit sum when the moduli are known to be pairwise coprime.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for the Chinese remainder theorem returns the explicit sum ai\u00b7Mi\u00b7yi and the pairwise-coprime hypothesis. <strong>The Chinese remainder theorem builds the unique solution modulo the product of pairwise coprime moduli as a sum of residues weighted by modular inverses.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: The Chinese remainder theorem says that if the moduli m1, \u2026, mk are pairwise coprime, then for any residues a1, \u2026, ak there is one solution modulo M = m1\u00b7\u2026\u00b7mk of the system x \u2261 ai (mod mi). The constructive form computes, for each i, the product&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,42],"tags":[],"class_list":["post-272","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-number-theory"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=272"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/272\/revisions"}],"predecessor-version":[{"id":671,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/272\/revisions\/671"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}