Euclid
Euclid
Klaus Pommerening
Fachbereich Mathematik
der Johannes-Gutenberg-Universität
Saarstraße 21
D-55099 Mainz
1 The Algorithm
Euclid’s algorithm gives the greatest common divisor (gcd) of two integers,
gcd : Z × Z −→ N
Proof. Trivial; for (iv) use the equivalence d|a, b ⇐⇒ d|a − qb, b. 3
1
Since moreover
r1 > r2 > . . . > ri ≥ 0 for all i,
we reach the terminating condition rn+1 = 0 after at most n ≤ |b| iteration
steps (i. e. divisions).
A small additional consideration even gives more. Note that each ri is an
integer linear combination of the two preceeding division remainders, hence
of |a| and |b|:
for r0 and r1 this is immediate, and in the general case it follows by induction:
Let rj = |a|xj + |b|yj for 0 ≤ j ≤ i. Then
Remarks
1. The least common multiple is efficiently calculated by the formula
ab
lcm(a, b) = .
gcd(a, b)
2
2 Analysis of Euclid’s Algorithm
The algorithm of the last section has a hidden problem: Though the quo-
tients and division remainders are safely bounded by the input parameters,
the coefficients xi and yi are uncontrolled at first sight. How can we guar-
antee that we don’t get an overflow, if we use the usual integer arithmetic
with bounded precision? Now, the following reasoning controls the growth:
Proof. (Sketch.) Show (i), (ii), and (iii) by induction. From 0 = rn+1 =
|a|xn+1 + |b|yn+1 then follows xn+1 |b and yn+1 |a. 3
Proposition 2 (Binet 1841) For a, b ∈ Z with 0 < b < Fn+1 the Eucliden
algorithm finds the greatest common divisor in at most n − 1 iteration steps.
Addendum. This is true also for b = Fn+1 , except if a ≡ Fn+2 ≡ Fn
(mod b).
This gives a quite elegant mathematical formulation, but not yet an ex-
plicit bound. However the growth of the Fibonacci numbers
√ is well-known.
1+ 5
One can express it by the golden section ϕ = 2 , that is defined by
ϕ2 − ϕ − 1 = 0.
3
Lemma 3 For a real number c ∈ R and an index k ∈ N let Fk > c · ϕk and
Fk+1 > c · ϕk+1 . Then Fn > c · ϕn for all n ≥ k.
for n ≥ k + 2. 3
Proof.
√
2 3+ 5
ϕ = ϕ+1= ,
2 √
ϕ3 = ϕ2 + ϕ = 2 + 5,
√
7+3 5
ϕ4 3
= ϕ +ϕ =2
.
2
Therefore
√
F3 2 2( 5 − 2) √
= √ = = 2 5 − 4 > 0.47,
ϕ3 2+ 5 1
√ √
F4 3·2 6(7 − 3 5) 21 − 9 5
= √ = = > 0.43769
ϕ4 7+3 5 49 − 45 2
which proves the assertion. 3
4
If we additionally consider the costs for the multiplication and division
of large numbers depending on their number of digits, we get a working time
that grows quadratically with the number of digits as shown in the following.
If a has m digits (with respect to a base B of the integers), and b has p
digits, then the expense for the first division alone is already ≤ c · (m − p) · p;
here c is a constant that is at most twice as large as the constant that
bounds the expense for “multiplying quotient × divisor back”. Considering
actual computer architectures we would take B = 232 or 264 , and count
the basic operations addition, subtraction, multiplication, division with re-
mainder, and comparision of 1-digit numbers (in base B) as primitive steps.
Fortunately the involved numbers shrink in an exponential way along the
Euclidean division chain. The division step
ri−1 = qi ri + ri+1
yet requires ≤ c ·B log(qi )B log(ri ) primitive operations, hence the entire di-
vision chain needs
n
X n
X
B
A(a, b) ≤ c · log(qi )B log(ri ) ≤ c ·B log |b| · B
log(qi )
i=1 i=1
= c · log |b| ·B log(q1 · · · qn ).
B
|a| = r0 = q1 r1 + r2 = q1 (q2 r2 + r3 ) + r2 = . . . = q1 · · · qn rn + · · · ≥ q1 · · · qn
3 Congruence Division
The extended Euclidean algorithm also provides a solution of the—not
entirely trivial—problem of efficient division in the ring Z/nZ of integers
mod n.
5
Proposition 4 Let n ∈ N, n ≥ 2, and a, b ∈ Z with gcd(b, n) = d. Then a
is divisible by b in Z/nZ, if and only if d|a. In this case there are exactly d
solutions z of zb ≡ a (mod n) with 0 ≤ z < n, and any two of them differ
by a multiple of n/d. If d = xn + yb and a = td, then z = yt is a solution.
Therefore the invertible elements of the ring Z/nZ are exactly the equiv-
alence classes of the integers coprime with n. The most important case is:
n = p prime:
6
Proposition 5 (Chinese Remainder Theorem) Let m and n coprime nat-
ural numbers ≥ 1, and a, b arbitrary integers. Then there is exactly one
integer x, 0 ≤ x < mn, such that
m, n coprime ⇐⇒ d = 1 ⇐⇒ Zm + Zn = Z.
7
the statement: For each r ∈ R there is an s ∈ a1 ∩ · · · ∩ aq−1 with r − s ∈ aq ,
or in other words,
(a1 ∩ · · · ∩ aq−1 ) + aq = R.
To prove this intermediate assertion we choose ci ∈ ai for i = 1, . . . , q − 1
and b1 , . . . , bq−1 ∈ aq with bi + ci = 1. Then
x1 = a1 mod n1 , s1 = n1 ,
ti with 0 ≤ ti ≤ ni − 1 and ai − xi−1 − ti si−1 ∈ Rni ,
xi = xi−1 + ti si−1 , si = si−1 ni .
x = x1 + t1 n1 + · · · + tq−1 n1 · · · nq−1 .
8
5. As an example we treat Sun-Tsu’s problem from the 1st Century. In
our notation its formulation is: Find x such that
x1 = 2, s1 = 3,
1 − 3t2 ∈ 5Z, t2 = 2,
x2 = 2 + 2 · 3 = 8, s2 = 15,
−6 − 15t3 ∈ 7Z, t3 = 1,
x = x3 = 8 + 1 · 15 = 23.
6. For the polynomial ring K[T ] over a field K the interpolation problem
is a special case of the Chinese remainder problem. Our algorithm in
this case is just Newton’s interpolation procedure.
Proof. The Chinese Remainder Theorem just says that the natural ring
homomorphism
9
Corollary 2 Let n = pe11 · · · perr be the prime decomposition (all ei ≥ 1).
Then
r
Y 1
ϕ(n) = n · (1 − ).
pi
i=1
10