srcContext
stringlengths
44
89.6k
theoremStatement
stringlengths
28
895
theoremName
stringlengths
3
58
fileCreated
nullclasses
15 values
theoremCreated
nullclasses
50 values
file
stringclasses
23 values
positionMetadata
dict
dependencyMetadata
dict
proofMetadata
dict
split
stringclasses
4 values
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/
lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1
HTPI.mod_succ_lt
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 7, "tokenPositionInFile": 96, "theoremPositionInFile": 0 }
{ "inFilePremises": false, "repositoryPremises": false }
{ "hasProof": true, "proof": ":= by\n have h : n + 1 > 0 := Nat.succ_pos n\n show a % (n + 1) < n + 1 from Nat.mod_lt a h\n done", "proofType": "tactic", "proofLengthLines": 3, "proofLengthTokens": 98 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/
theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b)
HTPI.dvd_mod_of_dvd_a_b
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 117, "tokenPositionInFile": 3037, "theoremPositionInFile": 25 }
{ "inFilePremises": false, "repositoryPremises": false }
{ "hasProof": true, "proof": ":= by\n set q : Nat := a / b\n have h3 : b * q + a % b = a := Nat.div_add_mod a b\n obtain (j : Nat) (h4 : a = d * j) from h1\n obtain (k : Nat) (h5 : b = d * k) from h2\n define --Goal : ∃ (c : Nat), a % b = d * c\n apply Exists.intro (j - k * q)\n show a % b = d * (j - k * q) from\n calc a % b\n _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm\n _ = a - b * q := by rw [h3]\n _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc]\n _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm\n done", "proofType": "tactic", "proofLengthLines": 13, "proofLengthTokens": 538 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done
theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a
HTPI.dvd_a_of_dvd_b_mod
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 133, "tokenPositionInFile": 3662, "theoremPositionInFile": 26 }
{ "inFilePremises": false, "repositoryPremises": false }
{ "hasProof": false, "proof": ":= sorry", "proofType": "term", "proofLengthLines": 0, "proofLengthTokens": 8 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7
lemma gcd_base (a : Nat) : gcd a 0 = a
HTPI.gcd_base
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 138, "tokenPositionInFile": 3791, "theoremPositionInFile": 27 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by rfl", "proofType": "tactic", "proofLengthLines": 0, "proofLengthTokens": 9 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl
lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b)
HTPI.gcd_nonzero
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 140, "tokenPositionInFile": 3841, "theoremPositionInFile": 28 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by\n obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h\n rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1))\n rfl\n done", "proofType": "tactic", "proofLengthLines": 4, "proofLengthTokens": 158 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done
lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b
HTPI.mod_nonzero_lt
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 147, "tokenPositionInFile": 4081, "theoremPositionInFile": 29 }
{ "inFilePremises": false, "repositoryPremises": false }
{ "hasProof": true, "proof": ":= by\n have h1 : b > 0 := Nat.pos_of_ne_zero h\n show a % b < b from Nat.mod_lt a h1\n done", "proofType": "tactic", "proofLengthLines": 3, "proofLengthTokens": 92 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done
lemma dvd_self (n : Nat) : n ∣ n
HTPI.dvd_self
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 152, "tokenPositionInFile": 4240, "theoremPositionInFile": 30 }
{ "inFilePremises": false, "repositoryPremises": false }
{ "hasProof": true, "proof": ":= by\n apply Exists.intro 1\n ring\n done", "proofType": "tactic", "proofLengthLines": 3, "proofLengthTokens": 42 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done
theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b
HTPI.gcd_dvd
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 157, "tokenPositionInFile": 4317, "theoremPositionInFile": 31 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by\n by_strong_induc\n fix b : Nat\n assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1\n fix a : Nat\n by_cases h1 : b = 0\n · -- Case 1. h1 : b = 0\n rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0\n apply And.intro (dvd_self a)\n define\n apply Exists.intro 0\n rfl\n done\n · -- Case 2. h1 : b ≠ 0\n rewrite [gcd_nonzero a h1]\n --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b\n have h2 : a % b < b := mod_nonzero_lt a h1\n have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) :=\n ih (a % b) h2 b\n apply And.intro _ h3.left\n show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right\n done\n done", "proofType": "tactic", "proofLengthLines": 22, "proofLengthTokens": 670 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done
theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a
HTPI.gcd_dvd_left
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 181, "tokenPositionInFile": 5052, "theoremPositionInFile": 32 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= (gcd_dvd b a).left", "proofType": "term", "proofLengthLines": 0, "proofLengthTokens": 21 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left
theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b
HTPI.gcd_dvd_right
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 183, "tokenPositionInFile": 5124, "theoremPositionInFile": 33 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= (gcd_dvd b a).right", "proofType": "term", "proofLengthLines": 0, "proofLengthTokens": 22 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b := (gcd_dvd b a).right
lemma gcd_c1_base (a : Nat) : gcd_c1 a 0 = 1
HTPI.gcd_c1_base
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 185, "tokenPositionInFile": 5198, "theoremPositionInFile": 34 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by rfl", "proofType": "tactic", "proofLengthLines": 0, "proofLengthTokens": 9 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b := (gcd_dvd b a).right lemma gcd_c1_base (a : Nat) : gcd_c1 a 0 = 1 := by rfl
lemma gcd_c1_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c1 a b = gcd_c2 b (a % b)
HTPI.gcd_c1_nonzero
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 187, "tokenPositionInFile": 5254, "theoremPositionInFile": 35 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by\n obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h\n rewrite [h2]\n rfl\n done", "proofType": "tactic", "proofLengthLines": 4, "proofLengthTokens": 105 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b := (gcd_dvd b a).right lemma gcd_c1_base (a : Nat) : gcd_c1 a 0 = 1 := by rfl lemma gcd_c1_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c1 a b = gcd_c2 b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done
lemma gcd_c2_base (a : Nat) : gcd_c2 a 0 = 0
HTPI.gcd_c2_base
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 194, "tokenPositionInFile": 5450, "theoremPositionInFile": 36 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by rfl", "proofType": "tactic", "proofLengthLines": 0, "proofLengthTokens": 9 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b := (gcd_dvd b a).right lemma gcd_c1_base (a : Nat) : gcd_c1 a 0 = 1 := by rfl lemma gcd_c1_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c1 a b = gcd_c2 b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done lemma gcd_c2_base (a : Nat) : gcd_c2 a 0 = 0 := by rfl
lemma gcd_c2_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c2 a b = gcd_c1 b (a % b) - (gcd_c2 b (a % b)) * ↑(a / b)
HTPI.gcd_c2_nonzero
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 196, "tokenPositionInFile": 5506, "theoremPositionInFile": 37 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by\n obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h\n rewrite [h2]\n rfl\n done", "proofType": "tactic", "proofLengthLines": 4, "proofLengthTokens": 105 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b := (gcd_dvd b a).right lemma gcd_c1_base (a : Nat) : gcd_c1 a 0 = 1 := by rfl lemma gcd_c1_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c1 a b = gcd_c2 b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done lemma gcd_c2_base (a : Nat) : gcd_c2 a 0 = 0 := by rfl lemma gcd_c2_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c2 a b = gcd_c1 b (a % b) - (gcd_c2 b (a % b)) * ↑(a / b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done
theorem gcd_lin_comb : ∀ (b a : Nat), (gcd_c1 a b) * ↑a + (gcd_c2 a b) * ↑b = ↑(gcd a b)
HTPI.gcd_lin_comb
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 203, "tokenPositionInFile": 5734, "theoremPositionInFile": 38 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by\n by_strong_induc\n fix b : Nat\n assume ih : ∀ b_1 < b, ∀ (a : Nat),\n (gcd_c1 a b_1) * ↑a + (gcd_c2 a b_1) * ↑b_1 = ↑(gcd a b_1)\n fix a : Nat\n by_cases h1 : b = 0\n · -- Case 1. h1 : b = 0\n rewrite [h1, gcd_c1_base, gcd_c2_base, gcd_base]\n --Goal : 1 * ↑a + 0 * ↑0 = ↑a\n ring\n done\n · -- Case 2. h1 : b ≠ 0\n rewrite [gcd_c1_nonzero a h1, gcd_c2_nonzero a h1, gcd_nonzero a h1]\n --Goal : gcd_c2 b (a % b) * ↑a +\n -- (gcd_c1 b (a % b) - gcd_c2 b (a % b) * ↑(a / b)) * ↑b =\n -- ↑(gcd b (a % b))\n set r : Nat := a % b\n set q : Nat := a / b\n set s : Int := gcd_c1 b r\n set t : Int := gcd_c2 b r\n --Goal : t * ↑a + (s - t * ↑q) * ↑b = ↑(gcd b r)\n have h2 : r < b := mod_nonzero_lt a h1\n have h3 : s * ↑b + t * ↑r = ↑(gcd b r) := ih r h2 b\n have h4 : b * q + r = a := Nat.div_add_mod a b\n rewrite [←h3, ←h4]\n rewrite [Nat.cast_add, Nat.cast_mul]\n --Goal : t * (↑b * ↑q + ↑r) + (s - t * ↑q) * ↑b = s * ↑b + t * ↑r\n ring\n done\n done", "proofType": "tactic", "proofLengthLines": 30, "proofLengthTokens": 1012 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b := (gcd_dvd b a).right lemma gcd_c1_base (a : Nat) : gcd_c1 a 0 = 1 := by rfl lemma gcd_c1_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c1 a b = gcd_c2 b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done lemma gcd_c2_base (a : Nat) : gcd_c2 a 0 = 0 := by rfl lemma gcd_c2_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c2 a b = gcd_c1 b (a % b) - (gcd_c2 b (a % b)) * ↑(a / b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done theorem gcd_lin_comb : ∀ (b a : Nat), (gcd_c1 a b) * ↑a + (gcd_c2 a b) * ↑b = ↑(gcd a b) := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd_c1 a b_1) * ↑a + (gcd_c2 a b_1) * ↑b_1 = ↑(gcd a b_1) fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_c1_base, gcd_c2_base, gcd_base] --Goal : 1 * ↑a + 0 * ↑0 = ↑a ring done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_c1_nonzero a h1, gcd_c2_nonzero a h1, gcd_nonzero a h1] --Goal : gcd_c2 b (a % b) * ↑a + -- (gcd_c1 b (a % b) - gcd_c2 b (a % b) * ↑(a / b)) * ↑b = -- ↑(gcd b (a % b)) set r : Nat := a % b set q : Nat := a / b set s : Int := gcd_c1 b r set t : Int := gcd_c2 b r --Goal : t * ↑a + (s - t * ↑q) * ↑b = ↑(gcd b r) have h2 : r < b := mod_nonzero_lt a h1 have h3 : s * ↑b + t * ↑r = ↑(gcd b r) := ih r h2 b have h4 : b * q + r = a := Nat.div_add_mod a b rewrite [←h3, ←h4] rewrite [Nat.cast_add, Nat.cast_mul] --Goal : t * (↑b * ↑q + ↑r) + (s - t * ↑q) * ↑b = s * ↑b + t * ↑r ring done done #eval gcd_c1 672 161 --Answer: 6 #eval gcd_c2 672 161 --Answer: -25 --Note 6 * 672 - 25 * 161 = 4032 - 4025 = 7 = gcd 672 161
theorem Theorem_7_1_6 {d a b : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ gcd a b
HTPI.Theorem_7_1_6
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 240, "tokenPositionInFile": 6972, "theoremPositionInFile": 39 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by\n rewrite [←Int.natCast_dvd_natCast] --Goal : ↑d ∣ ↑(gcd a b)\n set s : Int := gcd_c1 a b\n set t : Int := gcd_c2 a b\n have h3 : s * ↑a + t * ↑b = ↑(gcd a b) := gcd_lin_comb b a\n rewrite [←h3] --Goal : ↑d ∣ s * ↑a + t * ↑b\n obtain (j : Nat) (h4 : a = d * j) from h1\n obtain (k : Nat) (h5 : b = d * k) from h2\n rewrite [h4, h5, Nat.cast_mul, Nat.cast_mul]\n --Goal : ↑d ∣ s * (↑d * ↑j) + t * (↑d * ↑k)\n define\n apply Exists.intro (s * ↑j + t * ↑k)\n ring\n done", "proofType": "tactic", "proofLengthLines": 13, "proofLengthTokens": 494 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b := (gcd_dvd b a).right lemma gcd_c1_base (a : Nat) : gcd_c1 a 0 = 1 := by rfl lemma gcd_c1_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c1 a b = gcd_c2 b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done lemma gcd_c2_base (a : Nat) : gcd_c2 a 0 = 0 := by rfl lemma gcd_c2_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c2 a b = gcd_c1 b (a % b) - (gcd_c2 b (a % b)) * ↑(a / b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done theorem gcd_lin_comb : ∀ (b a : Nat), (gcd_c1 a b) * ↑a + (gcd_c2 a b) * ↑b = ↑(gcd a b) := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd_c1 a b_1) * ↑a + (gcd_c2 a b_1) * ↑b_1 = ↑(gcd a b_1) fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_c1_base, gcd_c2_base, gcd_base] --Goal : 1 * ↑a + 0 * ↑0 = ↑a ring done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_c1_nonzero a h1, gcd_c2_nonzero a h1, gcd_nonzero a h1] --Goal : gcd_c2 b (a % b) * ↑a + -- (gcd_c1 b (a % b) - gcd_c2 b (a % b) * ↑(a / b)) * ↑b = -- ↑(gcd b (a % b)) set r : Nat := a % b set q : Nat := a / b set s : Int := gcd_c1 b r set t : Int := gcd_c2 b r --Goal : t * ↑a + (s - t * ↑q) * ↑b = ↑(gcd b r) have h2 : r < b := mod_nonzero_lt a h1 have h3 : s * ↑b + t * ↑r = ↑(gcd b r) := ih r h2 b have h4 : b * q + r = a := Nat.div_add_mod a b rewrite [←h3, ←h4] rewrite [Nat.cast_add, Nat.cast_mul] --Goal : t * (↑b * ↑q + ↑r) + (s - t * ↑q) * ↑b = s * ↑b + t * ↑r ring done done #eval gcd_c1 672 161 --Answer: 6 #eval gcd_c2 672 161 --Answer: -25 --Note 6 * 672 - 25 * 161 = 4032 - 4025 = 7 = gcd 672 161 theorem Theorem_7_1_6 {d a b : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ gcd a b := by rewrite [←Int.natCast_dvd_natCast] --Goal : ↑d ∣ ↑(gcd a b) set s : Int := gcd_c1 a b set t : Int := gcd_c2 a b have h3 : s * ↑a + t * ↑b = ↑(gcd a b) := gcd_lin_comb b a rewrite [←h3] --Goal : ↑d ∣ s * ↑a + t * ↑b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 rewrite [h4, h5, Nat.cast_mul, Nat.cast_mul] --Goal : ↑d ∣ s * (↑d * ↑j) + t * (↑d * ↑k) define apply Exists.intro (s * ↑j + t * ↑k) ring done /- Section 7.2 -/
theorem dvd_trans {a b c : Nat} (h1 : a ∣ b) (h2 : b ∣ c) : a ∣ c
HTPI.dvd_trans
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 257, "tokenPositionInFile": 7566, "theoremPositionInFile": 40 }
{ "inFilePremises": false, "repositoryPremises": false }
{ "hasProof": true, "proof": ":= by\n define at h1; define at h2; define\n obtain (m : Nat) (h3 : b = a * m) from h1\n obtain (n : Nat) (h4 : c = b * n) from h2\n rewrite [h3, mul_assoc] at h4\n apply Exists.intro (m * n)\n show c = a * (m * n) from h4\n done", "proofType": "tactic", "proofLengthLines": 7, "proofLengthTokens": 229 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b := (gcd_dvd b a).right lemma gcd_c1_base (a : Nat) : gcd_c1 a 0 = 1 := by rfl lemma gcd_c1_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c1 a b = gcd_c2 b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done lemma gcd_c2_base (a : Nat) : gcd_c2 a 0 = 0 := by rfl lemma gcd_c2_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c2 a b = gcd_c1 b (a % b) - (gcd_c2 b (a % b)) * ↑(a / b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done theorem gcd_lin_comb : ∀ (b a : Nat), (gcd_c1 a b) * ↑a + (gcd_c2 a b) * ↑b = ↑(gcd a b) := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd_c1 a b_1) * ↑a + (gcd_c2 a b_1) * ↑b_1 = ↑(gcd a b_1) fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_c1_base, gcd_c2_base, gcd_base] --Goal : 1 * ↑a + 0 * ↑0 = ↑a ring done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_c1_nonzero a h1, gcd_c2_nonzero a h1, gcd_nonzero a h1] --Goal : gcd_c2 b (a % b) * ↑a + -- (gcd_c1 b (a % b) - gcd_c2 b (a % b) * ↑(a / b)) * ↑b = -- ↑(gcd b (a % b)) set r : Nat := a % b set q : Nat := a / b set s : Int := gcd_c1 b r set t : Int := gcd_c2 b r --Goal : t * ↑a + (s - t * ↑q) * ↑b = ↑(gcd b r) have h2 : r < b := mod_nonzero_lt a h1 have h3 : s * ↑b + t * ↑r = ↑(gcd b r) := ih r h2 b have h4 : b * q + r = a := Nat.div_add_mod a b rewrite [←h3, ←h4] rewrite [Nat.cast_add, Nat.cast_mul] --Goal : t * (↑b * ↑q + ↑r) + (s - t * ↑q) * ↑b = s * ↑b + t * ↑r ring done done #eval gcd_c1 672 161 --Answer: 6 #eval gcd_c2 672 161 --Answer: -25 --Note 6 * 672 - 25 * 161 = 4032 - 4025 = 7 = gcd 672 161 theorem Theorem_7_1_6 {d a b : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ gcd a b := by rewrite [←Int.natCast_dvd_natCast] --Goal : ↑d ∣ ↑(gcd a b) set s : Int := gcd_c1 a b set t : Int := gcd_c2 a b have h3 : s * ↑a + t * ↑b = ↑(gcd a b) := gcd_lin_comb b a rewrite [←h3] --Goal : ↑d ∣ s * ↑a + t * ↑b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 rewrite [h4, h5, Nat.cast_mul, Nat.cast_mul] --Goal : ↑d ∣ s * (↑d * ↑j) + t * (↑d * ↑k) define apply Exists.intro (s * ↑j + t * ↑k) ring done /- Section 7.2 -/ theorem dvd_trans {a b c : Nat} (h1 : a ∣ b) (h2 : b ∣ c) : a ∣ c := by define at h1; define at h2; define obtain (m : Nat) (h3 : b = a * m) from h1 obtain (n : Nat) (h4 : c = b * n) from h2 rewrite [h3, mul_assoc] at h4 apply Exists.intro (m * n) show c = a * (m * n) from h4 done
lemma exists_prime_factor : ∀ (n : Nat), 2 ≤ n → ∃ (p : Nat), prime_factor p n
HTPI.exists_prime_factor
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 266, "tokenPositionInFile": 7863, "theoremPositionInFile": 41 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by\n by_strong_induc\n fix n : Nat\n assume ih : ∀ n_1 < n, 2 ≤ n_1 → ∃ (p : Nat), prime_factor p n_1\n assume h1 : 2 ≤ n\n by_cases h2 : prime n\n · -- Case 1. h2 : prime n\n apply Exists.intro n\n define --Goal : prime n ∧ n ∣ n\n show prime n ∧ n ∣ n from And.intro h2 (dvd_self n)\n done\n · -- Case 2. h2 : ¬prime n\n define at h2\n --h2 : ¬(2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n)\n demorgan at h2\n disj_syll h2 h1\n obtain (a : Nat) (h3 : ∃ (b : Nat), a * b = n ∧ a < n ∧ b < n) from h2\n obtain (b : Nat) (h4 : a * b = n ∧ a < n ∧ b < n) from h3\n have h5 : 2 ≤ a := by\n by_contra h6\n have h7 : a ≤ 1 := by linarith\n have h8 : n ≤ b :=\n calc n\n _ = a * b := h4.left.symm\n _ ≤ 1 * b := by rel [h7]\n _ = b := by ring\n linarith --n ≤ b contradicts b < n\n done\n have h6 : ∃ (p : Nat), prime_factor p a := ih a h4.right.left h5\n obtain (p : Nat) (h7 : prime_factor p a) from h6\n apply Exists.intro p\n define --Goal : prime p ∧ p ∣ n\n define at h7 --h7 : prime p ∧ p ∣ a\n apply And.intro h7.left\n have h8 : a ∣ n := by\n apply Exists.intro b\n show n = a * b from (h4.left).symm\n done\n show p ∣ n from dvd_trans h7.right h8\n done\n done", "proofType": "tactic", "proofLengthLines": 40, "proofLengthTokens": 1310 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b := (gcd_dvd b a).right lemma gcd_c1_base (a : Nat) : gcd_c1 a 0 = 1 := by rfl lemma gcd_c1_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c1 a b = gcd_c2 b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done lemma gcd_c2_base (a : Nat) : gcd_c2 a 0 = 0 := by rfl lemma gcd_c2_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c2 a b = gcd_c1 b (a % b) - (gcd_c2 b (a % b)) * ↑(a / b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done theorem gcd_lin_comb : ∀ (b a : Nat), (gcd_c1 a b) * ↑a + (gcd_c2 a b) * ↑b = ↑(gcd a b) := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd_c1 a b_1) * ↑a + (gcd_c2 a b_1) * ↑b_1 = ↑(gcd a b_1) fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_c1_base, gcd_c2_base, gcd_base] --Goal : 1 * ↑a + 0 * ↑0 = ↑a ring done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_c1_nonzero a h1, gcd_c2_nonzero a h1, gcd_nonzero a h1] --Goal : gcd_c2 b (a % b) * ↑a + -- (gcd_c1 b (a % b) - gcd_c2 b (a % b) * ↑(a / b)) * ↑b = -- ↑(gcd b (a % b)) set r : Nat := a % b set q : Nat := a / b set s : Int := gcd_c1 b r set t : Int := gcd_c2 b r --Goal : t * ↑a + (s - t * ↑q) * ↑b = ↑(gcd b r) have h2 : r < b := mod_nonzero_lt a h1 have h3 : s * ↑b + t * ↑r = ↑(gcd b r) := ih r h2 b have h4 : b * q + r = a := Nat.div_add_mod a b rewrite [←h3, ←h4] rewrite [Nat.cast_add, Nat.cast_mul] --Goal : t * (↑b * ↑q + ↑r) + (s - t * ↑q) * ↑b = s * ↑b + t * ↑r ring done done #eval gcd_c1 672 161 --Answer: 6 #eval gcd_c2 672 161 --Answer: -25 --Note 6 * 672 - 25 * 161 = 4032 - 4025 = 7 = gcd 672 161 theorem Theorem_7_1_6 {d a b : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ gcd a b := by rewrite [←Int.natCast_dvd_natCast] --Goal : ↑d ∣ ↑(gcd a b) set s : Int := gcd_c1 a b set t : Int := gcd_c2 a b have h3 : s * ↑a + t * ↑b = ↑(gcd a b) := gcd_lin_comb b a rewrite [←h3] --Goal : ↑d ∣ s * ↑a + t * ↑b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 rewrite [h4, h5, Nat.cast_mul, Nat.cast_mul] --Goal : ↑d ∣ s * (↑d * ↑j) + t * (↑d * ↑k) define apply Exists.intro (s * ↑j + t * ↑k) ring done /- Section 7.2 -/ theorem dvd_trans {a b c : Nat} (h1 : a ∣ b) (h2 : b ∣ c) : a ∣ c := by define at h1; define at h2; define obtain (m : Nat) (h3 : b = a * m) from h1 obtain (n : Nat) (h4 : c = b * n) from h2 rewrite [h3, mul_assoc] at h4 apply Exists.intro (m * n) show c = a * (m * n) from h4 done lemma exists_prime_factor : ∀ (n : Nat), 2 ≤ n → ∃ (p : Nat), prime_factor p n := by by_strong_induc fix n : Nat assume ih : ∀ n_1 < n, 2 ≤ n_1 → ∃ (p : Nat), prime_factor p n_1 assume h1 : 2 ≤ n by_cases h2 : prime n · -- Case 1. h2 : prime n apply Exists.intro n define --Goal : prime n ∧ n ∣ n show prime n ∧ n ∣ n from And.intro h2 (dvd_self n) done · -- Case 2. h2 : ¬prime n define at h2 --h2 : ¬(2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n) demorgan at h2 disj_syll h2 h1 obtain (a : Nat) (h3 : ∃ (b : Nat), a * b = n ∧ a < n ∧ b < n) from h2 obtain (b : Nat) (h4 : a * b = n ∧ a < n ∧ b < n) from h3 have h5 : 2 ≤ a := by by_contra h6 have h7 : a ≤ 1 := by linarith have h8 : n ≤ b := calc n _ = a * b := h4.left.symm _ ≤ 1 * b := by rel [h7] _ = b := by ring linarith --n ≤ b contradicts b < n done have h6 : ∃ (p : Nat), prime_factor p a := ih a h4.right.left h5 obtain (p : Nat) (h7 : prime_factor p a) from h6 apply Exists.intro p define --Goal : prime p ∧ p ∣ n define at h7 --h7 : prime p ∧ p ∣ a apply And.intro h7.left have h8 : a ∣ n := by apply Exists.intro b show n = a * b from (h4.left).symm done show p ∣ n from dvd_trans h7.right h8 done done
lemma exists_least_prime_factor {n : Nat} (h : 2 ≤ n) : ∃ (p : Nat), prime_factor p n ∧ ∀ (q : Nat), prime_factor q n → p ≤ q
HTPI.exists_least_prime_factor
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 309, "tokenPositionInFile": 9258, "theoremPositionInFile": 42 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by\n set S : Set Nat := {p : Nat | prime_factor p n}\n have h2 : ∃ (p : Nat), p ∈ S := exists_prime_factor n h\n show ∃ (p : Nat), prime_factor p n ∧\n ∀ (q : Nat), prime_factor q n → p ≤ q from well_ord_princ S h2\n done", "proofType": "tactic", "proofLengthLines": 5, "proofLengthTokens": 226 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b := (gcd_dvd b a).right lemma gcd_c1_base (a : Nat) : gcd_c1 a 0 = 1 := by rfl lemma gcd_c1_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c1 a b = gcd_c2 b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done lemma gcd_c2_base (a : Nat) : gcd_c2 a 0 = 0 := by rfl lemma gcd_c2_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c2 a b = gcd_c1 b (a % b) - (gcd_c2 b (a % b)) * ↑(a / b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done theorem gcd_lin_comb : ∀ (b a : Nat), (gcd_c1 a b) * ↑a + (gcd_c2 a b) * ↑b = ↑(gcd a b) := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd_c1 a b_1) * ↑a + (gcd_c2 a b_1) * ↑b_1 = ↑(gcd a b_1) fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_c1_base, gcd_c2_base, gcd_base] --Goal : 1 * ↑a + 0 * ↑0 = ↑a ring done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_c1_nonzero a h1, gcd_c2_nonzero a h1, gcd_nonzero a h1] --Goal : gcd_c2 b (a % b) * ↑a + -- (gcd_c1 b (a % b) - gcd_c2 b (a % b) * ↑(a / b)) * ↑b = -- ↑(gcd b (a % b)) set r : Nat := a % b set q : Nat := a / b set s : Int := gcd_c1 b r set t : Int := gcd_c2 b r --Goal : t * ↑a + (s - t * ↑q) * ↑b = ↑(gcd b r) have h2 : r < b := mod_nonzero_lt a h1 have h3 : s * ↑b + t * ↑r = ↑(gcd b r) := ih r h2 b have h4 : b * q + r = a := Nat.div_add_mod a b rewrite [←h3, ←h4] rewrite [Nat.cast_add, Nat.cast_mul] --Goal : t * (↑b * ↑q + ↑r) + (s - t * ↑q) * ↑b = s * ↑b + t * ↑r ring done done #eval gcd_c1 672 161 --Answer: 6 #eval gcd_c2 672 161 --Answer: -25 --Note 6 * 672 - 25 * 161 = 4032 - 4025 = 7 = gcd 672 161 theorem Theorem_7_1_6 {d a b : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ gcd a b := by rewrite [←Int.natCast_dvd_natCast] --Goal : ↑d ∣ ↑(gcd a b) set s : Int := gcd_c1 a b set t : Int := gcd_c2 a b have h3 : s * ↑a + t * ↑b = ↑(gcd a b) := gcd_lin_comb b a rewrite [←h3] --Goal : ↑d ∣ s * ↑a + t * ↑b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 rewrite [h4, h5, Nat.cast_mul, Nat.cast_mul] --Goal : ↑d ∣ s * (↑d * ↑j) + t * (↑d * ↑k) define apply Exists.intro (s * ↑j + t * ↑k) ring done /- Section 7.2 -/ theorem dvd_trans {a b c : Nat} (h1 : a ∣ b) (h2 : b ∣ c) : a ∣ c := by define at h1; define at h2; define obtain (m : Nat) (h3 : b = a * m) from h1 obtain (n : Nat) (h4 : c = b * n) from h2 rewrite [h3, mul_assoc] at h4 apply Exists.intro (m * n) show c = a * (m * n) from h4 done lemma exists_prime_factor : ∀ (n : Nat), 2 ≤ n → ∃ (p : Nat), prime_factor p n := by by_strong_induc fix n : Nat assume ih : ∀ n_1 < n, 2 ≤ n_1 → ∃ (p : Nat), prime_factor p n_1 assume h1 : 2 ≤ n by_cases h2 : prime n · -- Case 1. h2 : prime n apply Exists.intro n define --Goal : prime n ∧ n ∣ n show prime n ∧ n ∣ n from And.intro h2 (dvd_self n) done · -- Case 2. h2 : ¬prime n define at h2 --h2 : ¬(2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n) demorgan at h2 disj_syll h2 h1 obtain (a : Nat) (h3 : ∃ (b : Nat), a * b = n ∧ a < n ∧ b < n) from h2 obtain (b : Nat) (h4 : a * b = n ∧ a < n ∧ b < n) from h3 have h5 : 2 ≤ a := by by_contra h6 have h7 : a ≤ 1 := by linarith have h8 : n ≤ b := calc n _ = a * b := h4.left.symm _ ≤ 1 * b := by rel [h7] _ = b := by ring linarith --n ≤ b contradicts b < n done have h6 : ∃ (p : Nat), prime_factor p a := ih a h4.right.left h5 obtain (p : Nat) (h7 : prime_factor p a) from h6 apply Exists.intro p define --Goal : prime p ∧ p ∣ n define at h7 --h7 : prime p ∧ p ∣ a apply And.intro h7.left have h8 : a ∣ n := by apply Exists.intro b show n = a * b from (h4.left).symm done show p ∣ n from dvd_trans h7.right h8 done done lemma exists_least_prime_factor {n : Nat} (h : 2 ≤ n) : ∃ (p : Nat), prime_factor p n ∧ ∀ (q : Nat), prime_factor q n → p ≤ q := by set S : Set Nat := {p : Nat | prime_factor p n} have h2 : ∃ (p : Nat), p ∈ S := exists_prime_factor n h show ∃ (p : Nat), prime_factor p n ∧ ∀ (q : Nat), prime_factor q n → p ≤ q from well_ord_princ S h2 done
lemma all_prime_nil : all_prime []
HTPI.all_prime_nil
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 318, "tokenPositionInFile": 9620, "theoremPositionInFile": 43 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by\n define --Goal : ∀ p ∈ [], prime p\n fix p : Nat\n contrapos --Goal : ¬prime p → p ∉ []\n assume h1 : ¬prime p\n show p ∉ [] from List.not_mem_nil p\n done", "proofType": "tactic", "proofLengthLines": 6, "proofLengthTokens": 167 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b := (gcd_dvd b a).right lemma gcd_c1_base (a : Nat) : gcd_c1 a 0 = 1 := by rfl lemma gcd_c1_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c1 a b = gcd_c2 b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done lemma gcd_c2_base (a : Nat) : gcd_c2 a 0 = 0 := by rfl lemma gcd_c2_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c2 a b = gcd_c1 b (a % b) - (gcd_c2 b (a % b)) * ↑(a / b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done theorem gcd_lin_comb : ∀ (b a : Nat), (gcd_c1 a b) * ↑a + (gcd_c2 a b) * ↑b = ↑(gcd a b) := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd_c1 a b_1) * ↑a + (gcd_c2 a b_1) * ↑b_1 = ↑(gcd a b_1) fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_c1_base, gcd_c2_base, gcd_base] --Goal : 1 * ↑a + 0 * ↑0 = ↑a ring done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_c1_nonzero a h1, gcd_c2_nonzero a h1, gcd_nonzero a h1] --Goal : gcd_c2 b (a % b) * ↑a + -- (gcd_c1 b (a % b) - gcd_c2 b (a % b) * ↑(a / b)) * ↑b = -- ↑(gcd b (a % b)) set r : Nat := a % b set q : Nat := a / b set s : Int := gcd_c1 b r set t : Int := gcd_c2 b r --Goal : t * ↑a + (s - t * ↑q) * ↑b = ↑(gcd b r) have h2 : r < b := mod_nonzero_lt a h1 have h3 : s * ↑b + t * ↑r = ↑(gcd b r) := ih r h2 b have h4 : b * q + r = a := Nat.div_add_mod a b rewrite [←h3, ←h4] rewrite [Nat.cast_add, Nat.cast_mul] --Goal : t * (↑b * ↑q + ↑r) + (s - t * ↑q) * ↑b = s * ↑b + t * ↑r ring done done #eval gcd_c1 672 161 --Answer: 6 #eval gcd_c2 672 161 --Answer: -25 --Note 6 * 672 - 25 * 161 = 4032 - 4025 = 7 = gcd 672 161 theorem Theorem_7_1_6 {d a b : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ gcd a b := by rewrite [←Int.natCast_dvd_natCast] --Goal : ↑d ∣ ↑(gcd a b) set s : Int := gcd_c1 a b set t : Int := gcd_c2 a b have h3 : s * ↑a + t * ↑b = ↑(gcd a b) := gcd_lin_comb b a rewrite [←h3] --Goal : ↑d ∣ s * ↑a + t * ↑b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 rewrite [h4, h5, Nat.cast_mul, Nat.cast_mul] --Goal : ↑d ∣ s * (↑d * ↑j) + t * (↑d * ↑k) define apply Exists.intro (s * ↑j + t * ↑k) ring done /- Section 7.2 -/ theorem dvd_trans {a b c : Nat} (h1 : a ∣ b) (h2 : b ∣ c) : a ∣ c := by define at h1; define at h2; define obtain (m : Nat) (h3 : b = a * m) from h1 obtain (n : Nat) (h4 : c = b * n) from h2 rewrite [h3, mul_assoc] at h4 apply Exists.intro (m * n) show c = a * (m * n) from h4 done lemma exists_prime_factor : ∀ (n : Nat), 2 ≤ n → ∃ (p : Nat), prime_factor p n := by by_strong_induc fix n : Nat assume ih : ∀ n_1 < n, 2 ≤ n_1 → ∃ (p : Nat), prime_factor p n_1 assume h1 : 2 ≤ n by_cases h2 : prime n · -- Case 1. h2 : prime n apply Exists.intro n define --Goal : prime n ∧ n ∣ n show prime n ∧ n ∣ n from And.intro h2 (dvd_self n) done · -- Case 2. h2 : ¬prime n define at h2 --h2 : ¬(2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n) demorgan at h2 disj_syll h2 h1 obtain (a : Nat) (h3 : ∃ (b : Nat), a * b = n ∧ a < n ∧ b < n) from h2 obtain (b : Nat) (h4 : a * b = n ∧ a < n ∧ b < n) from h3 have h5 : 2 ≤ a := by by_contra h6 have h7 : a ≤ 1 := by linarith have h8 : n ≤ b := calc n _ = a * b := h4.left.symm _ ≤ 1 * b := by rel [h7] _ = b := by ring linarith --n ≤ b contradicts b < n done have h6 : ∃ (p : Nat), prime_factor p a := ih a h4.right.left h5 obtain (p : Nat) (h7 : prime_factor p a) from h6 apply Exists.intro p define --Goal : prime p ∧ p ∣ n define at h7 --h7 : prime p ∧ p ∣ a apply And.intro h7.left have h8 : a ∣ n := by apply Exists.intro b show n = a * b from (h4.left).symm done show p ∣ n from dvd_trans h7.right h8 done done lemma exists_least_prime_factor {n : Nat} (h : 2 ≤ n) : ∃ (p : Nat), prime_factor p n ∧ ∀ (q : Nat), prime_factor q n → p ≤ q := by set S : Set Nat := {p : Nat | prime_factor p n} have h2 : ∃ (p : Nat), p ∈ S := exists_prime_factor n h show ∃ (p : Nat), prime_factor p n ∧ ∀ (q : Nat), prime_factor q n → p ≤ q from well_ord_princ S h2 done lemma all_prime_nil : all_prime [] := by define --Goal : ∀ p ∈ [], prime p fix p : Nat contrapos --Goal : ¬prime p → p ∉ [] assume h1 : ¬prime p show p ∉ [] from List.not_mem_nil p done
lemma all_prime_cons (n : Nat) (L : List Nat) : all_prime (n :: L) ↔ prime n ∧ all_prime L
HTPI.all_prime_cons
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 326, "tokenPositionInFile": 9824, "theoremPositionInFile": 44 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by\n apply Iff.intro\n · -- (→)\n assume h1 : all_prime (n :: L) --Goal : prime n ∧ all_prime L\n define at h1 --h1 : ∀ p ∈ n :: L, prime p\n apply And.intro (h1 n (List.mem_cons_self n L))\n define --Goal : ∀ p ∈ L, prime p\n fix p : Nat\n assume h2 : p ∈ L\n show prime p from h1 p (List.mem_cons_of_mem n h2)\n done\n · -- (←)\n assume h1 : prime n ∧ all_prime L --Goal : all_prime (n :: l)\n define : all_prime L at h1\n define\n fix p : Nat\n assume h2 : p ∈ n :: L\n rewrite [List.mem_cons] at h2 --h2 : p = n ∨ p ∈ L\n by_cases on h2\n · -- Case 1. h2 : p = n\n rewrite [h2]\n show prime n from h1.left\n done\n · -- Case 2. h2 : p ∈ L\n show prime p from h1.right p h2\n done\n done\n done", "proofType": "tactic", "proofLengthLines": 27, "proofLengthTokens": 768 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b := (gcd_dvd b a).right lemma gcd_c1_base (a : Nat) : gcd_c1 a 0 = 1 := by rfl lemma gcd_c1_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c1 a b = gcd_c2 b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done lemma gcd_c2_base (a : Nat) : gcd_c2 a 0 = 0 := by rfl lemma gcd_c2_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c2 a b = gcd_c1 b (a % b) - (gcd_c2 b (a % b)) * ↑(a / b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done theorem gcd_lin_comb : ∀ (b a : Nat), (gcd_c1 a b) * ↑a + (gcd_c2 a b) * ↑b = ↑(gcd a b) := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd_c1 a b_1) * ↑a + (gcd_c2 a b_1) * ↑b_1 = ↑(gcd a b_1) fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_c1_base, gcd_c2_base, gcd_base] --Goal : 1 * ↑a + 0 * ↑0 = ↑a ring done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_c1_nonzero a h1, gcd_c2_nonzero a h1, gcd_nonzero a h1] --Goal : gcd_c2 b (a % b) * ↑a + -- (gcd_c1 b (a % b) - gcd_c2 b (a % b) * ↑(a / b)) * ↑b = -- ↑(gcd b (a % b)) set r : Nat := a % b set q : Nat := a / b set s : Int := gcd_c1 b r set t : Int := gcd_c2 b r --Goal : t * ↑a + (s - t * ↑q) * ↑b = ↑(gcd b r) have h2 : r < b := mod_nonzero_lt a h1 have h3 : s * ↑b + t * ↑r = ↑(gcd b r) := ih r h2 b have h4 : b * q + r = a := Nat.div_add_mod a b rewrite [←h3, ←h4] rewrite [Nat.cast_add, Nat.cast_mul] --Goal : t * (↑b * ↑q + ↑r) + (s - t * ↑q) * ↑b = s * ↑b + t * ↑r ring done done #eval gcd_c1 672 161 --Answer: 6 #eval gcd_c2 672 161 --Answer: -25 --Note 6 * 672 - 25 * 161 = 4032 - 4025 = 7 = gcd 672 161 theorem Theorem_7_1_6 {d a b : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ gcd a b := by rewrite [←Int.natCast_dvd_natCast] --Goal : ↑d ∣ ↑(gcd a b) set s : Int := gcd_c1 a b set t : Int := gcd_c2 a b have h3 : s * ↑a + t * ↑b = ↑(gcd a b) := gcd_lin_comb b a rewrite [←h3] --Goal : ↑d ∣ s * ↑a + t * ↑b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 rewrite [h4, h5, Nat.cast_mul, Nat.cast_mul] --Goal : ↑d ∣ s * (↑d * ↑j) + t * (↑d * ↑k) define apply Exists.intro (s * ↑j + t * ↑k) ring done /- Section 7.2 -/ theorem dvd_trans {a b c : Nat} (h1 : a ∣ b) (h2 : b ∣ c) : a ∣ c := by define at h1; define at h2; define obtain (m : Nat) (h3 : b = a * m) from h1 obtain (n : Nat) (h4 : c = b * n) from h2 rewrite [h3, mul_assoc] at h4 apply Exists.intro (m * n) show c = a * (m * n) from h4 done lemma exists_prime_factor : ∀ (n : Nat), 2 ≤ n → ∃ (p : Nat), prime_factor p n := by by_strong_induc fix n : Nat assume ih : ∀ n_1 < n, 2 ≤ n_1 → ∃ (p : Nat), prime_factor p n_1 assume h1 : 2 ≤ n by_cases h2 : prime n · -- Case 1. h2 : prime n apply Exists.intro n define --Goal : prime n ∧ n ∣ n show prime n ∧ n ∣ n from And.intro h2 (dvd_self n) done · -- Case 2. h2 : ¬prime n define at h2 --h2 : ¬(2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n) demorgan at h2 disj_syll h2 h1 obtain (a : Nat) (h3 : ∃ (b : Nat), a * b = n ∧ a < n ∧ b < n) from h2 obtain (b : Nat) (h4 : a * b = n ∧ a < n ∧ b < n) from h3 have h5 : 2 ≤ a := by by_contra h6 have h7 : a ≤ 1 := by linarith have h8 : n ≤ b := calc n _ = a * b := h4.left.symm _ ≤ 1 * b := by rel [h7] _ = b := by ring linarith --n ≤ b contradicts b < n done have h6 : ∃ (p : Nat), prime_factor p a := ih a h4.right.left h5 obtain (p : Nat) (h7 : prime_factor p a) from h6 apply Exists.intro p define --Goal : prime p ∧ p ∣ n define at h7 --h7 : prime p ∧ p ∣ a apply And.intro h7.left have h8 : a ∣ n := by apply Exists.intro b show n = a * b from (h4.left).symm done show p ∣ n from dvd_trans h7.right h8 done done lemma exists_least_prime_factor {n : Nat} (h : 2 ≤ n) : ∃ (p : Nat), prime_factor p n ∧ ∀ (q : Nat), prime_factor q n → p ≤ q := by set S : Set Nat := {p : Nat | prime_factor p n} have h2 : ∃ (p : Nat), p ∈ S := exists_prime_factor n h show ∃ (p : Nat), prime_factor p n ∧ ∀ (q : Nat), prime_factor q n → p ≤ q from well_ord_princ S h2 done lemma all_prime_nil : all_prime [] := by define --Goal : ∀ p ∈ [], prime p fix p : Nat contrapos --Goal : ¬prime p → p ∉ [] assume h1 : ¬prime p show p ∉ [] from List.not_mem_nil p done lemma all_prime_cons (n : Nat) (L : List Nat) : all_prime (n :: L) ↔ prime n ∧ all_prime L := by apply Iff.intro · -- (→) assume h1 : all_prime (n :: L) --Goal : prime n ∧ all_prime L define at h1 --h1 : ∀ p ∈ n :: L, prime p apply And.intro (h1 n (List.mem_cons_self n L)) define --Goal : ∀ p ∈ L, prime p fix p : Nat assume h2 : p ∈ L show prime p from h1 p (List.mem_cons_of_mem n h2) done · -- (←) assume h1 : prime n ∧ all_prime L --Goal : all_prime (n :: l) define : all_prime L at h1 define fix p : Nat assume h2 : p ∈ n :: L rewrite [List.mem_cons] at h2 --h2 : p = n ∨ p ∈ L by_cases on h2 · -- Case 1. h2 : p = n rewrite [h2] show prime n from h1.left done · -- Case 2. h2 : p ∈ L show prime p from h1.right p h2 done done done
lemma nondec_nil : nondec []
HTPI.nondec_nil
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 356, "tokenPositionInFile": 10689, "theoremPositionInFile": 45 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by\n define --Goal : True\n trivial --trivial proves some obviously true statements, such as True\n done", "proofType": "tactic", "proofLengthLines": 3, "proofLengthTokens": 108 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b := (gcd_dvd b a).right lemma gcd_c1_base (a : Nat) : gcd_c1 a 0 = 1 := by rfl lemma gcd_c1_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c1 a b = gcd_c2 b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done lemma gcd_c2_base (a : Nat) : gcd_c2 a 0 = 0 := by rfl lemma gcd_c2_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c2 a b = gcd_c1 b (a % b) - (gcd_c2 b (a % b)) * ↑(a / b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done theorem gcd_lin_comb : ∀ (b a : Nat), (gcd_c1 a b) * ↑a + (gcd_c2 a b) * ↑b = ↑(gcd a b) := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd_c1 a b_1) * ↑a + (gcd_c2 a b_1) * ↑b_1 = ↑(gcd a b_1) fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_c1_base, gcd_c2_base, gcd_base] --Goal : 1 * ↑a + 0 * ↑0 = ↑a ring done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_c1_nonzero a h1, gcd_c2_nonzero a h1, gcd_nonzero a h1] --Goal : gcd_c2 b (a % b) * ↑a + -- (gcd_c1 b (a % b) - gcd_c2 b (a % b) * ↑(a / b)) * ↑b = -- ↑(gcd b (a % b)) set r : Nat := a % b set q : Nat := a / b set s : Int := gcd_c1 b r set t : Int := gcd_c2 b r --Goal : t * ↑a + (s - t * ↑q) * ↑b = ↑(gcd b r) have h2 : r < b := mod_nonzero_lt a h1 have h3 : s * ↑b + t * ↑r = ↑(gcd b r) := ih r h2 b have h4 : b * q + r = a := Nat.div_add_mod a b rewrite [←h3, ←h4] rewrite [Nat.cast_add, Nat.cast_mul] --Goal : t * (↑b * ↑q + ↑r) + (s - t * ↑q) * ↑b = s * ↑b + t * ↑r ring done done #eval gcd_c1 672 161 --Answer: 6 #eval gcd_c2 672 161 --Answer: -25 --Note 6 * 672 - 25 * 161 = 4032 - 4025 = 7 = gcd 672 161 theorem Theorem_7_1_6 {d a b : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ gcd a b := by rewrite [←Int.natCast_dvd_natCast] --Goal : ↑d ∣ ↑(gcd a b) set s : Int := gcd_c1 a b set t : Int := gcd_c2 a b have h3 : s * ↑a + t * ↑b = ↑(gcd a b) := gcd_lin_comb b a rewrite [←h3] --Goal : ↑d ∣ s * ↑a + t * ↑b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 rewrite [h4, h5, Nat.cast_mul, Nat.cast_mul] --Goal : ↑d ∣ s * (↑d * ↑j) + t * (↑d * ↑k) define apply Exists.intro (s * ↑j + t * ↑k) ring done /- Section 7.2 -/ theorem dvd_trans {a b c : Nat} (h1 : a ∣ b) (h2 : b ∣ c) : a ∣ c := by define at h1; define at h2; define obtain (m : Nat) (h3 : b = a * m) from h1 obtain (n : Nat) (h4 : c = b * n) from h2 rewrite [h3, mul_assoc] at h4 apply Exists.intro (m * n) show c = a * (m * n) from h4 done lemma exists_prime_factor : ∀ (n : Nat), 2 ≤ n → ∃ (p : Nat), prime_factor p n := by by_strong_induc fix n : Nat assume ih : ∀ n_1 < n, 2 ≤ n_1 → ∃ (p : Nat), prime_factor p n_1 assume h1 : 2 ≤ n by_cases h2 : prime n · -- Case 1. h2 : prime n apply Exists.intro n define --Goal : prime n ∧ n ∣ n show prime n ∧ n ∣ n from And.intro h2 (dvd_self n) done · -- Case 2. h2 : ¬prime n define at h2 --h2 : ¬(2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n) demorgan at h2 disj_syll h2 h1 obtain (a : Nat) (h3 : ∃ (b : Nat), a * b = n ∧ a < n ∧ b < n) from h2 obtain (b : Nat) (h4 : a * b = n ∧ a < n ∧ b < n) from h3 have h5 : 2 ≤ a := by by_contra h6 have h7 : a ≤ 1 := by linarith have h8 : n ≤ b := calc n _ = a * b := h4.left.symm _ ≤ 1 * b := by rel [h7] _ = b := by ring linarith --n ≤ b contradicts b < n done have h6 : ∃ (p : Nat), prime_factor p a := ih a h4.right.left h5 obtain (p : Nat) (h7 : prime_factor p a) from h6 apply Exists.intro p define --Goal : prime p ∧ p ∣ n define at h7 --h7 : prime p ∧ p ∣ a apply And.intro h7.left have h8 : a ∣ n := by apply Exists.intro b show n = a * b from (h4.left).symm done show p ∣ n from dvd_trans h7.right h8 done done lemma exists_least_prime_factor {n : Nat} (h : 2 ≤ n) : ∃ (p : Nat), prime_factor p n ∧ ∀ (q : Nat), prime_factor q n → p ≤ q := by set S : Set Nat := {p : Nat | prime_factor p n} have h2 : ∃ (p : Nat), p ∈ S := exists_prime_factor n h show ∃ (p : Nat), prime_factor p n ∧ ∀ (q : Nat), prime_factor q n → p ≤ q from well_ord_princ S h2 done lemma all_prime_nil : all_prime [] := by define --Goal : ∀ p ∈ [], prime p fix p : Nat contrapos --Goal : ¬prime p → p ∉ [] assume h1 : ¬prime p show p ∉ [] from List.not_mem_nil p done lemma all_prime_cons (n : Nat) (L : List Nat) : all_prime (n :: L) ↔ prime n ∧ all_prime L := by apply Iff.intro · -- (→) assume h1 : all_prime (n :: L) --Goal : prime n ∧ all_prime L define at h1 --h1 : ∀ p ∈ n :: L, prime p apply And.intro (h1 n (List.mem_cons_self n L)) define --Goal : ∀ p ∈ L, prime p fix p : Nat assume h2 : p ∈ L show prime p from h1 p (List.mem_cons_of_mem n h2) done · -- (←) assume h1 : prime n ∧ all_prime L --Goal : all_prime (n :: l) define : all_prime L at h1 define fix p : Nat assume h2 : p ∈ n :: L rewrite [List.mem_cons] at h2 --h2 : p = n ∨ p ∈ L by_cases on h2 · -- Case 1. h2 : p = n rewrite [h2] show prime n from h1.left done · -- Case 2. h2 : p ∈ L show prime p from h1.right p h2 done done done lemma nondec_nil : nondec [] := by define --Goal : True trivial --trivial proves some obviously true statements, such as True done
lemma nondec_cons (n : Nat) (L : List Nat) : nondec (n :: L) ↔ (∀ m ∈ L, n ≤ m) ∧ nondec L
HTPI.nondec_cons
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 361, "tokenPositionInFile": 10828, "theoremPositionInFile": 46 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by rfl", "proofType": "tactic", "proofLengthLines": 0, "proofLengthTokens": 9 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b := (gcd_dvd b a).right lemma gcd_c1_base (a : Nat) : gcd_c1 a 0 = 1 := by rfl lemma gcd_c1_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c1 a b = gcd_c2 b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done lemma gcd_c2_base (a : Nat) : gcd_c2 a 0 = 0 := by rfl lemma gcd_c2_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c2 a b = gcd_c1 b (a % b) - (gcd_c2 b (a % b)) * ↑(a / b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done theorem gcd_lin_comb : ∀ (b a : Nat), (gcd_c1 a b) * ↑a + (gcd_c2 a b) * ↑b = ↑(gcd a b) := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd_c1 a b_1) * ↑a + (gcd_c2 a b_1) * ↑b_1 = ↑(gcd a b_1) fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_c1_base, gcd_c2_base, gcd_base] --Goal : 1 * ↑a + 0 * ↑0 = ↑a ring done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_c1_nonzero a h1, gcd_c2_nonzero a h1, gcd_nonzero a h1] --Goal : gcd_c2 b (a % b) * ↑a + -- (gcd_c1 b (a % b) - gcd_c2 b (a % b) * ↑(a / b)) * ↑b = -- ↑(gcd b (a % b)) set r : Nat := a % b set q : Nat := a / b set s : Int := gcd_c1 b r set t : Int := gcd_c2 b r --Goal : t * ↑a + (s - t * ↑q) * ↑b = ↑(gcd b r) have h2 : r < b := mod_nonzero_lt a h1 have h3 : s * ↑b + t * ↑r = ↑(gcd b r) := ih r h2 b have h4 : b * q + r = a := Nat.div_add_mod a b rewrite [←h3, ←h4] rewrite [Nat.cast_add, Nat.cast_mul] --Goal : t * (↑b * ↑q + ↑r) + (s - t * ↑q) * ↑b = s * ↑b + t * ↑r ring done done #eval gcd_c1 672 161 --Answer: 6 #eval gcd_c2 672 161 --Answer: -25 --Note 6 * 672 - 25 * 161 = 4032 - 4025 = 7 = gcd 672 161 theorem Theorem_7_1_6 {d a b : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ gcd a b := by rewrite [←Int.natCast_dvd_natCast] --Goal : ↑d ∣ ↑(gcd a b) set s : Int := gcd_c1 a b set t : Int := gcd_c2 a b have h3 : s * ↑a + t * ↑b = ↑(gcd a b) := gcd_lin_comb b a rewrite [←h3] --Goal : ↑d ∣ s * ↑a + t * ↑b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 rewrite [h4, h5, Nat.cast_mul, Nat.cast_mul] --Goal : ↑d ∣ s * (↑d * ↑j) + t * (↑d * ↑k) define apply Exists.intro (s * ↑j + t * ↑k) ring done /- Section 7.2 -/ theorem dvd_trans {a b c : Nat} (h1 : a ∣ b) (h2 : b ∣ c) : a ∣ c := by define at h1; define at h2; define obtain (m : Nat) (h3 : b = a * m) from h1 obtain (n : Nat) (h4 : c = b * n) from h2 rewrite [h3, mul_assoc] at h4 apply Exists.intro (m * n) show c = a * (m * n) from h4 done lemma exists_prime_factor : ∀ (n : Nat), 2 ≤ n → ∃ (p : Nat), prime_factor p n := by by_strong_induc fix n : Nat assume ih : ∀ n_1 < n, 2 ≤ n_1 → ∃ (p : Nat), prime_factor p n_1 assume h1 : 2 ≤ n by_cases h2 : prime n · -- Case 1. h2 : prime n apply Exists.intro n define --Goal : prime n ∧ n ∣ n show prime n ∧ n ∣ n from And.intro h2 (dvd_self n) done · -- Case 2. h2 : ¬prime n define at h2 --h2 : ¬(2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n) demorgan at h2 disj_syll h2 h1 obtain (a : Nat) (h3 : ∃ (b : Nat), a * b = n ∧ a < n ∧ b < n) from h2 obtain (b : Nat) (h4 : a * b = n ∧ a < n ∧ b < n) from h3 have h5 : 2 ≤ a := by by_contra h6 have h7 : a ≤ 1 := by linarith have h8 : n ≤ b := calc n _ = a * b := h4.left.symm _ ≤ 1 * b := by rel [h7] _ = b := by ring linarith --n ≤ b contradicts b < n done have h6 : ∃ (p : Nat), prime_factor p a := ih a h4.right.left h5 obtain (p : Nat) (h7 : prime_factor p a) from h6 apply Exists.intro p define --Goal : prime p ∧ p ∣ n define at h7 --h7 : prime p ∧ p ∣ a apply And.intro h7.left have h8 : a ∣ n := by apply Exists.intro b show n = a * b from (h4.left).symm done show p ∣ n from dvd_trans h7.right h8 done done lemma exists_least_prime_factor {n : Nat} (h : 2 ≤ n) : ∃ (p : Nat), prime_factor p n ∧ ∀ (q : Nat), prime_factor q n → p ≤ q := by set S : Set Nat := {p : Nat | prime_factor p n} have h2 : ∃ (p : Nat), p ∈ S := exists_prime_factor n h show ∃ (p : Nat), prime_factor p n ∧ ∀ (q : Nat), prime_factor q n → p ≤ q from well_ord_princ S h2 done lemma all_prime_nil : all_prime [] := by define --Goal : ∀ p ∈ [], prime p fix p : Nat contrapos --Goal : ¬prime p → p ∉ [] assume h1 : ¬prime p show p ∉ [] from List.not_mem_nil p done lemma all_prime_cons (n : Nat) (L : List Nat) : all_prime (n :: L) ↔ prime n ∧ all_prime L := by apply Iff.intro · -- (→) assume h1 : all_prime (n :: L) --Goal : prime n ∧ all_prime L define at h1 --h1 : ∀ p ∈ n :: L, prime p apply And.intro (h1 n (List.mem_cons_self n L)) define --Goal : ∀ p ∈ L, prime p fix p : Nat assume h2 : p ∈ L show prime p from h1 p (List.mem_cons_of_mem n h2) done · -- (←) assume h1 : prime n ∧ all_prime L --Goal : all_prime (n :: l) define : all_prime L at h1 define fix p : Nat assume h2 : p ∈ n :: L rewrite [List.mem_cons] at h2 --h2 : p = n ∨ p ∈ L by_cases on h2 · -- Case 1. h2 : p = n rewrite [h2] show prime n from h1.left done · -- Case 2. h2 : p ∈ L show prime p from h1.right p h2 done done done lemma nondec_nil : nondec [] := by define --Goal : True trivial --trivial proves some obviously true statements, such as True done lemma nondec_cons (n : Nat) (L : List Nat) : nondec (n :: L) ↔ (∀ m ∈ L, n ≤ m) ∧ nondec L := by rfl
lemma prod_nil : prod [] = 1
HTPI.prod_nil
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 364, "tokenPositionInFile": 10934, "theoremPositionInFile": 47 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by rfl", "proofType": "tactic", "proofLengthLines": 0, "proofLengthTokens": 9 }
htpi
/- Copyright 2023 Daniel J. Velleman -/ import HTPILib.Chap6 namespace HTPI /- Definitions -/ lemma mod_succ_lt (a n : Nat) : a % (n + 1) < n + 1 := by have h : n + 1 > 0 := Nat.succ_pos n show a % (n + 1) < n + 1 from Nat.mod_lt a h done def gcd (a b : Nat) : Nat := match b with | 0 => a | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd (n + 1) (a % (n + 1)) termination_by b mutual def gcd_c1 (a b : Nat) : Int := match b with | 0 => 1 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c2 (n + 1) (a % (n + 1)) --Corresponds to s = t' termination_by b def gcd_c2 (a b : Nat) : Int := match b with | 0 => 0 | n + 1 => have : a % (n + 1) < n + 1 := mod_succ_lt a n gcd_c1 (n + 1) (a % (n + 1)) - (gcd_c2 (n + 1) (a % (n + 1))) * ↑(a / (n + 1)) --Corresponds to t = s' - t'q termination_by b end def prime (n : Nat) : Prop := 2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n def prime_factor (p n : Nat) : Prop := prime p ∧ p ∣ n def all_prime (l : List Nat) : Prop := ∀ p ∈ l, prime p def nondec (l : List Nat) : Prop := match l with | [] => True --Of course, True is a proposition that is always true | n :: L => (∀ m ∈ L, n ≤ m) ∧ nondec L def nondec_prime_list (l : List Nat) : Prop := all_prime l ∧ nondec l def prod (l : List Nat) : Nat := match l with | [] => 1 | n :: L => n * (prod L) def prime_factorization (n : Nat) (l : List Nat) : Prop := nondec_prime_list l ∧ prod l = n def rel_prime (a b : Nat) : Prop := gcd a b = 1 def congr_mod (m : Nat) (a b : Int) : Prop := (↑m : Int) ∣ (a - b) def cc (m : Nat) (a : Int) : ZMod m := (↑a : ZMod m) notation:50 a " ≡ " b " (MOD " m ")" => congr_mod m a b notation:max "["a"]_"m:max => cc m a def invertible {m : Nat} (X : ZMod m) : Prop := ∃ (Y : ZMod m), X * Y = [1]_m def num_rp_below (m k : Nat) : Nat := match k with | 0 => 0 | j + 1 => if gcd m j = 1 then (num_rp_below m j) + 1 else num_rp_below m j def phi (m : Nat) : Nat := num_rp_below m m def prod_seq {m : Nat} (j k : Nat) (f : Nat → ZMod m) : ZMod m := match j with | 0 => [1]_m | n + 1 => prod_seq n k f * f (k + n) def maps_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i < n, g i < n def one_one_below (n : Nat) (g : Nat → Nat) : Prop := ∀ i1 < n, ∀ i2 < n, g i1 = g i2 → i1 = i2 def onto_below (n : Nat) (g : Nat → Nat) : Prop := ∀ k < n, ∃ i < n, g i = k def perm_below (n : Nat) (g : Nat → Nat) : Prop := maps_below n g ∧ one_one_below n g ∧ onto_below n g def inv_mod (m a : Nat) : Nat := Int.toNat ((gcd_c2 m a) % m) def swap (u v i : Nat) : Nat := if i = u then v else if i = v then u else i namespace Euler --For definitions specific to Euler's theorem def F (m i : Nat) : ZMod m := if gcd m i = 1 then [i]_m else [1]_m def G (m a i : Nat) : Nat := (a * i) % m def Ginv (m a i : Nat) : Nat := G m (inv_mod m a) i end Euler /- Section 7.1 -/ theorem dvd_mod_of_dvd_a_b {a b d : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ (a % b) := by set q : Nat := a / b have h3 : b * q + a % b = a := Nat.div_add_mod a b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 define --Goal : ∃ (c : Nat), a % b = d * c apply Exists.intro (j - k * q) show a % b = d * (j - k * q) from calc a % b _ = b * q + a % b - b * q := (Nat.add_sub_cancel_left _ _).symm _ = a - b * q := by rw [h3] _ = d * j - d * (k * q) := by rw [h4, h5, mul_assoc] _ = d * (j - k * q) := (Nat.mul_sub_left_distrib _ _ _).symm done theorem dvd_a_of_dvd_b_mod {a b d : Nat} (h1 : d ∣ b) (h2 : d ∣ (a % b)) : d ∣ a := sorry #eval gcd 672 161 --Answer: 7 lemma gcd_base (a : Nat) : gcd a 0 = a := by rfl lemma gcd_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd a b = gcd b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] --Goal : gcd a (n + 1) = gcd (n + 1) (a % (n + 1)) rfl done lemma mod_nonzero_lt (a : Nat) {b : Nat} (h : b ≠ 0) : a % b < b := by have h1 : b > 0 := Nat.pos_of_ne_zero h show a % b < b from Nat.mod_lt a h1 done lemma dvd_self (n : Nat) : n ∣ n := by apply Exists.intro 1 ring done theorem gcd_dvd : ∀ (b a : Nat), (gcd a b) ∣ a ∧ (gcd a b) ∣ b := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd a b_1) ∣ a ∧ (gcd a b_1) ∣ b_1 fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_base] --Goal: a ∣ a ∧ a ∣ 0 apply And.intro (dvd_self a) define apply Exists.intro 0 rfl done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_nonzero a h1] --Goal : gcd b (a % b) ∣ a ∧ gcd b (a % b) ∣ b have h2 : a % b < b := mod_nonzero_lt a h1 have h3 : (gcd b (a % b)) ∣ b ∧ (gcd b (a % b)) ∣ (a % b) := ih (a % b) h2 b apply And.intro _ h3.left show (gcd b (a % b)) ∣ a from dvd_a_of_dvd_b_mod h3.left h3.right done done theorem gcd_dvd_left (a b : Nat) : (gcd a b) ∣ a := (gcd_dvd b a).left theorem gcd_dvd_right (a b : Nat) : (gcd a b) ∣ b := (gcd_dvd b a).right lemma gcd_c1_base (a : Nat) : gcd_c1 a 0 = 1 := by rfl lemma gcd_c1_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c1 a b = gcd_c2 b (a % b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done lemma gcd_c2_base (a : Nat) : gcd_c2 a 0 = 0 := by rfl lemma gcd_c2_nonzero (a : Nat) {b : Nat} (h : b ≠ 0) : gcd_c2 a b = gcd_c1 b (a % b) - (gcd_c2 b (a % b)) * ↑(a / b) := by obtain (n : Nat) (h2 : b = n + 1) from exists_eq_add_one_of_ne_zero h rewrite [h2] rfl done theorem gcd_lin_comb : ∀ (b a : Nat), (gcd_c1 a b) * ↑a + (gcd_c2 a b) * ↑b = ↑(gcd a b) := by by_strong_induc fix b : Nat assume ih : ∀ b_1 < b, ∀ (a : Nat), (gcd_c1 a b_1) * ↑a + (gcd_c2 a b_1) * ↑b_1 = ↑(gcd a b_1) fix a : Nat by_cases h1 : b = 0 · -- Case 1. h1 : b = 0 rewrite [h1, gcd_c1_base, gcd_c2_base, gcd_base] --Goal : 1 * ↑a + 0 * ↑0 = ↑a ring done · -- Case 2. h1 : b ≠ 0 rewrite [gcd_c1_nonzero a h1, gcd_c2_nonzero a h1, gcd_nonzero a h1] --Goal : gcd_c2 b (a % b) * ↑a + -- (gcd_c1 b (a % b) - gcd_c2 b (a % b) * ↑(a / b)) * ↑b = -- ↑(gcd b (a % b)) set r : Nat := a % b set q : Nat := a / b set s : Int := gcd_c1 b r set t : Int := gcd_c2 b r --Goal : t * ↑a + (s - t * ↑q) * ↑b = ↑(gcd b r) have h2 : r < b := mod_nonzero_lt a h1 have h3 : s * ↑b + t * ↑r = ↑(gcd b r) := ih r h2 b have h4 : b * q + r = a := Nat.div_add_mod a b rewrite [←h3, ←h4] rewrite [Nat.cast_add, Nat.cast_mul] --Goal : t * (↑b * ↑q + ↑r) + (s - t * ↑q) * ↑b = s * ↑b + t * ↑r ring done done #eval gcd_c1 672 161 --Answer: 6 #eval gcd_c2 672 161 --Answer: -25 --Note 6 * 672 - 25 * 161 = 4032 - 4025 = 7 = gcd 672 161 theorem Theorem_7_1_6 {d a b : Nat} (h1 : d ∣ a) (h2 : d ∣ b) : d ∣ gcd a b := by rewrite [←Int.natCast_dvd_natCast] --Goal : ↑d ∣ ↑(gcd a b) set s : Int := gcd_c1 a b set t : Int := gcd_c2 a b have h3 : s * ↑a + t * ↑b = ↑(gcd a b) := gcd_lin_comb b a rewrite [←h3] --Goal : ↑d ∣ s * ↑a + t * ↑b obtain (j : Nat) (h4 : a = d * j) from h1 obtain (k : Nat) (h5 : b = d * k) from h2 rewrite [h4, h5, Nat.cast_mul, Nat.cast_mul] --Goal : ↑d ∣ s * (↑d * ↑j) + t * (↑d * ↑k) define apply Exists.intro (s * ↑j + t * ↑k) ring done /- Section 7.2 -/ theorem dvd_trans {a b c : Nat} (h1 : a ∣ b) (h2 : b ∣ c) : a ∣ c := by define at h1; define at h2; define obtain (m : Nat) (h3 : b = a * m) from h1 obtain (n : Nat) (h4 : c = b * n) from h2 rewrite [h3, mul_assoc] at h4 apply Exists.intro (m * n) show c = a * (m * n) from h4 done lemma exists_prime_factor : ∀ (n : Nat), 2 ≤ n → ∃ (p : Nat), prime_factor p n := by by_strong_induc fix n : Nat assume ih : ∀ n_1 < n, 2 ≤ n_1 → ∃ (p : Nat), prime_factor p n_1 assume h1 : 2 ≤ n by_cases h2 : prime n · -- Case 1. h2 : prime n apply Exists.intro n define --Goal : prime n ∧ n ∣ n show prime n ∧ n ∣ n from And.intro h2 (dvd_self n) done · -- Case 2. h2 : ¬prime n define at h2 --h2 : ¬(2 ≤ n ∧ ¬∃ (a b : Nat), a * b = n ∧ a < n ∧ b < n) demorgan at h2 disj_syll h2 h1 obtain (a : Nat) (h3 : ∃ (b : Nat), a * b = n ∧ a < n ∧ b < n) from h2 obtain (b : Nat) (h4 : a * b = n ∧ a < n ∧ b < n) from h3 have h5 : 2 ≤ a := by by_contra h6 have h7 : a ≤ 1 := by linarith have h8 : n ≤ b := calc n _ = a * b := h4.left.symm _ ≤ 1 * b := by rel [h7] _ = b := by ring linarith --n ≤ b contradicts b < n done have h6 : ∃ (p : Nat), prime_factor p a := ih a h4.right.left h5 obtain (p : Nat) (h7 : prime_factor p a) from h6 apply Exists.intro p define --Goal : prime p ∧ p ∣ n define at h7 --h7 : prime p ∧ p ∣ a apply And.intro h7.left have h8 : a ∣ n := by apply Exists.intro b show n = a * b from (h4.left).symm done show p ∣ n from dvd_trans h7.right h8 done done lemma exists_least_prime_factor {n : Nat} (h : 2 ≤ n) : ∃ (p : Nat), prime_factor p n ∧ ∀ (q : Nat), prime_factor q n → p ≤ q := by set S : Set Nat := {p : Nat | prime_factor p n} have h2 : ∃ (p : Nat), p ∈ S := exists_prime_factor n h show ∃ (p : Nat), prime_factor p n ∧ ∀ (q : Nat), prime_factor q n → p ≤ q from well_ord_princ S h2 done lemma all_prime_nil : all_prime [] := by define --Goal : ∀ p ∈ [], prime p fix p : Nat contrapos --Goal : ¬prime p → p ∉ [] assume h1 : ¬prime p show p ∉ [] from List.not_mem_nil p done lemma all_prime_cons (n : Nat) (L : List Nat) : all_prime (n :: L) ↔ prime n ∧ all_prime L := by apply Iff.intro · -- (→) assume h1 : all_prime (n :: L) --Goal : prime n ∧ all_prime L define at h1 --h1 : ∀ p ∈ n :: L, prime p apply And.intro (h1 n (List.mem_cons_self n L)) define --Goal : ∀ p ∈ L, prime p fix p : Nat assume h2 : p ∈ L show prime p from h1 p (List.mem_cons_of_mem n h2) done · -- (←) assume h1 : prime n ∧ all_prime L --Goal : all_prime (n :: l) define : all_prime L at h1 define fix p : Nat assume h2 : p ∈ n :: L rewrite [List.mem_cons] at h2 --h2 : p = n ∨ p ∈ L by_cases on h2 · -- Case 1. h2 : p = n rewrite [h2] show prime n from h1.left done · -- Case 2. h2 : p ∈ L show prime p from h1.right p h2 done done done lemma nondec_nil : nondec [] := by define --Goal : True trivial --trivial proves some obviously true statements, such as True done lemma nondec_cons (n : Nat) (L : List Nat) : nondec (n :: L) ↔ (∀ m ∈ L, n ≤ m) ∧ nondec L := by rfl lemma prod_nil : prod [] = 1 := by rfl
lemma prod_cons : prod (n :: L) = n * (prod L)
HTPI.prod_cons
null
null
htpi/HTPILib/Chap7.lean
{ "lineInFile": 366, "tokenPositionInFile": 10974, "theoremPositionInFile": 48 }
{ "inFilePremises": true, "repositoryPremises": true }
{ "hasProof": true, "proof": ":= by rfl", "proofType": "tactic", "proofLengthLines": 0, "proofLengthTokens": 9 }
htpi

Example Entry

An entry in the miniCTX dataset consists of the theorem statement, preceding file contents, and metadata information. For example, given the following theorem s_eq_pow_two in context:

import Mathlib.Data.Real.Basic

/-!
# Square function
We define the squaring function `s : ℝ → ℝ` to be `s x := x * x`.
-/

def s (x : ℝ) : ℝ := x * x

lemma s_eq_pow_two {x : ℝ} : s x = x ^ 2 := by
  rw [s, pow_two]

The problem is formatted in JSON as follows:

{
  "srcContext": "import Mathlib.Data.Real.Basic\n\n/-!\n# Square function\nWe define the squaring function `s : ℝ → ℝ` to be `s x := x * x`.\n-/\n\ndef s (x : ℝ) : ℝ := x * x\n\n",
  "theoremStatement": "lemma s_eq_pow_two {x : ℝ} : s x = x ^ 2",
  "theoremName": "s_eq_pow_two",
  "fileCreated": "(git commit)",
  "theoremCreated": "(git commit)",
  "file": "(file name)",
  "positionMetadata": {
    "lineInFile": 10,
    "tokenPositionInFile": 152,
    "theoremPositionInFile": 1
  },
  "dependencyMetadata": {
    "inFilePremises": true,
    "repositoryPremises": false
  },
  "proofMetadata": {
    "hasProof": true,
    "proof": "by\n  rw [s, pow_two]",
    "proofType": "tactic",
    "proofLengthLines": 2,
    "proofLengthTokens": 20
  }
}

Description of Each Entry

  • srcContext: The context of the source file preceding the theorem, including imports and relevant definitions. This provides necessary background to understand the theorem.
  • theoremStatement: The statement of the theorem being proved.
  • theoremName: The name of the theorem.
  • fileCreated: The git commit hash indicating when the file was created.
  • theoremCreated: The git commit hash indicating when the theorem was added.
  • file: The name of the file containing the theorem.
  • positionMetadata:
    • lineInFile: The line number in the file where the theorem is located.
    • tokenPositionInFile: The number of tokens in the file before the theorem starts.
    • theoremPositionInFile: The number of premises (definitions, theorems) before the theorem in the file.
  • dependencyMetadata:
    • inFilePremises: Indicates whether the theorem uses definitions or lemmas defined in the same file.
    • repositoryPremises: Indicates whether the theorem uses definitions or lemmas defined in another file within the same repository.
  • proofMetadata:
    • hasProof: Indicates whether the theorem has a proof.
    • proof: The proof of the theorem.
    • proofType: The type of proof, term proof or tactic proof.
    • proofLengthLines: The length of the proof in lines.
    • proofLengthTokens: The length of the proof in tokens.

In addition to individual entries, we also provide the link and git commit version of each split for evaluation:

Downloads last month
0
Edit dataset card

Collection including l3lab/miniCTX