#WHILE #MAXREG 5 #IMPORT AddSub def GCDWhile out: o0 in: i0, i1 aux: a0, a1, a2, a3 do o0 := 0; // Initialize with 0 o0 := IfLet(i0,i1)(); // Now x0 is nonzero unless either n = 0 or m = 0 a0 := i0; a1 := i1; while o0 do a2 := Sub(a0,a1)(); // Trunkated sum n - m a3 := Sub(a1,a0)(); // Trunkated sum m - n o0 := Add(a2,a3)(); // o0 = 0 iff n = m a0 := IfLet(a2,a2)(); // Try to update n a1 := IfLet(a3,a3)() // Try to update m end; o0 := IfLet(a0,a1)() // Update x0 with x1=x2 unless one of them is 0 enddef def GCDLoop out: o0 in: i0, i1 aux: a0, a1, a2, a3 do o0 := Add(i1,i0)(); // This is to estimate the number of iterations // The algorithm would not need more than n+m steps a3 := 0; // Cheching if n = 0 or m = 0 a2:= IfLet(i0,i1)(); a3:= IfLet(a2,o0)(); o0:=a3; // This would be o0 calculated previosly unless n = 0 or m = 0 a0 := i0; a1 := i1; loop o0 do a2 := Sub(a0,a1)(); // Trunkated sum n - m a3 := Sub(a1,a0)(); // Trunkated sum m - n a0 := IfLet(a2,a2)(); // Try to update n a1 := IfLet(a3,a3)() // Try to update m end; o0 := IfLet(a0,a1)() // Update x0 with a0=a1 unless one of them is 0 enddef x0:= GCDLoop(x1,x2)(x3,x4,x5,x6)