Library Coq.ZArith.Zgcd_alt
Zgcd_alt : an alternate version of Z.gcd, based on Euclid's algorithm
Author: Pierre Letouzey
The alternate
Zgcd_alt given here used to be the main
Z.gcd
function (see file
Znumtheory), but this main
Z.gcd is now
based on a modern binary-efficient algorithm. This earlier
version, based on Euclid's algorithm of iterated modulo, is kept
here due to both its intrinsic interest and its use as reference
point when proving gcd on Int31 numbers
In Coq, we need to control the number of iteration of modulo.
For that, we use an explicit measure in nat, and we prove later
that using 2*d is enough, where d is the number of binary
digits of the first argument.
A first obvious fact : Z.gcd a b is positive.
We now prove that Z.gcd is indeed a gcd.
1) We prove a weaker & easier bound.
2) For Euclid's algorithm, the worst-case situation corresponds
to Fibonacci numbers. Let's define them:
3) We prove that fibonacci numbers are indeed worst-case:
for a given number n, if we reach a conclusion about gcd(a,b) in
exactly n+1 loops, then fibonacci (n+1)<=a /\ fibonacci(n+2)<=b
3b) We reformulate the previous result in a more positive way.
4) The proposed bound leads to a fibonacci number that is big enough.