Proofs: Batch GCD
These are the human proofs of Batch GCD, at full length.
Batch GCD returns gcd(N_i, the product of the other inputs) for each i full
This proof shows: correctness, termination, time.worst, bit_operations.worst, bit_operations.schoolbook.
We use the pseudocode BATCH-GCD of the page, on positive integers with . Let and , so . The levels are lists of positive integers, and each element of is the product of one or two consecutive elements of , its children.
Lemma 1 (product tree). The loop of lines L3 to L5 stops with , and . Each node is the product of the leaves below it, and each leaf lies below exactly one node of each level.
Proof. A level of length produces a level of length , so the loop stops, after rounds, at length . Each element of is the product of its children, and the children of distinct elements are disjoint and cover , in order. By induction on , the elements of are the products of disjoint runs of consecutive leaves that cover all leaves. At level the single run is all leaves, so .
Lemma 2 (nested reduction). Let with , and let be an integer. Then .
Proof. is a multiple of , hence of , since . Two integers that differ by a multiple of have the same remainder modulo .
Lemma 3 (remainder tree). After line L8 for level , each node of level has .
Proof. We show by downward induction on , from , that each node of level has and . At level , line L6 sets , which satisfies both. Assume it for level , and let be a child of , so by Lemma 1 and . Line L8 sets . Since , also , and by Lemma 2 (or directly), . So , which is non-negative and congruent to modulo .
Theorem 1 (correctness). BATCH-GCD returns for each .
Proof. If , then , the loop of line L7 does not run, , and line L10 returns , since is the empty product . Let , so and the leaves are at level . By Lemma 3, . Divide by : with . Then and , so . So is divisible by and . Line L10 returns , since and differ by a multiple of and so have the same common divisors with .
Zero and signs. The wrapper replaces each input by its absolute value, which changes no gcd. With one zero at position , and , while for the product of the others is , and . With two or more zeros, every product of the others is , so for all . These are the values the wrapper returns before calling the trees.
Theorem 2 (cost). Let with the number of binary digits of , and let be the cost of multiplying two -digit integers, with division with remainder in and non-decreasing. Then lines L1 to L8 cost bit operations, and with a gcd of cost the whole algorithm costs . With this is ; with schoolbook arithmetic, and the total is .
Proof. A product of numbers with and digits has at most digits. So by Lemma 1 the elements of each level have total length at most digits, and a level of the product tree costs by the superadditivity that follows from non-decreasing. In the remainder tree, a node with parent reduces a number below , of at most digits, modulo ; this costs , and the parents of one level have total length at most , each counted for at most two children, so each level costs . There are levels, so both trees cost . The gcds of line L10 are on numbers of at most digits, costing each, in total . Since , the total is .
For schoolbook arithmetic, multiplying an -digit by a -digit number costs , and dividing an -digit number by a -digit number costs . In the product tree, a node with children of lengths and costs ; expanding and as sums of leaf lengths, each pair of leaves contributes at their lowest common ancestor only, so the whole tree costs . In the remainder tree, a node with sibling and parent reduces a number of at most digits modulo , of about digits, at cost ; a node without sibling costs . The products again sum to over the tree, and the other terms are per level, in total. The Euclidean gcds of line L10 cost . So the total is . The loops all have fixed bounds, so the algorithm terminates.