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

Theorem eucialg 10819
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 (𝑅𝑁) is equal to the gcd of the values comprising the input state 𝑀, 𝑁. This is Metamath 100 proof #69 (greatest common divisor algorithm). (Contributed by Jim Kingdon, 11-Jan-2022.)

Hypotheses
Ref Expression
eucalgval.1 𝐸 = (𝑥 ∈ ℕ0, 𝑦 ∈ ℕ0 ↦ if(𝑦 = 0, ⟨𝑥, 𝑦⟩, ⟨𝑦, (𝑥 mod 𝑦)⟩))
eucialg.2 𝑅 = seq0((𝐸 ∘ 1st ), (ℕ0 × {𝐴}), (ℕ0 × ℕ0))
eucialgcvga.3 𝑁 = (2nd𝐴)
eucialg.3 𝐴 = ⟨𝑀, 𝑁
Assertion
Ref Expression
eucialg ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (1st ‘(𝑅𝑁)) = (𝑀 gcd 𝑁))
Distinct variable groups:   𝑥,𝑦,𝑀   𝑥,𝑁,𝑦   𝑥,𝐴,𝑦   𝑥,𝑅
Allowed substitution hints:   𝑅(𝑦)   𝐸(𝑥,𝑦)

Proof of Theorem eucialg
Dummy variable 𝑧 is distinct from all other variables.
StepHypRef Expression
1 nn0uz 8946 . . . . . . . 8 0 = (ℤ‘0)
2 eucialg.2 . . . . . . . 8 𝑅 = seq0((𝐸 ∘ 1st ), (ℕ0 × {𝐴}), (ℕ0 × ℕ0))
3 0zd 8656 . . . . . . . 8 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → 0 ∈ ℤ)
4 eucialg.3 . . . . . . . . 9 𝐴 = ⟨𝑀, 𝑁
5 opelxpi 4430 . . . . . . . . 9 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → ⟨𝑀, 𝑁⟩ ∈ (ℕ0 × ℕ0))
64, 5syl5eqel 2169 . . . . . . . 8 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → 𝐴 ∈ (ℕ0 × ℕ0))
7 eucalgval.1 . . . . . . . . . 10 𝐸 = (𝑥 ∈ ℕ0, 𝑦 ∈ ℕ0 ↦ if(𝑦 = 0, ⟨𝑥, 𝑦⟩, ⟨𝑦, (𝑥 mod 𝑦)⟩))
87eucalgf 10815 . . . . . . . . 9 𝐸:(ℕ0 × ℕ0)⟶(ℕ0 × ℕ0)
98a1i 9 . . . . . . . 8 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → 𝐸:(ℕ0 × ℕ0)⟶(ℕ0 × ℕ0))
10 nn0ex 8569 . . . . . . . . . 10 0 ∈ V
1110, 10xpex 4509 . . . . . . . . 9 (ℕ0 × ℕ0) ∈ V
1211a1i 9 . . . . . . . 8 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (ℕ0 × ℕ0) ∈ V)
131, 2, 3, 6, 9, 12ialgrf 10805 . . . . . . 7 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → 𝑅:ℕ0⟶(ℕ0 × ℕ0))
14 ffvelrn 5375 . . . . . . 7 ((𝑅:ℕ0⟶(ℕ0 × ℕ0) ∧ 𝑁 ∈ ℕ0) → (𝑅𝑁) ∈ (ℕ0 × ℕ0))
1513, 14sylancom 411 . . . . . 6 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (𝑅𝑁) ∈ (ℕ0 × ℕ0))
16 1st2nd2 5878 . . . . . 6 ((𝑅𝑁) ∈ (ℕ0 × ℕ0) → (𝑅𝑁) = ⟨(1st ‘(𝑅𝑁)), (2nd ‘(𝑅𝑁))⟩)
1715, 16syl 14 . . . . 5 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (𝑅𝑁) = ⟨(1st ‘(𝑅𝑁)), (2nd ‘(𝑅𝑁))⟩)
1817fveq2d 5255 . . . 4 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → ( gcd ‘(𝑅𝑁)) = ( gcd ‘⟨(1st ‘(𝑅𝑁)), (2nd ‘(𝑅𝑁))⟩))
19 df-ov 5592 . . . 4 ((1st ‘(𝑅𝑁)) gcd (2nd ‘(𝑅𝑁))) = ( gcd ‘⟨(1st ‘(𝑅𝑁)), (2nd ‘(𝑅𝑁))⟩)
2018, 19syl6eqr 2133 . . 3 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → ( gcd ‘(𝑅𝑁)) = ((1st ‘(𝑅𝑁)) gcd (2nd ‘(𝑅𝑁))))
214fveq2i 5254 . . . . . . . 8 (2nd𝐴) = (2nd ‘⟨𝑀, 𝑁⟩)
22 op2ndg 5855 . . . . . . . 8 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (2nd ‘⟨𝑀, 𝑁⟩) = 𝑁)
2321, 22syl5eq 2127 . . . . . . 7 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (2nd𝐴) = 𝑁)
2423fveq2d 5255 . . . . . 6 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (𝑅‘(2nd𝐴)) = (𝑅𝑁))
2524fveq2d 5255 . . . . 5 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (2nd ‘(𝑅‘(2nd𝐴))) = (2nd ‘(𝑅𝑁)))
26 xp2nd 5870 . . . . . . . . 9 (𝐴 ∈ (ℕ0 × ℕ0) → (2nd𝐴) ∈ ℕ0)
2726nn0zd 8760 . . . . . . . 8 (𝐴 ∈ (ℕ0 × ℕ0) → (2nd𝐴) ∈ ℤ)
28 uzid 8926 . . . . . . . 8 ((2nd𝐴) ∈ ℤ → (2nd𝐴) ∈ (ℤ‘(2nd𝐴)))
2927, 28syl 14 . . . . . . 7 (𝐴 ∈ (ℕ0 × ℕ0) → (2nd𝐴) ∈ (ℤ‘(2nd𝐴)))
30 eqid 2083 . . . . . . . 8 (2nd𝐴) = (2nd𝐴)
317, 2, 30eucialgcvga 10818 . . . . . . 7 (𝐴 ∈ (ℕ0 × ℕ0) → ((2nd𝐴) ∈ (ℤ‘(2nd𝐴)) → (2nd ‘(𝑅‘(2nd𝐴))) = 0))
3229, 31mpd 13 . . . . . 6 (𝐴 ∈ (ℕ0 × ℕ0) → (2nd ‘(𝑅‘(2nd𝐴))) = 0)
336, 32syl 14 . . . . 5 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (2nd ‘(𝑅‘(2nd𝐴))) = 0)
3425, 33eqtr3d 2117 . . . 4 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (2nd ‘(𝑅𝑁)) = 0)
3534oveq2d 5605 . . 3 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → ((1st ‘(𝑅𝑁)) gcd (2nd ‘(𝑅𝑁))) = ((1st ‘(𝑅𝑁)) gcd 0))
36 xp1st 5869 . . . 4 ((𝑅𝑁) ∈ (ℕ0 × ℕ0) → (1st ‘(𝑅𝑁)) ∈ ℕ0)
37 nn0gcdid0 10750 . . . 4 ((1st ‘(𝑅𝑁)) ∈ ℕ0 → ((1st ‘(𝑅𝑁)) gcd 0) = (1st ‘(𝑅𝑁)))
3815, 36, 373syl 17 . . 3 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → ((1st ‘(𝑅𝑁)) gcd 0) = (1st ‘(𝑅𝑁)))
3920, 35, 383eqtrrd 2120 . 2 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (1st ‘(𝑅𝑁)) = ( gcd ‘(𝑅𝑁)))
40 gcdf 10742 . . . . . . 7 gcd :(ℤ × ℤ)⟶ℕ0
41 ffn 5112 . . . . . . 7 ( gcd :(ℤ × ℤ)⟶ℕ0 → gcd Fn (ℤ × ℤ))
4240, 41ax-mp 7 . . . . . 6 gcd Fn (ℤ × ℤ)
43 nn0ssz 8662 . . . . . . 7 0 ⊆ ℤ
44 xpss12 4501 . . . . . . 7 ((ℕ0 ⊆ ℤ ∧ ℕ0 ⊆ ℤ) → (ℕ0 × ℕ0) ⊆ (ℤ × ℤ))
4543, 43, 44mp2an 417 . . . . . 6 (ℕ0 × ℕ0) ⊆ (ℤ × ℤ)
46 fnssres 5078 . . . . . 6 (( gcd Fn (ℤ × ℤ) ∧ (ℕ0 × ℕ0) ⊆ (ℤ × ℤ)) → ( gcd ↾ (ℕ0 × ℕ0)) Fn (ℕ0 × ℕ0))
4742, 45, 46mp2an 417 . . . . 5 ( gcd ↾ (ℕ0 × ℕ0)) Fn (ℕ0 × ℕ0)
487eucalginv 10816 . . . . . 6 (𝑧 ∈ (ℕ0 × ℕ0) → ( gcd ‘(𝐸𝑧)) = ( gcd ‘𝑧))
498ffvelrni 5376 . . . . . . 7 (𝑧 ∈ (ℕ0 × ℕ0) → (𝐸𝑧) ∈ (ℕ0 × ℕ0))
50 fvres 5272 . . . . . . 7 ((𝐸𝑧) ∈ (ℕ0 × ℕ0) → (( gcd ↾ (ℕ0 × ℕ0))‘(𝐸𝑧)) = ( gcd ‘(𝐸𝑧)))
5149, 50syl 14 . . . . . 6 (𝑧 ∈ (ℕ0 × ℕ0) → (( gcd ↾ (ℕ0 × ℕ0))‘(𝐸𝑧)) = ( gcd ‘(𝐸𝑧)))
52 fvres 5272 . . . . . 6 (𝑧 ∈ (ℕ0 × ℕ0) → (( gcd ↾ (ℕ0 × ℕ0))‘𝑧) = ( gcd ‘𝑧))
5348, 51, 523eqtr4d 2125 . . . . 5 (𝑧 ∈ (ℕ0 × ℕ0) → (( gcd ↾ (ℕ0 × ℕ0))‘(𝐸𝑧)) = (( gcd ↾ (ℕ0 × ℕ0))‘𝑧))
542, 8, 47, 53, 11ialginv 10807 . . . 4 ((𝐴 ∈ (ℕ0 × ℕ0) ∧ 𝑁 ∈ ℕ0) → (( gcd ↾ (ℕ0 × ℕ0))‘(𝑅𝑁)) = (( gcd ↾ (ℕ0 × ℕ0))‘(𝑅‘0)))
556, 54sylancom 411 . . 3 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (( gcd ↾ (ℕ0 × ℕ0))‘(𝑅𝑁)) = (( gcd ↾ (ℕ0 × ℕ0))‘(𝑅‘0)))
56 fvres 5272 . . . 4 ((𝑅𝑁) ∈ (ℕ0 × ℕ0) → (( gcd ↾ (ℕ0 × ℕ0))‘(𝑅𝑁)) = ( gcd ‘(𝑅𝑁)))
5715, 56syl 14 . . 3 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (( gcd ↾ (ℕ0 × ℕ0))‘(𝑅𝑁)) = ( gcd ‘(𝑅𝑁)))
58 0nn0 8578 . . . . 5 0 ∈ ℕ0
59 ffvelrn 5375 . . . . 5 ((𝑅:ℕ0⟶(ℕ0 × ℕ0) ∧ 0 ∈ ℕ0) → (𝑅‘0) ∈ (ℕ0 × ℕ0))
6013, 58, 59sylancl 404 . . . 4 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (𝑅‘0) ∈ (ℕ0 × ℕ0))
61 fvres 5272 . . . 4 ((𝑅‘0) ∈ (ℕ0 × ℕ0) → (( gcd ↾ (ℕ0 × ℕ0))‘(𝑅‘0)) = ( gcd ‘(𝑅‘0)))
6260, 61syl 14 . . 3 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (( gcd ↾ (ℕ0 × ℕ0))‘(𝑅‘0)) = ( gcd ‘(𝑅‘0)))
6355, 57, 623eqtr3d 2123 . 2 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → ( gcd ‘(𝑅𝑁)) = ( gcd ‘(𝑅‘0)))
641, 2, 3, 6, 9, 12ialgr0 10804 . . . . 5 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (𝑅‘0) = 𝐴)
6564, 4syl6eq 2131 . . . 4 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (𝑅‘0) = ⟨𝑀, 𝑁⟩)
6665fveq2d 5255 . . 3 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → ( gcd ‘(𝑅‘0)) = ( gcd ‘⟨𝑀, 𝑁⟩))
67 df-ov 5592 . . 3 (𝑀 gcd 𝑁) = ( gcd ‘⟨𝑀, 𝑁⟩)
6866, 67syl6eqr 2133 . 2 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → ( gcd ‘(𝑅‘0)) = (𝑀 gcd 𝑁))
6939, 63, 683eqtrd 2119 1 ((𝑀 ∈ ℕ0𝑁 ∈ ℕ0) → (1st ‘(𝑅𝑁)) = (𝑀 gcd 𝑁))
Colors of variables: wff set class
Syntax hints:  wi 4  wa 102   = wceq 1285  wcel 1434  Vcvv 2612  wss 2984  ifcif 3373  {csn 3422  cop 3425   × cxp 4397  cres 4401  ccom 4403   Fn wfn 4962  wf 4963  cfv 4967  (class class class)co 5589  cmpt2 5591  1st c1st 5842  2nd c2nd 5843  0cc0 7251  0cn0 8563  cz 8644  cuz 8912   mod cmo 9616  seqcseq 9738   gcd cgcd 10716
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 1377  ax-7 1378  ax-gen 1379  ax-ie1 1423  ax-ie2 1424  ax-8 1436  ax-10 1437  ax-11 1438  ax-i12 1439  ax-bndl 1440  ax-4 1441  ax-13 1445  ax-14 1446  ax-17 1460  ax-i9 1464  ax-ial 1468  ax-i5r 1469  ax-ext 2065  ax-coll 3919  ax-sep 3922  ax-nul 3930  ax-pow 3974  ax-pr 3999  ax-un 4223  ax-setind 4315  ax-iinf 4365  ax-cnex 7337  ax-resscn 7338  ax-1cn 7339  ax-1re 7340  ax-icn 7341  ax-addcl 7342  ax-addrcl 7343  ax-mulcl 7344  ax-mulrcl 7345  ax-addcom 7346  ax-mulcom 7347  ax-addass 7348  ax-mulass 7349  ax-distr 7350  ax-i2m1 7351  ax-0lt1 7352  ax-1rid 7353  ax-0id 7354  ax-rnegex 7355  ax-precex 7356  ax-cnre 7357  ax-pre-ltirr 7358  ax-pre-ltwlin 7359  ax-pre-lttrn 7360  ax-pre-apti 7361  ax-pre-ltadd 7362  ax-pre-mulgt0 7363  ax-pre-mulext 7364  ax-arch 7365  ax-caucvg 7366
This theorem depends on definitions:  df-bi 115  df-dc 777  df-3or 921  df-3an 922  df-tru 1288  df-fal 1291  df-nf 1391  df-sb 1688  df-eu 1946  df-mo 1947  df-clab 2070  df-cleq 2076  df-clel 2079  df-nfc 2212  df-ne 2250  df-nel 2345  df-ral 2358  df-rex 2359  df-reu 2360  df-rmo 2361  df-rab 2362  df-v 2614  df-sbc 2827  df-csb 2920  df-dif 2986  df-un 2988  df-in 2990  df-ss 2997  df-nul 3270  df-if 3374  df-pw 3408  df-sn 3428  df-pr 3429  df-op 3431  df-uni 3628  df-int 3663  df-iun 3706  df-br 3812  df-opab 3866  df-mpt 3867  df-tr 3902  df-id 4083  df-po 4086  df-iso 4087  df-iord 4156  df-on 4158  df-ilim 4159  df-suc 4161  df-iom 4368  df-xp 4405  df-rel 4406  df-cnv 4407  df-co 4408  df-dm 4409  df-rn 4410  df-res 4411  df-ima 4412  df-iota 4932  df-fun 4969  df-fn 4970  df-f 4971  df-f1 4972  df-fo 4973  df-f1o 4974  df-fv 4975  df-riota 5545  df-ov 5592  df-oprab 5593  df-mpt2 5594  df-1st 5844  df-2nd 5845  df-recs 6000  df-frec 6086  df-sup 6584  df-pnf 7425  df-mnf 7426  df-xr 7427  df-ltxr 7428  df-le 7429  df-sub 7556  df-neg 7557  df-reap 7950  df-ap 7957  df-div 8036  df-inn 8315  df-2 8373  df-3 8374  df-4 8375  df-n0 8564  df-z 8645  df-uz 8913  df-q 8998  df-rp 9028  df-fz 9318  df-fzo 9442  df-fl 9564  df-mod 9617  df-iseq 9739  df-iexp 9790  df-cj 10101  df-re 10102  df-im 10103  df-rsqrt 10256  df-abs 10257  df-dvds 10575  df-gcd 10717
This theorem is referenced by: (None)
  Copyright terms: Public domain W3C validator