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

Theorem eucalg 12751
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
Dummy variable  z is distinct from all other variables.
Allowed substitution hints:    R( y)    E( x, y)

Proof of Theorem eucalg
StepHypRef Expression
1 nn0uz 10257 . . . . . . . 8  |-  NN0  =  ( ZZ>= `  0 )
2 eucalg.2 . . . . . . . 8  |-  R  =  seq  0 ( ( E  o.  1st ) ,  ( NN0  X.  { A } ) )
3 0z 10030 . . . . . . . . 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 4720 . . . . . . . . 9  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  ->  <. M ,  N >.  e.  ( NN0  X.  NN0 ) )
75, 6syl5eqel 2368 . . . . . . . 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 12747 . . . . . . . . 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 12737 . . . . . . 7  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  ->  R : NN0 --> ( NN0 
X.  NN0 ) )
12 ffvelrn 5624 . . . . . . 7  |-  ( ( R : NN0 --> ( NN0 
X.  NN0 )  /\  N  e.  NN0 )  ->  ( R `  N )  e.  ( NN0  X.  NN0 ) )
1311, 12sylancom 650 . . . . . 6  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  N
)  e.  ( NN0 
X.  NN0 ) )
14 1st2nd2 6120 . . . . . 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 5489 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 N ) )  =  (  gcd  `  <. ( 1st `  ( R `
 N ) ) ,  ( 2nd `  ( R `  N )
) >. ) )
17 df-ov 5822 . . . 4  |-  ( ( 1st `  ( R `
 N ) )  gcd  ( 2nd `  ( R `  N )
) )  =  (  gcd  `  <. ( 1st `  ( R `  N
) ) ,  ( 2nd `  ( R `
 N ) )
>. )
1816, 17syl6eqr 2334 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 N ) )  =  ( ( 1st `  ( R `  N
) )  gcd  ( 2nd `  ( R `  N ) ) ) )
195fveq2i 5488 . . . . . . . 8  |-  ( 2nd `  A )  =  ( 2nd `  <. M ,  N >. )
20 op2ndg 6094 . . . . . . . 8  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  <. M ,  N >. )  =  N )
2119, 20syl5eq 2328 . . . . . . 7  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  A
)  =  N )
2221fveq2d 5489 . . . . . 6  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  ( 2nd `  A ) )  =  ( R `  N ) )
2322fveq2d 5489 . . . . 5  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  ( R `  ( 2nd `  A ) ) )  =  ( 2nd `  ( R `  N )
) )
24 xp2nd 6111 . . . . . . . . 9  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( 2nd `  A )  e.  NN0 )
2524nn0zd 10110 . . . . . . . 8  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( 2nd `  A )  e.  ZZ )
26 uzid 10237 . . . . . . . 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 2284 . . . . . . . 8  |-  ( 2nd `  A )  =  ( 2nd `  A )
298, 2, 28eucalgcvga 12750 . . . . . . 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 2318 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  ( R `  N )
)  =  0 )
3332oveq2d 5835 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( ( 1st `  ( R `  N )
)  gcd  ( 2nd `  ( R `  N
) ) )  =  ( ( 1st `  ( R `  N )
)  gcd  0 ) )
34 xp1st 6110 . . . 4  |-  ( ( R `  N )  e.  ( NN0  X.  NN0 )  ->  ( 1st `  ( R `  N
) )  e.  NN0 )
35 nn0gcdid0 12698 . . . 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 2321 . 2  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 1st `  ( R `  N )
)  =  (  gcd  `  ( R `  N
) ) )
38 gcdf 12692 . . . . . . 7  |-  gcd  :
( ZZ  X.  ZZ )
--> NN0
39 ffn 5354 . . . . . . 7  |-  (  gcd 
: ( ZZ  X.  ZZ ) --> NN0  ->  gcd  Fn  ( ZZ  X.  ZZ ) )
4038, 39ax-mp 10 . . . . . 6  |-  gcd  Fn  ( ZZ  X.  ZZ )
41 nn0ssz 10039 . . . . . . 7  |-  NN0  C_  ZZ
42 xpss12 4791 . . . . . . 7  |-  ( ( NN0  C_  ZZ  /\  NN0  C_  ZZ )  ->  ( NN0  X.  NN0 )  C_  ( ZZ  X.  ZZ ) )
4341, 41, 42mp2an 655 . . . . . 6  |-  ( NN0 
X.  NN0 )  C_  ( ZZ  X.  ZZ )
44 fnssres 5322 . . . . . 6  |-  ( (  gcd  Fn  ( ZZ 
X.  ZZ )  /\  ( NN0  X.  NN0 )  C_  ( ZZ  X.  ZZ ) )  ->  (  gcd  |`  ( NN0  X.  NN0 ) )  Fn  ( NN0  X.  NN0 ) )
4540, 43, 44mp2an 655 . . . . 5  |-  (  gcd  |`  ( NN0  X.  NN0 ) )  Fn  ( NN0  X.  NN0 )
468eucalginv 12748 . . . . . 6  |-  ( z  e.  ( NN0  X.  NN0 )  ->  (  gcd  `  ( E `  z
) )  =  (  gcd  `  z )
)
479ffvelrni 5625 . . . . . . 7  |-  ( z  e.  ( NN0  X.  NN0 )  ->  ( E `
 z )  e.  ( NN0  X.  NN0 ) )
48 fvres 5502 . . . . . . 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 5502 . . . . . 6  |-  ( z  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  z )  =  (  gcd  `  z )
)
5146, 49, 503eqtr4d 2326 . . . . 5  |-  ( z  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( E `  z ) )  =  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  z ) )
522, 9, 45, 51alginv 12739 . . . 4  |-  ( ( A  e.  ( NN0 
X.  NN0 )  /\  N  e.  NN0 )  ->  (
(  gcd  |`  ( NN0 
X.  NN0 ) ) `  ( R `  N ) )  =  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( R `  0 ) ) )
537, 52sylancom 650 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( (  gcd  |`  ( NN0  X.  NN0 ) ) `
 ( R `  N ) )  =  ( (  gcd  |`  ( NN0  X.  NN0 ) ) `
 ( R ` 
0 ) ) )
54 fvres 5502 . . . 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 9975 . . . . 5  |-  0  e.  NN0
57 ffvelrn 5624 . . . . 5  |-  ( ( R : NN0 --> ( NN0 
X.  NN0 )  /\  0  e.  NN0 )  ->  ( R `  0 )  e.  ( NN0  X.  NN0 ) )
5811, 56, 57sylancl 645 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  0
)  e.  ( NN0 
X.  NN0 ) )
59 fvres 5502 . . . 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 2324 . 2  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 N ) )  =  (  gcd  `  ( R `  0 )
) )
621, 2, 4, 7algr0 12736 . . . . 5  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  0
)  =  A )
6362, 5syl6eq 2332 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  0
)  =  <. M ,  N >. )
6463fveq2d 5489 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 0 ) )  =  (  gcd  `  <. M ,  N >. )
)
65 df-ov 5822 . . 3  |-  ( M  gcd  N )  =  (  gcd  `  <. M ,  N >. )
6664, 65syl6eqr 2334 . 2  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 0 ) )  =  ( M  gcd  N ) )
6737, 61, 663eqtrd 2320 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 1624    e. wcel 1685    C_ wss 3153   ifcif 3566   {csn 3641   <.cop 3644    X. cxp 4686    |` cres 4690    o. ccom 4692    Fn wfn 5216   -->wf 5217   ` cfv 5221  (class class class)co 5819    e. cmpt2 5821   1stc1st 6081   2ndc2nd 6082   0cc0 8732   NN0cn0 9960   ZZcz 10019   ZZ>=cuz 10225    mod cmo 10967    seq cseq 11040    gcd cgcd 12679
This theorem was proved from axioms:  ax-1 7  ax-2 8  ax-3 9  ax-mp 10  ax-gen 1534  ax-5 1545  ax-17 1604  ax-9 1637  ax-8 1645  ax-13 1687  ax-14 1689  ax-6 1704  ax-7 1709  ax-11 1716  ax-12 1867  ax-ext 2265  ax-sep 4142  ax-nul 4150  ax-pow 4187  ax-pr 4213  ax-un 4511  ax-cnex 8788  ax-resscn 8789  ax-1cn 8790  ax-icn 8791  ax-addcl 8792  ax-addrcl 8793  ax-mulcl 8794  ax-mulrcl 8795  ax-mulcom 8796  ax-addass 8797  ax-mulass 8798  ax-distr 8799  ax-i2m1 8800  ax-1ne0 8801  ax-1rid 8802  ax-rnegex 8803  ax-rrecex 8804  ax-cnre 8805  ax-pre-lttri 8806  ax-pre-lttrn 8807  ax-pre-ltadd 8808  ax-pre-mulgt0 8809  ax-pre-sup 8810
This theorem depends on definitions:  df-bi 179  df-or 361  df-an 362  df-3or 937  df-3an 938  df-tru 1312  df-ex 1530  df-nf 1533  df-sb 1632  df-eu 2148  df-mo 2149  df-clab 2271  df-cleq 2277  df-clel 2280  df-nfc 2409  df-ne 2449  df-nel 2450  df-ral 2549  df-rex 2550  df-reu 2551  df-rmo 2552  df-rab 2553  df-v 2791  df-sbc 2993  df-csb 3083  df-dif 3156  df-un 3158  df-in 3160  df-ss 3167  df-pss 3169  df-nul 3457  df-if 3567  df-pw 3628  df-sn 3647  df-pr 3648  df-tp 3649  df-op 3650  df-uni 3829  df-iun 3908  df-br 4025  df-opab 4079  df-mpt 4080  df-tr 4115  df-eprel 4304  df-id 4308  df-po 4313  df-so 4314  df-fr 4351  df-we 4353  df-ord 4394  df-on 4395  df-lim 4396  df-suc 4397  df-om 4656  df-xp 4694  df-rel 4695  df-cnv 4696  df-co 4697  df-dm 4698  df-rn 4699  df-res 4700  df-ima 4701  df-fun 5223  df-fn 5224  df-f 5225  df-f1 5226  df-fo 5227  df-f1o 5228  df-fv 5229  df-ov 5822  df-oprab 5823  df-mpt2 5824  df-1st 6083  df-2nd 6084  df-iota 6252  df-riota 6299  df-recs 6383  df-rdg 6418  df-er 6655  df-en 6859  df-dom 6860  df-sdom 6861  df-sup 7189  df-pnf 8864  df-mnf 8865  df-xr 8866  df-ltxr 8867  df-le 8868  df-sub 9034  df-neg 9035  df-div 9419  df-nn 9742  df-2 9799  df-3 9800  df-n0 9961  df-z 10020  df-uz 10226  df-rp 10350  df-fz 10777  df-fl 10919  df-mod 10968  df-seq 11041  df-exp 11099  df-cj 11578  df-re 11579  df-im 11580  df-sqr 11714  df-abs 11715  df-dvds 12526  df-gcd 12680
  Copyright terms: Public domain W3C validator