MPE Home Metamath Proof Explorer < Previous   Next >
Nearby theorems
Mirrors  >  Home  >  MPE Home  >  Th. List  >  eucalg Unicode version

Theorem eucalg 12684
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.

Upon halting, the 1st member of the final state  ( R `  N ) is equal to the gcd of the values comprising the input state  <. M ,  N >.. (Contributed by Paul Chapman, 31-Mar-2011.) (Proof shortened by Mario Carneiro, 29-May-2014.)

Hypotheses
Ref Expression
eucalgval.1  |-  E  =  ( x  e.  NN0 ,  y  e.  NN0  |->  if ( y  =  0 , 
<. x ,  y >. ,  <. y ,  ( x  mod  y )
>. ) )
eucalg.2  |-  R  =  seq  0 ( ( E  o.  1st ) ,  ( NN0  X.  { A } ) )
eucalg.3  |-  A  = 
<. M ,  N >.
Assertion
Ref Expression
eucalg  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 1st `  ( R `  N )
)  =  ( M  gcd  N ) )
Distinct variable groups:    x, y, M    x, N, y    x, A, y    x, R
Allowed substitution hints:    R( y)    E( x, y)

Proof of Theorem eucalg
StepHypRef Expression
1 nn0uz 10194 . . . . . . . 8  |-  NN0  =  ( ZZ>= `  0 )
2 eucalg.2 . . . . . . . 8  |-  R  =  seq  0 ( ( E  o.  1st ) ,  ( NN0  X.  { A } ) )
3 0z 9967 . . . . . . . . 9  |-  0  e.  ZZ
43a1i 12 . . . . . . . 8  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
0  e.  ZZ )
5 eucalg.3 . . . . . . . . 9  |-  A  = 
<. M ,  N >.
6 opelxpi 4674 . . . . . . . . 9  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  ->  <. M ,  N >.  e.  ( NN0  X.  NN0 ) )
75, 6syl5eqel 2340 . . . . . . . 8  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  ->  A  e.  ( NN0  X. 
NN0 ) )
8 eucalgval.1 . . . . . . . . . 10  |-  E  =  ( x  e.  NN0 ,  y  e.  NN0  |->  if ( y  =  0 , 
<. x ,  y >. ,  <. y ,  ( x  mod  y )
>. ) )
98eucalgf 12680 . . . . . . . . 9  |-  E :
( NN0  X.  NN0 ) --> ( NN0  X.  NN0 )
109a1i 12 . . . . . . . 8  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  ->  E : ( NN0  X.  NN0 ) --> ( NN0  X.  NN0 ) )
111, 2, 4, 7, 10algrf 12670 . . . . . . 7  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  ->  R : NN0 --> ( NN0 
X.  NN0 ) )
12 ffvelrn 5562 . . . . . . 7  |-  ( ( R : NN0 --> ( NN0 
X.  NN0 )  /\  N  e.  NN0 )  ->  ( R `  N )  e.  ( NN0  X.  NN0 ) )
1311, 12sylancom 651 . . . . . 6  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  N
)  e.  ( NN0 
X.  NN0 ) )
14 1st2nd2 6058 . . . . . 6  |-  ( ( R `  N )  e.  ( NN0  X.  NN0 )  ->  ( R `
 N )  = 
<. ( 1st `  ( R `  N )
) ,  ( 2nd `  ( R `  N
) ) >. )
1513, 14syl 17 . . . . 5  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  N
)  =  <. ( 1st `  ( R `  N ) ) ,  ( 2nd `  ( R `  N )
) >. )
1615fveq2d 5427 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 N ) )  =  (  gcd  `  <. ( 1st `  ( R `
 N ) ) ,  ( 2nd `  ( R `  N )
) >. ) )
17 df-ov 5760 . . . 4  |-  ( ( 1st `  ( R `
 N ) )  gcd  ( 2nd `  ( R `  N )
) )  =  (  gcd  `  <. ( 1st `  ( R `  N
) ) ,  ( 2nd `  ( R `
 N ) )
>. )
1816, 17syl6eqr 2306 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 N ) )  =  ( ( 1st `  ( R `  N
) )  gcd  ( 2nd `  ( R `  N ) ) ) )
195fveq2i 5426 . . . . . . . 8  |-  ( 2nd `  A )  =  ( 2nd `  <. M ,  N >. )
20 op2ndg 6032 . . . . . . . 8  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  <. M ,  N >. )  =  N )
2119, 20syl5eq 2300 . . . . . . 7  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  A
)  =  N )
2221fveq2d 5427 . . . . . 6  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  ( 2nd `  A ) )  =  ( R `  N ) )
2322fveq2d 5427 . . . . 5  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  ( R `  ( 2nd `  A ) ) )  =  ( 2nd `  ( R `  N )
) )
24 xp2nd 6049 . . . . . . . . 9  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( 2nd `  A )  e.  NN0 )
2524nn0zd 10047 . . . . . . . 8  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( 2nd `  A )  e.  ZZ )
26 uzid 10174 . . . . . . . 8  |-  ( ( 2nd `  A )  e.  ZZ  ->  ( 2nd `  A )  e.  ( ZZ>= `  ( 2nd `  A ) ) )
2725, 26syl 17 . . . . . . 7  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( 2nd `  A )  e.  (
ZZ>= `  ( 2nd `  A
) ) )
28 eqid 2256 . . . . . . . 8  |-  ( 2nd `  A )  =  ( 2nd `  A )
298, 2, 28eucalgcvga 12683 . . . . . . 7  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( ( 2nd `  A )  e.  ( ZZ>= `  ( 2nd `  A ) )  ->  ( 2nd `  ( R `  ( 2nd `  A ) ) )  =  0 ) )
3027, 29mpd 16 . . . . . 6  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( 2nd `  ( R `  ( 2nd `  A ) ) )  =  0 )
317, 30syl 17 . . . . 5  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  ( R `  ( 2nd `  A ) ) )  =  0 )
3223, 31eqtr3d 2290 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  ( R `  N )
)  =  0 )
3332oveq2d 5773 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( ( 1st `  ( R `  N )
)  gcd  ( 2nd `  ( R `  N
) ) )  =  ( ( 1st `  ( R `  N )
)  gcd  0 ) )
34 xp1st 6048 . . . 4  |-  ( ( R `  N )  e.  ( NN0  X.  NN0 )  ->  ( 1st `  ( R `  N
) )  e.  NN0 )
35 nn0gcdid0 12631 . . . 4  |-  ( ( 1st `  ( R `
 N ) )  e.  NN0  ->  ( ( 1st `  ( R `
 N ) )  gcd  0 )  =  ( 1st `  ( R `  N )
) )
3613, 34, 353syl 20 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( ( 1st `  ( R `  N )
)  gcd  0 )  =  ( 1st `  ( R `  N )
) )
3718, 33, 363eqtrrd 2293 . 2  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 1st `  ( R `  N )
)  =  (  gcd  `  ( R `  N
) ) )
38 gcdf 12625 . . . . . . 7  |-  gcd  :
( ZZ  X.  ZZ )
--> NN0
39 ffn 5292 . . . . . . 7  |-  (  gcd 
: ( ZZ  X.  ZZ ) --> NN0  ->  gcd  Fn  ( ZZ  X.  ZZ ) )
4038, 39ax-mp 10 . . . . . 6  |-  gcd  Fn  ( ZZ  X.  ZZ )
41 nn0ssz 9976 . . . . . . 7  |-  NN0  C_  ZZ
42 xpss12 4745 . . . . . . 7  |-  ( ( NN0  C_  ZZ  /\  NN0  C_  ZZ )  ->  ( NN0  X.  NN0 )  C_  ( ZZ  X.  ZZ ) )
4341, 41, 42mp2an 656 . . . . . 6  |-  ( NN0 
X.  NN0 )  C_  ( ZZ  X.  ZZ )
44 fnssres 5260 . . . . . 6  |-  ( (  gcd  Fn  ( ZZ 
X.  ZZ )  /\  ( NN0  X.  NN0 )  C_  ( ZZ  X.  ZZ ) )  ->  (  gcd  |`  ( NN0  X.  NN0 ) )  Fn  ( NN0  X.  NN0 ) )
4540, 43, 44mp2an 656 . . . . 5  |-  (  gcd  |`  ( NN0  X.  NN0 ) )  Fn  ( NN0  X.  NN0 )
468eucalginv 12681 . . . . . 6  |-  ( z  e.  ( NN0  X.  NN0 )  ->  (  gcd  `  ( E `  z
) )  =  (  gcd  `  z )
)
479ffvelrni 5563 . . . . . . 7  |-  ( z  e.  ( NN0  X.  NN0 )  ->  ( E `
 z )  e.  ( NN0  X.  NN0 ) )
48 fvres 5440 . . . . . . 7  |-  ( ( E `  z )  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( E `  z ) )  =  (  gcd  `  ( E `  z
) ) )
4947, 48syl 17 . . . . . 6  |-  ( z  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( E `  z ) )  =  (  gcd  `  ( E `  z
) ) )
50 fvres 5440 . . . . . 6  |-  ( z  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  z )  =  (  gcd  `  z )
)
5146, 49, 503eqtr4d 2298 . . . . 5  |-  ( z  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( E `  z ) )  =  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  z ) )
522, 9, 45, 51alginv 12672 . . . 4  |-  ( ( A  e.  ( NN0 
X.  NN0 )  /\  N  e.  NN0 )  ->  (
(  gcd  |`  ( NN0 
X.  NN0 ) ) `  ( R `  N ) )  =  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( R `  0 ) ) )
537, 52sylancom 651 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( (  gcd  |`  ( NN0  X.  NN0 ) ) `
 ( R `  N ) )  =  ( (  gcd  |`  ( NN0  X.  NN0 ) ) `
 ( R ` 
0 ) ) )
54 fvres 5440 . . . 4  |-  ( ( R `  N )  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( R `  N ) )  =  (  gcd  `  ( R `  N
) ) )
5513, 54syl 17 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( (  gcd  |`  ( NN0  X.  NN0 ) ) `
 ( R `  N ) )  =  (  gcd  `  ( R `  N )
) )
56 0nn0 9912 . . . . 5  |-  0  e.  NN0
57 ffvelrn 5562 . . . . 5  |-  ( ( R : NN0 --> ( NN0 
X.  NN0 )  /\  0  e.  NN0 )  ->  ( R `  0 )  e.  ( NN0  X.  NN0 ) )
5811, 56, 57sylancl 646 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  0
)  e.  ( NN0 
X.  NN0 ) )
59 fvres 5440 . . . 4  |-  ( ( R `  0 )  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( R `  0 ) )  =  (  gcd  `  ( R `  0
) ) )
6058, 59syl 17 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( (  gcd  |`  ( NN0  X.  NN0 ) ) `
 ( R ` 
0 ) )  =  (  gcd  `  ( R `  0 )
) )
6153, 55, 603eqtr3d 2296 . 2  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 N ) )  =  (  gcd  `  ( R `  0 )
) )
621, 2, 4, 7algr0 12669 . . . . 5  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  0
)  =  A )
6362, 5syl6eq 2304 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  0
)  =  <. M ,  N >. )
6463fveq2d 5427 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 0 ) )  =  (  gcd  `  <. M ,  N >. )
)
65 df-ov 5760 . . 3  |-  ( M  gcd  N )  =  (  gcd  `  <. M ,  N >. )
6664, 65syl6eqr 2306 . 2  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 0 ) )  =  ( M  gcd  N ) )
6737, 61, 663eqtrd 2292 1  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 1st `  ( R `  N )
)  =  ( M  gcd  N ) )
Colors of variables: wff set class
Syntax hints:    -> wi 6    /\ wa 360    = wceq 1619    e. wcel 1621    C_ wss 3094   ifcif 3506   {csn 3581   <.cop 3584    X. cxp 4624    |` cres 4628    o. ccom 4630    Fn wfn 4633   -->wf 4634   ` cfv 4638  (class class class)co 5757    e. cmpt2 5759   1stc1st 6019   2ndc2nd 6020   0cc0 8670   NN0cn0 9897   ZZcz 9956   ZZ>=cuz 10162    mod cmo 10904    seq cseq 10977    gcd cgcd 12612
This theorem was proved from axioms:  ax-1 7  ax-2 8  ax-3 9  ax-mp 10  ax-5 1533  ax-6 1534  ax-7 1535  ax-gen 1536  ax-8 1623  ax-11 1624  ax-13 1625  ax-14 1626  ax-17 1628  ax-12o 1664  ax-10 1678  ax-9 1684  ax-4 1692  ax-16 1927  ax-ext 2237  ax-sep 4081  ax-nul 4089  ax-pow 4126  ax-pr 4152  ax-un 4449  ax-cnex 8726  ax-resscn 8727  ax-1cn 8728  ax-icn 8729  ax-addcl 8730  ax-addrcl 8731  ax-mulcl 8732  ax-mulrcl 8733  ax-mulcom 8734  ax-addass 8735  ax-mulass 8736  ax-distr 8737  ax-i2m1 8738  ax-1ne0 8739  ax-1rid 8740  ax-rnegex 8741  ax-rrecex 8742  ax-cnre 8743  ax-pre-lttri 8744  ax-pre-lttrn 8745  ax-pre-ltadd 8746  ax-pre-mulgt0 8747  ax-pre-sup 8748
This theorem depends on definitions:  df-bi 179  df-or 361  df-an 362  df-3or 940  df-3an 941  df-tru 1315  df-ex 1538  df-nf 1540  df-sb 1884  df-eu 2121  df-mo 2122  df-clab 2243  df-cleq 2249  df-clel 2252  df-nfc 2381  df-ne 2421  df-nel 2422  df-ral 2520  df-rex 2521  df-reu 2522  df-rab 2523  df-v 2742  df-sbc 2936  df-csb 3024  df-dif 3097  df-un 3099  df-in 3101  df-ss 3108  df-pss 3110  df-nul 3398  df-if 3507  df-pw 3568  df-sn 3587  df-pr 3588  df-tp 3589  df-op 3590  df-uni 3769  df-iun 3848  df-br 3964  df-opab 4018  df-mpt 4019  df-tr 4054  df-eprel 4242  df-id 4246  df-po 4251  df-so 4252  df-fr 4289  df-we 4291  df-ord 4332  df-on 4333  df-lim 4334  df-suc 4335  df-om 4594  df-xp 4640  df-rel 4641  df-cnv 4642  df-co 4643  df-dm 4644  df-rn 4645  df-res 4646  df-ima 4647  df-fun 4648  df-fn 4649  df-f 4650  df-f1 4651  df-fo 4652  df-f1o 4653  df-fv 4654  df-ov 5760  df-oprab 5761  df-mpt2 5762  df-1st 6021  df-2nd 6022  df-iota 6190  df-riota 6237  df-recs 6321  df-rdg 6356  df-er 6593  df-en 6797  df-dom 6798  df-sdom 6799  df-sup 7127  df-pnf 8802  df-mnf 8803  df-xr 8804  df-ltxr 8805  df-le 8806  df-sub 8972  df-neg 8973  df-div 9357  df-n 9680  df-2 9737  df-3 9738  df-n0 9898  df-z 9957  df-uz 10163  df-rp 10287  df-fz 10714  df-fl 10856  df-mod 10905  df-seq 10978  df-exp 11036  df-cj 11514  df-re 11515  df-im 11516  df-sqr 11650  df-abs 11651  df-divides 12459  df-gcd 12613
  Copyright terms: Public domain W3C validator