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

Theorem eucialg 10923
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 Jim Kingdon, 11-Jan-2022.)

Hypotheses
Ref Expression
eucalgval.1  |-  E  =  ( x  e.  NN0 ,  y  e.  NN0  |->  if ( y  =  0 , 
<. x ,  y >. ,  <. y ,  ( x  mod  y )
>. ) )
eucialg.2  |-  R  =  seq 0 ( ( E  o.  1st ) ,  ( NN0  X.  { A } ) ,  ( NN0  X.  NN0 ) )
eucialgcvga.3  |-  N  =  ( 2nd `  A
)
eucialg.3  |-  A  = 
<. M ,  N >.
Assertion
Ref Expression
eucialg  |-  ( ( 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 eucialg
Dummy variable  z is distinct from all other variables.
StepHypRef Expression
1 nn0uz 8985 . . . . . . . 8  |-  NN0  =  ( ZZ>= `  0 )
2 eucialg.2 . . . . . . . 8  |-  R  =  seq 0 ( ( E  o.  1st ) ,  ( NN0  X.  { A } ) ,  ( NN0  X.  NN0 ) )
3 0zd 8695 . . . . . . . 8  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
0  e.  ZZ )
4 eucialg.3 . . . . . . . . 9  |-  A  = 
<. M ,  N >.
5 opelxpi 4442 . . . . . . . . 9  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  ->  <. M ,  N >.  e.  ( NN0  X.  NN0 ) )
64, 5syl5eqel 2171 . . . . . . . 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 10919 . . . . . . . . 9  |-  E :
( NN0  X.  NN0 ) --> ( NN0  X.  NN0 )
98a1i 9 . . . . . . . 8  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  ->  E : ( NN0  X.  NN0 ) --> ( NN0  X.  NN0 ) )
10 nn0ex 8612 . . . . . . . . . 10  |-  NN0  e.  _V
1110, 10xpex 4521 . . . . . . . . 9  |-  ( NN0 
X.  NN0 )  e.  _V
1211a1i 9 . . . . . . . 8  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( NN0  X.  NN0 )  e.  _V )
131, 2, 3, 6, 9, 12ialgrf 10909 . . . . . . 7  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  ->  R : NN0 --> ( NN0 
X.  NN0 ) )
14 ffvelrn 5395 . . . . . . 7  |-  ( ( R : NN0 --> ( NN0 
X.  NN0 )  /\  N  e.  NN0 )  ->  ( R `  N )  e.  ( NN0  X.  NN0 ) )
1513, 14sylancom 411 . . . . . 6  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  N
)  e.  ( NN0 
X.  NN0 ) )
16 1st2nd2 5902 . . . . . 6  |-  ( ( R `  N )  e.  ( NN0  X.  NN0 )  ->  ( R `
 N )  = 
<. ( 1st `  ( R `  N )
) ,  ( 2nd `  ( R `  N
) ) >. )
1715, 16syl 14 . . . . 5  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  N
)  =  <. ( 1st `  ( R `  N ) ) ,  ( 2nd `  ( R `  N )
) >. )
1817fveq2d 5272 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 N ) )  =  (  gcd  `  <. ( 1st `  ( R `
 N ) ) ,  ( 2nd `  ( R `  N )
) >. ) )
19 df-ov 5616 . . . 4  |-  ( ( 1st `  ( R `
 N ) )  gcd  ( 2nd `  ( R `  N )
) )  =  (  gcd  `  <. ( 1st `  ( R `  N
) ) ,  ( 2nd `  ( R `
 N ) )
>. )
2018, 19syl6eqr 2135 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 N ) )  =  ( ( 1st `  ( R `  N
) )  gcd  ( 2nd `  ( R `  N ) ) ) )
214fveq2i 5271 . . . . . . . 8  |-  ( 2nd `  A )  =  ( 2nd `  <. M ,  N >. )
22 op2ndg 5879 . . . . . . . 8  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  <. M ,  N >. )  =  N )
2321, 22syl5eq 2129 . . . . . . 7  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  A
)  =  N )
2423fveq2d 5272 . . . . . 6  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  ( 2nd `  A ) )  =  ( R `  N ) )
2524fveq2d 5272 . . . . 5  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  ( R `  ( 2nd `  A ) ) )  =  ( 2nd `  ( R `  N )
) )
26 xp2nd 5894 . . . . . . . . 9  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( 2nd `  A )  e.  NN0 )
2726nn0zd 8799 . . . . . . . 8  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( 2nd `  A )  e.  ZZ )
28 uzid 8965 . . . . . . . 8  |-  ( ( 2nd `  A )  e.  ZZ  ->  ( 2nd `  A )  e.  ( ZZ>= `  ( 2nd `  A ) ) )
2927, 28syl 14 . . . . . . 7  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( 2nd `  A )  e.  (
ZZ>= `  ( 2nd `  A
) ) )
30 eqid 2085 . . . . . . . 8  |-  ( 2nd `  A )  =  ( 2nd `  A )
317, 2, 30eucialgcvga 10922 . . . . . . 7  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( ( 2nd `  A )  e.  ( ZZ>= `  ( 2nd `  A ) )  ->  ( 2nd `  ( R `  ( 2nd `  A ) ) )  =  0 ) )
3229, 31mpd 13 . . . . . 6  |-  ( A  e.  ( NN0  X.  NN0 )  ->  ( 2nd `  ( R `  ( 2nd `  A ) ) )  =  0 )
336, 32syl 14 . . . . 5  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  ( R `  ( 2nd `  A ) ) )  =  0 )
3425, 33eqtr3d 2119 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 2nd `  ( R `  N )
)  =  0 )
3534oveq2d 5629 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( ( 1st `  ( R `  N )
)  gcd  ( 2nd `  ( R `  N
) ) )  =  ( ( 1st `  ( R `  N )
)  gcd  0 ) )
36 xp1st 5893 . . . 4  |-  ( ( R `  N )  e.  ( NN0  X.  NN0 )  ->  ( 1st `  ( R `  N
) )  e.  NN0 )
37 nn0gcdid0 10854 . . . 4  |-  ( ( 1st `  ( R `
 N ) )  e.  NN0  ->  ( ( 1st `  ( R `
 N ) )  gcd  0 )  =  ( 1st `  ( R `  N )
) )
3815, 36, 373syl 17 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( ( 1st `  ( R `  N )
)  gcd  0 )  =  ( 1st `  ( R `  N )
) )
3920, 35, 383eqtrrd 2122 . 2  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 1st `  ( R `  N )
)  =  (  gcd  `  ( R `  N
) ) )
40 gcdf 10846 . . . . . . 7  |-  gcd  :
( ZZ  X.  ZZ )
--> NN0
41 ffn 5126 . . . . . . 7  |-  (  gcd 
: ( ZZ  X.  ZZ ) --> NN0  ->  gcd  Fn  ( ZZ  X.  ZZ ) )
4240, 41ax-mp 7 . . . . . 6  |-  gcd  Fn  ( ZZ  X.  ZZ )
43 nn0ssz 8701 . . . . . . 7  |-  NN0  C_  ZZ
44 xpss12 4513 . . . . . . 7  |-  ( ( NN0  C_  ZZ  /\  NN0  C_  ZZ )  ->  ( NN0  X.  NN0 )  C_  ( ZZ  X.  ZZ ) )
4543, 43, 44mp2an 417 . . . . . 6  |-  ( NN0 
X.  NN0 )  C_  ( ZZ  X.  ZZ )
46 fnssres 5092 . . . . . 6  |-  ( (  gcd  Fn  ( ZZ 
X.  ZZ )  /\  ( NN0  X.  NN0 )  C_  ( ZZ  X.  ZZ ) )  ->  (  gcd  |`  ( NN0  X.  NN0 ) )  Fn  ( NN0  X.  NN0 ) )
4742, 45, 46mp2an 417 . . . . 5  |-  (  gcd  |`  ( NN0  X.  NN0 ) )  Fn  ( NN0  X.  NN0 )
487eucalginv 10920 . . . . . 6  |-  ( z  e.  ( NN0  X.  NN0 )  ->  (  gcd  `  ( E `  z
) )  =  (  gcd  `  z )
)
498ffvelrni 5396 . . . . . . 7  |-  ( z  e.  ( NN0  X.  NN0 )  ->  ( E `
 z )  e.  ( NN0  X.  NN0 ) )
50 fvres 5292 . . . . . . 7  |-  ( ( E `  z )  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( E `  z ) )  =  (  gcd  `  ( E `  z
) ) )
5149, 50syl 14 . . . . . 6  |-  ( z  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( E `  z ) )  =  (  gcd  `  ( E `  z
) ) )
52 fvres 5292 . . . . . 6  |-  ( z  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  z )  =  (  gcd  `  z )
)
5348, 51, 523eqtr4d 2127 . . . . 5  |-  ( z  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( E `  z ) )  =  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  z ) )
542, 8, 47, 53, 11ialginv 10911 . . . 4  |-  ( ( A  e.  ( NN0 
X.  NN0 )  /\  N  e.  NN0 )  ->  (
(  gcd  |`  ( NN0 
X.  NN0 ) ) `  ( R `  N ) )  =  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( R `  0 ) ) )
556, 54sylancom 411 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( (  gcd  |`  ( NN0  X.  NN0 ) ) `
 ( R `  N ) )  =  ( (  gcd  |`  ( NN0  X.  NN0 ) ) `
 ( R ` 
0 ) ) )
56 fvres 5292 . . . 4  |-  ( ( R `  N )  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( R `  N ) )  =  (  gcd  `  ( R `  N
) ) )
5715, 56syl 14 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( (  gcd  |`  ( NN0  X.  NN0 ) ) `
 ( R `  N ) )  =  (  gcd  `  ( R `  N )
) )
58 0nn0 8621 . . . . 5  |-  0  e.  NN0
59 ffvelrn 5395 . . . . 5  |-  ( ( R : NN0 --> ( NN0 
X.  NN0 )  /\  0  e.  NN0 )  ->  ( R `  0 )  e.  ( NN0  X.  NN0 ) )
6013, 58, 59sylancl 404 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  0
)  e.  ( NN0 
X.  NN0 ) )
61 fvres 5292 . . . 4  |-  ( ( R `  0 )  e.  ( NN0  X.  NN0 )  ->  ( (  gcd  |`  ( NN0  X. 
NN0 ) ) `  ( R `  0 ) )  =  (  gcd  `  ( R `  0
) ) )
6260, 61syl 14 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( (  gcd  |`  ( NN0  X.  NN0 ) ) `
 ( R ` 
0 ) )  =  (  gcd  `  ( R `  0 )
) )
6355, 57, 623eqtr3d 2125 . 2  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 N ) )  =  (  gcd  `  ( R `  0 )
) )
641, 2, 3, 6, 9, 12ialgr0 10908 . . . . 5  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  0
)  =  A )
6564, 4syl6eq 2133 . . . 4  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( R `  0
)  =  <. M ,  N >. )
6665fveq2d 5272 . . 3  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 0 ) )  =  (  gcd  `  <. M ,  N >. )
)
67 df-ov 5616 . . 3  |-  ( M  gcd  N )  =  (  gcd  `  <. M ,  N >. )
6866, 67syl6eqr 2135 . 2  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
(  gcd  `  ( R `
 0 ) )  =  ( M  gcd  N ) )
6939, 63, 683eqtrd 2121 1  |-  ( ( M  e.  NN0  /\  N  e.  NN0 )  -> 
( 1st `  ( R `  N )
)  =  ( M  gcd  N ) )
Colors of variables: wff set class
Syntax hints:    -> wi 4    /\ wa 102    = wceq 1287    e. wcel 1436   _Vcvv 2615    C_ wss 2988   ifcif 3379   {csn 3431   <.cop 3434    X. cxp 4409    |` cres 4413    o. ccom 4415    Fn wfn 4976   -->wf 4977   ` cfv 4981  (class class class)co 5613    |-> cmpt2 5615   1stc1st 5866   2ndc2nd 5867   0cc0 7294   NN0cn0 8606   ZZcz 8683   ZZ>=cuz 8951    mod cmo 9657    seqcseq 9779    gcd cgcd 10820
This theorem was proved from axioms:  ax-1 5  ax-2 6  ax-mp 7  ax-ia1 104  ax-ia2 105  ax-ia3 106  ax-in1 577  ax-in2 578  ax-io 663  ax-5 1379  ax-7 1380  ax-gen 1381  ax-ie1 1425  ax-ie2 1426  ax-8 1438  ax-10 1439  ax-11 1440  ax-i12 1441  ax-bndl 1442  ax-4 1443  ax-13 1447  ax-14 1448  ax-17 1462  ax-i9 1466  ax-ial 1470  ax-i5r 1471  ax-ext 2067  ax-coll 3929  ax-sep 3932  ax-nul 3940  ax-pow 3984  ax-pr 4010  ax-un 4234  ax-setind 4326  ax-iinf 4376  ax-cnex 7380  ax-resscn 7381  ax-1cn 7382  ax-1re 7383  ax-icn 7384  ax-addcl 7385  ax-addrcl 7386  ax-mulcl 7387  ax-mulrcl 7388  ax-addcom 7389  ax-mulcom 7390  ax-addass 7391  ax-mulass 7392  ax-distr 7393  ax-i2m1 7394  ax-0lt1 7395  ax-1rid 7396  ax-0id 7397  ax-rnegex 7398  ax-precex 7399  ax-cnre 7400  ax-pre-ltirr 7401  ax-pre-ltwlin 7402  ax-pre-lttrn 7403  ax-pre-apti 7404  ax-pre-ltadd 7405  ax-pre-mulgt0 7406  ax-pre-mulext 7407  ax-arch 7408  ax-caucvg 7409
This theorem depends on definitions:  df-bi 115  df-dc 779  df-3or 923  df-3an 924  df-tru 1290  df-fal 1293  df-nf 1393  df-sb 1690  df-eu 1948  df-mo 1949  df-clab 2072  df-cleq 2078  df-clel 2081  df-nfc 2214  df-ne 2252  df-nel 2347  df-ral 2360  df-rex 2361  df-reu 2362  df-rmo 2363  df-rab 2364  df-v 2617  df-sbc 2830  df-csb 2923  df-dif 2990  df-un 2992  df-in 2994  df-ss 3001  df-nul 3276  df-if 3380  df-pw 3417  df-sn 3437  df-pr 3438  df-op 3440  df-uni 3637  df-int 3672  df-iun 3715  df-br 3821  df-opab 3875  df-mpt 3876  df-tr 3912  df-id 4094  df-po 4097  df-iso 4098  df-iord 4167  df-on 4169  df-ilim 4170  df-suc 4172  df-iom 4379  df-xp 4417  df-rel 4418  df-cnv 4419  df-co 4420  df-dm 4421  df-rn 4422  df-res 4423  df-ima 4424  df-iota 4946  df-fun 4983  df-fn 4984  df-f 4985  df-f1 4986  df-fo 4987  df-f1o 4988  df-fv 4989  df-riota 5569  df-ov 5616  df-oprab 5617  df-mpt2 5618  df-1st 5868  df-2nd 5869  df-recs 6024  df-frec 6110  df-sup 6623  df-pnf 7468  df-mnf 7469  df-xr 7470  df-ltxr 7471  df-le 7472  df-sub 7599  df-neg 7600  df-reap 7993  df-ap 8000  df-div 8079  df-inn 8358  df-2 8416  df-3 8417  df-4 8418  df-n0 8607  df-z 8684  df-uz 8952  df-q 9037  df-rp 9067  df-fz 9357  df-fzo 9482  df-fl 9605  df-mod 9658  df-iseq 9780  df-iexp 9854  df-cj 10172  df-re 10173  df-im 10174  df-rsqrt 10327  df-abs 10328  df-dvds 10679  df-gcd 10821
This theorem is referenced by: (None)
  Copyright terms: Public domain W3C validator