Algorithm Search⌘K

Proofs: Garner's algorithm

These are the human proofs of Garner's algorithm, at full length.

Garner's algorithm returns the unique solution in [0, M) full

This proof shows: correctness.

Notation. Let m1,…,mt≥1m_1, \ldots, m_t \ge 1, let P0=1P_0 = 1 and Pi=m1m2⋯miP_i = m_1 m_2 \cdots m_i, and let M=PtM = P_t. The residues are v1,…,vtv_1, \ldots, v_t.

Claim. The setup (lines L1 to L4) succeeds if and only if the moduli are pairwise coprime, and then Ci≡Pi−1−1(modmi)C_i \equiv P_{i-1}^{-1} \pmod{m_i} for 2≤i≤t2 \le i \le t. With these constants, lines L5 to L10 compute digits 0≤ui<mi0 \le u_i < m_i, and lines L11 to L13 return

x=u1+u2P1+u3P2+⋯+utPt−1,x = u_1 + u_2 P_1 + u_3 P_2 + \cdots + u_t P_{t-1},

which is the unique integer with 0≤x<M0 \le x < M and x≡vi(modmi)x \equiv v_i \pmod{m_i} for all ii.

Setup. For each i≥2i \ge 2, line L2 computes p≡Pi−1(modmi)p \equiv P_{i-1} \pmod{m_i}, as a product of the factors mj mod mim_j \bmod m_i reduced after each step. Line L3 computes g=gcd⁡(p,mi)=gcd⁡(Pi−1,mi)g = \gcd(p, m_i) = \gcd(P_{i-1}, m_i) and, when g=1g = 1, CiC_i with CiPi−1≡1(modmi)C_i P_{i-1} \equiv 1 \pmod{m_i}. A prime divides gcd⁡(Pi−1,mi)\gcd(P_{i-1}, m_i) exactly when it divides mim_i and some mjm_j with j<ij < i. So all the checks of line L4 pass if and only if every pair mj,mim_j, m_i with j<ij < i is coprime.

Partial values. Let xi=u1+u2P1+⋯+uiPi−1x_i = u_1 + u_2 P_1 + \cdots + u_i P_{i-1}. We prove by induction on ii that 0≤xi<Pi0 \le x_i < P_i and xi≡vj(modmj)x_i \equiv v_j \pmod{m_j} for all j≤ij \le i.

  • Base. Line L5 sets u1=v1 mod m1u_1 = v_1 \bmod m_1, so x1=u1∈[0,m1)x_1 = u_1 \in [0, m_1) and x1≡v1(modm1)x_1 \equiv v_1 \pmod{m_1}.
  • The Horner loop. Fix i≥2i \ge 2. Define yi−1=ui−1y_{i-1} = u_{i-1} and yj=uj+mjyj+1y_j = u_j + m_j y_{j+1} for j=i−2,…,1j = i - 2, \ldots, 1. Expanding, yj=∑l=ji−1ul mjmj+1⋯ml−1y_j = \sum_{l=j}^{i-1} u_l \, m_j m_{j+1} \cdots m_{l-1}, so y1=xi−1y_1 = x_{i-1}. Line L7 sets s≡yi−1s \equiv y_{i-1}, and each pass of line L9, for jj from i−2i - 2 down to 11, turns s≡yj+1s \equiv y_{j+1} into s≡mjyj+1+uj=yj(modmi)s \equiv m_j y_{j+1} + u_j = y_j \pmod{m_i}. After the loop, s≡xi−1(modmi)s \equiv x_{i-1} \pmod{m_i}.
  • Step. Line L10 sets ui=(vi−s)Ci mod miu_i = (v_i - s) C_i \bmod m_i, so 0≤ui<mi0 \le u_i < m_i. Then
xi=xi−1+uiPi−1≡s+(vi−s)CiPi−1≡s+(vi−s)=vi(modmi).x_i = x_{i-1} + u_i P_{i-1} \equiv s + (v_i - s) C_i P_{i-1} \equiv s + (v_i - s) = v_i \pmod{m_i}.

For j<ij < i, mj∣Pi−1m_j \mid P_{i-1}, so xi≡xi−1≡vj(modmj)x_i \equiv x_{i-1} \equiv v_j \pmod{m_j} by the induction hypothesis. For the range, 0≤xi≤(Pi−1−1)+(mi−1)Pi−1=Pi−10 \le x_i \le (P_{i-1} - 1) + (m_i - 1) P_{i-1} = P_i - 1.

Evaluation. Lines L11 and L12 compute utu_t, then utmt−1+ut−1u_t m_{t-1} + u_{t-1}, and so on down to index 11. This is the Horner form of xtx_t, so line L13 returns x=xtx = x_t. By the induction, 0≤x<M0 \le x < M and x≡vi(modmi)x \equiv v_i \pmod{m_i} for all ii.

Uniqueness. If xx and x′x' both satisfy all congruences, then every mim_i divides x−x′x - x'. The moduli are pairwise coprime, so their product MM divides x−x′x - x'. Two different integers in [0,M)[0, M) differ by less than MM, so x=x′x = x'.

Empty system. For t=0t = 0, M=1M = 1 and the only value in [0,1)[0, 1) is 00, which the implementation returns. ■\blacksquare

Back to Garner's algorithm