The scaled remainder tree returns every remainder exactly full
This proof shows: correctness, termination.
Let x≥0 and m1,…,mk≥1. In the product tree every internal node v has two children and Qv is the product of the moduli at the leaves below it; let d be its height. Write λ(Q) for the binary length of Q≥1, so 2λ(Q)−1≤Q<2λ(Q). Let G≥1 with 2G≥d+1 and pv=λ(Qv)+G. For a real z write {z}=z−⌊z⌋∈[0,1).
Theorem. SCALED-REMAINDERS terminates and returns xmodmi at the leaf of mi, for every i.
Termination. The tree has 2k−1 nodes, line L3 runs once, line L5 once per node other than the root, and line L6 once per leaf; each is a finite computation on integers.
1. Precisions decrease. If c is a child of v with sibling s, then Qv=QcQs≥Qc, so λ(Qc)≤λ(Qv) and pc≤pv; the shift in line L5 is by a nonnegative amount.
2. The error invariant. Let yv={x/Qv}. For every node v there is a real δv with
yv≡2pvYv+δv(mod1),0≤Qvδv<(depth(v)+1)2−G. Proof. By induction on the depth. At the root, x/P≡(xmodP)/P(mod1), and line L3 sets Y=⌊(xmodP)2p/P⌋, so δroot=(xmodP)/P−Y/2p∈[0,2−p), and Pδroot<P2−λ(P)−G<2−G.
Let c be a child of v with sibling s. Since x/Qc=Qs⋅x/Qv and Qs is an integer, yc≡Qsyv(mod1), and by the hypothesis
yc≡2pvQsYv+Qsδv(mod1). Let Z=(QsYv)mod2pv; then QsYv/2pv≡Z/2pv(mod1). Line L5 sets Yc=⌊Z/2pv−pc⌋, so Z/2pv=Yc/2pc+τ with 0≤τ<2−pc. Hence yc≡Yc/2pc+δc with δc=τ+Qsδv≥0, and
Qcδc=Qcτ+Qvδv<Qc2−λ(Qc)−G+(depth(v)+1)2−G<(depth(v)+2)2−G, using QcQs=Qv and Qc<2λ(Qc). Since depth(c)=depth(v)+1, the invariant holds at c. □
3. The leaves. Let a leaf have modulus m, value Y and precision p, at depth at most d, and let r=xmodm. Then y={x/m}=r/m, and by part 2, r/m=Y/2p+δ+j for an integer j, with 0≤mδ<(d+1)2−G≤1. Multiplying by m,
2pmY=r−mj−mδ,0≤mδ<1. Since r−mj is an integer, ⌈mY/2p⌉=r−mj, and reducing modulo m gives r, because 0≤r<m. So line L6 returns xmodm at every leaf. ■