Metamath Proof Explorer


Theorem eucalg

Description: Euclid's Algorithm computes the greatest common divisor of two nonnegative integers by repeatedly replacing the larger of them with its remainder modulo the smaller until the remainder is 0. Theorem 1.15 in ApostolNT p. 20.

Upon halting, the first member of the final state ( RN ) is equal to the gcd of the values comprising the input state <. M , N >. . This is Metamath 100 proof #69 (greatest common divisor algorithm). (Contributed by Paul Chapman, 31-Mar-2011) (Proof shortened by Mario Carneiro, 29-May-2014)

Ref Expression
Hypotheses eucalgval.1 E=x0,y0ify=0xyyxmody
eucalg.2 R=seq0E1st0×A
eucalg.3 A=MN
Assertion eucalg M0N01stRN=MgcdN

Proof

Step Hyp Ref Expression
1 eucalgval.1 E=x0,y0ify=0xyyxmody
2 eucalg.2 R=seq0E1st0×A
3 eucalg.3 A=MN
4 nn0uz 0=0
5 0zd M0N00
6 opelxpi M0N0MN0×0
7 3 6 eqeltrid M0N0A0×0
8 1 eucalgf E:0×00×0
9 8 a1i M0N0E:0×00×0
10 4 2 5 7 9 algrf M0N0R:00×0
11 ffvelcdm R:00×0N0RN0×0
12 10 11 sylancom M0N0RN0×0
13 1st2nd2 RN0×0RN=1stRN2ndRN
14 12 13 syl M0N0RN=1stRN2ndRN
15 14 fveq2d M0N0gcdRN=gcd1stRN2ndRN
16 df-ov 1stRNgcd2ndRN=gcd1stRN2ndRN
17 15 16 eqtr4di M0N0gcdRN=1stRNgcd2ndRN
18 3 fveq2i 2ndA=2ndMN
19 op2ndg M0N02ndMN=N
20 18 19 eqtrid M0N02ndA=N
21 20 fveq2d M0N0R2ndA=RN
22 21 fveq2d M0N02ndR2ndA=2ndRN
23 xp2nd A0×02ndA0
24 23 nn0zd A0×02ndA
25 uzid 2ndA2ndA2ndA
26 24 25 syl A0×02ndA2ndA
27 eqid 2ndA=2ndA
28 1 2 27 eucalgcvga A0×02ndA2ndA2ndR2ndA=0
29 26 28 mpd A0×02ndR2ndA=0
30 7 29 syl M0N02ndR2ndA=0
31 22 30 eqtr3d M0N02ndRN=0
32 31 oveq2d M0N01stRNgcd2ndRN=1stRNgcd0
33 xp1st RN0×01stRN0
34 nn0gcdid0 1stRN01stRNgcd0=1stRN
35 12 33 34 3syl M0N01stRNgcd0=1stRN
36 17 32 35 3eqtrrd M0N01stRN=gcdRN
37 1 eucalginv z0×0gcdEz=gcdz
38 8 ffvelcdmi z0×0Ez0×0
39 38 fvresd z0×0gcd0×0Ez=gcdEz
40 fvres z0×0gcd0×0z=gcdz
41 37 39 40 3eqtr4d z0×0gcd0×0Ez=gcd0×0z
42 2 8 41 alginv A0×0N0gcd0×0RN=gcd0×0R0
43 7 42 sylancom M0N0gcd0×0RN=gcd0×0R0
44 12 fvresd M0N0gcd0×0RN=gcdRN
45 0nn0 00
46 ffvelcdm R:00×000R00×0
47 10 45 46 sylancl M0N0R00×0
48 47 fvresd M0N0gcd0×0R0=gcdR0
49 43 44 48 3eqtr3d M0N0gcdRN=gcdR0
50 4 2 5 7 algr0 M0N0R0=A
51 50 3 eqtrdi M0N0R0=MN
52 51 fveq2d M0N0gcdR0=gcdMN
53 df-ov MgcdN=gcdMN
54 52 53 eqtr4di M0N0gcdR0=MgcdN
55 36 49 54 3eqtrd M0N01stRN=MgcdN