ILE Home Intuitionistic Logic Explorer < Previous   Next >
Nearby theorems
Mirrors  >  Home  >  ILE Home  >  Th. List  >  eucalg Unicode version

Theorem eucalg 12002
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 1st member of the final state  ( R `  N ) 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.)

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
Dummy variable  z is distinct from all other variables.
StepHypRef Expression
1 nn0uz 9510 . . . . . . . 8  |-  NN0  =  ( ZZ>= `  0 )
2 eucalg.2 . . . . . . . 8  |-  R  =  seq 0 ( ( E  o.  1st ) ,  ( NN0  X.  { A } ) )
3 0zd 9213 . . . . . . . 8  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
0  e.  ZZ )
4 eucalg.3 . . . . . . . . 9  |-  A  = 
<. M ,  N >.
5 opelxpi 4641 . . . . . . . . 9  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  ->  <. M ,  N >.  e.  ( NN0  X.  NN0 ) )
64, 5eqeltrid 2257 . . . . . . . 8  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  ->  A  e.  ( NN0  X. 
NN0 ) )
7 eucalgval.1 . . . . . . . . . 10  |-  E  =  ( x  e.  NN0 ,  y  e.  NN0  |->  if ( y  =  0 , 
<. x ,  y >. ,  <. y ,  ( x  mod  y )
>. ) )
87eucalgf 11998 . . . . . . . . 9  |-  E :
( NN0  X.  NN0 ) --> ( NN0  X.  NN0 )
98a1i 9 . . . . . . . 8  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  ->  E : ( NN0  X.  NN0 ) --> ( NN0  X.  NN0 ) )
101, 2, 3, 6, 9algrf 11988 . . . . . . 7  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  ->  R : NN0 --> ( NN0 
X.  NN0 ) )
11 ffvelrn 5627 . . . . . . 7  |-  ( ( R : NN0 --> ( NN0 
X.  NN0 )  /\  N  e.  NN0 )  ->  ( R `  N )  e.  ( NN0  X.  NN0 ) )
1210, 11sylancom 418 . . . . . 6  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  N
)  e.  ( NN0 
X.  NN0 ) )
13 1st2nd2 6152 . . . . . 6  |-  ( ( R `  N )  e.  ( NN0  X.  NN0 )  ->  ( R `
 N )  = 
<. ( 1st `  ( R `  N )
) ,  ( 2nd `  ( R `  N
) ) >. )
1412, 13syl 14 . . . . 5  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  N
)  =  <. ( 1st `  ( R `  N ) ) ,  ( 2nd `  ( R `  N )
) >. )
1514fveq2d 5498 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 N ) )  =  (  gcd  `  <. ( 1st `  ( R `
 N ) ) ,  ( 2nd `  ( R `  N )
) >. ) )
16 df-ov 5854 . . . 4  |-  ( ( 1st `  ( R `
 N ) )  gcd  ( 2nd `  ( R `  N )
) )  =  (  gcd  `  <. ( 1st `  ( R `  N
) ) ,  ( 2nd `  ( R `
 N ) )
>. )
1715, 16eqtr4di 2221 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 N ) )  =  ( ( 1st `  ( R `  N
) )  gcd  ( 2nd `  ( R `  N ) ) ) )
184fveq2i 5497 . . . . . . . 8  |-  ( 2nd `  A )  =  ( 2nd `  <. M ,  N >. )
19 op2ndg 6128 . . . . . . . 8  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  <. M ,  N >. )  =  N )
2018, 19eqtrid 2215 . . . . . . 7  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  A
)  =  N )
2120fveq2d 5498 . . . . . 6  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  ( 2nd `  A ) )  =  ( R `  N ) )
2221fveq2d 5498 . . . . 5  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  ( R `  ( 2nd `  A ) ) )  =  ( 2nd `  ( R `  N )
) )
23 xp2nd 6143 . . . . . . . . 9  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( 2nd `  A )  e.  NN0 )
2423nn0zd 9321 . . . . . . . 8  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( 2nd `  A )  e.  ZZ )
25 uzid 9490 . . . . . . . 8  |-  ( ( 2nd `  A )  e.  ZZ  ->  ( 2nd `  A )  e.  ( ZZ>= `  ( 2nd `  A ) ) )
2624, 25syl 14 . . . . . . 7  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( 2nd `  A )  e.  (
ZZ>= `  ( 2nd `  A
) ) )
27 eqid 2170 . . . . . . . 8  |-  ( 2nd `  A )  =  ( 2nd `  A )
287, 2, 27eucalgcvga 12001 . . . . . . 7  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( ( 2nd `  A )  e.  ( ZZ>= `  ( 2nd `  A ) )  ->  ( 2nd `  ( R `  ( 2nd `  A ) ) )  =  0 ) )
2926, 28mpd 13 . . . . . 6  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( 2nd `  ( R `  ( 2nd `  A ) ) )  =  0 )
306, 29syl 14 . . . . 5  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  ( R `  ( 2nd `  A ) ) )  =  0 )
3122, 30eqtr3d 2205 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  ( R `  N )
)  =  0 )
3231oveq2d 5867 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( ( 1st `  ( R `  N )
)  gcd  ( 2nd `  ( R `  N
) ) )  =  ( ( 1st `  ( R `  N )
)  gcd  0 ) )
33 xp1st 6142 . . . 4  |-  ( ( R `  N )  e.  ( NN0  X.  NN0 )  ->  ( 1st `  ( R `  N
) )  e.  NN0 )
34 nn0gcdid0 11925 . . . 4  |-  ( ( 1st `  ( R `
 N ) )  e.  NN0  ->  ( ( 1st `  ( R `
 N ) )  gcd  0 )  =  ( 1st `  ( R `  N )
) )
3512, 33, 343syl 17 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( ( 1st `  ( R `  N )
)  gcd  0 )  =  ( 1st `  ( R `  N )
) )
3617, 32, 353eqtrrd 2208 . 2  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 1st `  ( R `  N )
)  =  (  gcd  `  ( R `  N
) ) )
377eucalginv 11999 . . . . . 6  |-  ( z  e.  ( NN0  X.  NN0 )  ->  (  gcd  `  ( E `  z
) )  =  (  gcd  `  z )
)
388ffvelrni 5628 . . . . . . 7  |-  ( z  e.  ( NN0  X.  NN0 )  ->  ( E `
 z )  e.  ( NN0  X.  NN0 ) )
39 fvres 5518 . . . . . . 7  |-  ( ( E `  z )  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( E `  z ) )  =  (  gcd  `  ( E `  z
) ) )
4038, 39syl 14 . . . . . 6  |-  ( z  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( E `  z ) )  =  (  gcd  `  ( E `  z
) ) )
41 fvres 5518 . . . . . 6  |-  ( z  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  z )  =  (  gcd  `  z )
)
4237, 40, 413eqtr4d 2213 . . . . 5  |-  ( z  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( E `  z ) )  =  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  z ) )
432, 8, 42alginv 11990 . . . 4  |-  ( ( A  e.  ( NN0 
X.  NN0 )  /\  N  e.  NN0 )  ->  (
(  gcd  |`  ( NN0 
X.  NN0 ) ) `  ( R `  N ) )  =  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( R `  0 ) ) )
446, 43sylancom 418 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( (  gcd  |`  ( NN0  X.  NN0 ) ) `
 ( R `  N ) )  =  ( (  gcd  |`  ( NN0  X.  NN0 ) ) `
 ( R ` 
0 ) ) )
45 fvres 5518 . . . 4  |-  ( ( R `  N )  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( R `  N ) )  =  (  gcd  `  ( R `  N
) ) )
4612, 45syl 14 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( (  gcd  |`  ( NN0  X.  NN0 ) ) `
 ( R `  N ) )  =  (  gcd  `  ( R `  N )
) )
47 0nn0 9139 . . . . 5  |-  0  e.  NN0
48 ffvelrn 5627 . . . . 5  |-  ( ( R : NN0 --> ( NN0 
X.  NN0 )  /\  0  e.  NN0 )  ->  ( R `  0 )  e.  ( NN0  X.  NN0 ) )
4910, 47, 48sylancl 411 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  0
)  e.  ( NN0 
X.  NN0 ) )
50 fvres 5518 . . . 4  |-  ( ( R `  0 )  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( R `  0 ) )  =  (  gcd  `  ( R `  0
) ) )
5149, 50syl 14 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( (  gcd  |`  ( NN0  X.  NN0 ) ) `
 ( R ` 
0 ) )  =  (  gcd  `  ( R `  0 )
) )
5244, 46, 513eqtr3d 2211 . 2  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 N ) )  =  (  gcd  `  ( R `  0 )
) )
531, 2, 3, 6, 9ialgr0 11987 . . . . 5  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  0
)  =  A )
5453, 4eqtrdi 2219 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  0
)  =  <. M ,  N >. )
5554fveq2d 5498 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 0 ) )  =  (  gcd  `  <. M ,  N >. )
)
56 df-ov 5854 . . 3  |-  ( M  gcd  N )  =  (  gcd  `  <. M ,  N >. )
5755, 56eqtr4di 2221 . 2  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 0 ) )  =  ( M  gcd  N ) )
5836, 52, 573eqtrd 2207 1  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 1st `  ( R `  N )
)  =  ( M  gcd  N ) )
Colors of variables: wff set class
Syntax hints:    -> wi 4    /\ wa 103    = wceq 1348    e. wcel 2141   ifcif 3525   {csn 3581   <.cop 3584    X. cxp 4607    |` cres 4611    o. ccom 4613   -->wf 5192   ` cfv 5196  (class class class)co 5851    e. cmpo 5853   1stc1st 6115   2ndc2nd 6116   0cc0 7763   NN0cn0 9124   ZZcz 9201   ZZ>=cuz 9476    mod cmo 10267    seqcseq 10390    gcd cgcd 11886
This theorem was proved from axioms:  ax-mp 5  ax-1 6  ax-2 7  ax-ia1 105  ax-ia2 106  ax-ia3 107  ax-in1 609  ax-in2 610  ax-io 704  ax-5 1440  ax-7 1441  ax-gen 1442  ax-ie1 1486  ax-ie2 1487  ax-8 1497  ax-10 1498  ax-11 1499  ax-i12 1500  ax-bndl 1502  ax-4 1503  ax-17 1519  ax-i9 1523  ax-ial 1527  ax-i5r 1528  ax-13 2143  ax-14 2144  ax-ext 2152  ax-coll 4102  ax-sep 4105  ax-nul 4113  ax-pow 4158  ax-pr 4192  ax-un 4416  ax-setind 4519  ax-iinf 4570  ax-cnex 7854  ax-resscn 7855  ax-1cn 7856  ax-1re 7857  ax-icn 7858  ax-addcl 7859  ax-addrcl 7860  ax-mulcl 7861  ax-mulrcl 7862  ax-addcom 7863  ax-mulcom 7864  ax-addass 7865  ax-mulass 7866  ax-distr 7867  ax-i2m1 7868  ax-0lt1 7869  ax-1rid 7870  ax-0id 7871  ax-rnegex 7872  ax-precex 7873  ax-cnre 7874  ax-pre-ltirr 7875  ax-pre-ltwlin 7876  ax-pre-lttrn 7877  ax-pre-apti 7878  ax-pre-ltadd 7879  ax-pre-mulgt0 7880  ax-pre-mulext 7881  ax-arch 7882  ax-caucvg 7883
This theorem depends on definitions:  df-bi 116  df-stab 826  df-dc 830  df-3or 974  df-3an 975  df-tru 1351  df-fal 1354  df-nf 1454  df-sb 1756  df-eu 2022  df-mo 2023  df-clab 2157  df-cleq 2163  df-clel 2166  df-nfc 2301  df-ne 2341  df-nel 2436  df-ral 2453  df-rex 2454  df-reu 2455  df-rmo 2456  df-rab 2457  df-v 2732  df-sbc 2956  df-csb 3050  df-dif 3123  df-un 3125  df-in 3127  df-ss 3134  df-nul 3415  df-if 3526  df-pw 3566  df-sn 3587  df-pr 3588  df-op 3590  df-uni 3795  df-int 3830  df-iun 3873  df-br 3988  df-opab 4049  df-mpt 4050  df-tr 4086  df-id 4276  df-po 4279  df-iso 4280  df-iord 4349  df-on 4351  df-ilim 4352  df-suc 4354  df-iom 4573  df-xp 4615  df-rel 4616  df-cnv 4617  df-co 4618  df-dm 4619  df-rn 4620  df-res 4621  df-ima 4622  df-iota 5158  df-fun 5198  df-fn 5199  df-f 5200  df-f1 5201  df-fo 5202  df-f1o 5203  df-fv 5204  df-riota 5807  df-ov 5854  df-oprab 5855  df-mpo 5856  df-1st 6117  df-2nd 6118  df-recs 6282  df-frec 6368  df-sup 6958  df-pnf 7945  df-mnf 7946  df-xr 7947  df-ltxr 7948  df-le 7949  df-sub 8081  df-neg 8082  df-reap 8483  df-ap 8490  df-div 8579  df-inn 8868  df-2 8926  df-3 8927  df-4 8928  df-n0 9125  df-z 9202  df-uz 9477  df-q 9568  df-rp 9600  df-fz 9955  df-fzo 10088  df-fl 10215  df-mod 10268  df-seqfrec 10391  df-exp 10465  df-cj 10795  df-re 10796  df-im 10797  df-rsqrt 10951  df-abs 10952  df-dvds 11739  df-gcd 11887
This theorem is referenced by: (None)
  Copyright terms: Public domain W3C validator