Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit fc7daa3

Browse files
feat(data/nat/parity): addition/subtraction of even/odd nats (#5934)
Added various theorems pertaining to the addition and subtraction of even and odd natural numbers.
1 parent 893ce8b commit fc7daa3

File tree

2 files changed

+64
-13
lines changed

2 files changed

+64
-13
lines changed

src/analysis/convex/specific_functions.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ begin
3535
apply convex_on_univ_of_deriv2_nonneg differentiable_pow,
3636
{ simp only [deriv_pow', differentiable.mul, differentiable_const, differentiable_pow] },
3737
{ intro x,
38-
rcases nat.even.sub hn (nat.even_bit0 1) with ⟨k, hk⟩,
38+
rcases nat.even.sub_even hn (nat.even_bit0 1) with ⟨k, hk⟩,
3939
simp only [iter_deriv_pow, finset.prod_range_succ, finset.prod_range_zero, nat.sub_zero,
4040
mul_one, hk, pow_mul', pow_two],
4141
exact mul_nonneg (nat.cast_nonneg _) (mul_self_nonneg _) }

src/data/nat/parity.lean

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/-
22
Copyright (c) 2019 Jeremy Avigad. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
4-
Authors: Jeremy Avigad
4+
Authors: Jeremy Avigad, Benjamin Davidson
55
66
The `even` and `odd` predicates on the natural numbers.
77
-/
@@ -19,8 +19,8 @@ theorem even_iff {n : ℕ} : even n ↔ n % 2 = 0 :=
1919
⟨λ ⟨m, hm⟩, by simp [hm], λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by simp [h])⟩⟩
2020

2121
theorem odd_iff {n : ℕ} : odd n ↔ n % 2 = 1 :=
22-
⟨λ ⟨m, hm⟩, by { rw [hm, add_mod], norm_num },
23-
λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by { rw h, abel })⟩⟩
22+
⟨λ ⟨m, hm⟩, by norm_num [hm, add_mod],
23+
λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by rw [h, add_comm])⟩⟩
2424

2525
lemma not_even_iff {n : ℕ} : ¬ even n ↔ n % 2 = 1 :=
2626
by rw [even_iff, mod_two_ne_zero]
@@ -86,32 +86,46 @@ begin
8686
exact @modeq.modeq_add _ _ 1 _ 1 h₁ h₂
8787
end
8888

89-
theorem even.add {m n : ℕ} (hm : even m) (hn : even n) : even (m + n) :=
89+
theorem even.add_even {m n : ℕ} (hm : even m) (hn : even n) : even (m + n) :=
9090
even_add.2 $ by simp only [*]
9191

92+
theorem even_add' {m n : ℕ} : even (m + n) ↔ (odd m ↔ odd n) :=
93+
by rw [even_add, even_iff_not_odd, even_iff_not_odd, not_iff_not]
94+
95+
theorem odd.add_odd {m n : ℕ} (hm : odd m) (hn : odd n) : even (m + n) :=
96+
even_add'.2 $ by simp only [*]
97+
9298
@[simp] theorem not_even_bit1 (n : ℕ) : ¬ even (bit1 n) :=
9399
by simp [bit1] with parity_simps
94100

95-
lemma two_not_dvd_two_mul_add_one (a : ℕ) : ¬(22 * a + 1) :=
101+
lemma two_not_dvd_two_mul_add_one (n : ℕ) : ¬(22 * n + 1) :=
96102
begin
97-
convert not_even_bit1 a,
98-
exact two_mul a,
103+
convert not_even_bit1 n,
104+
exact two_mul n,
99105
end
100106

101-
lemma two_not_dvd_two_mul_sub_one : Π {a : ℕ} (w : 0 < a), ¬(22 * a - 1)
102-
| (a+1) _ := two_not_dvd_two_mul_add_one a
107+
lemma two_not_dvd_two_mul_sub_one : Π {n : ℕ} (w : 0 < n), ¬(22 * n - 1) | (n + 1) _ :=
108+
two_not_dvd_two_mul_add_one n
103109

104110
@[parity_simps] theorem even_sub {m n : ℕ} (h : n ≤ m) : even (m - n) ↔ (even m ↔ even n) :=
105111
begin
106112
conv { to_rhs, rw [←nat.sub_add_cancel h, even_add] },
107113
by_cases h : even n; simp [h]
108114
end
109115

110-
theorem even.sub {m n : ℕ} (hm : even m) (hn : even n) : even (m - n) :=
116+
theorem even.sub_even {m n : ℕ} (hm : even m) (hn : even n) : even (m - n) :=
111117
(le_total n m).elim
112118
(λ h, by simp only [even_sub h, *])
113119
(λ h, by simp only [sub_eq_zero_of_le h, even_zero])
114120

121+
theorem even_sub' {m n : ℕ} (h : n ≤ m) : even (m - n) ↔ (odd m ↔ odd n) :=
122+
by rw [even_sub h, even_iff_not_odd, even_iff_not_odd, not_iff_not]
123+
124+
theorem odd.sub_odd {m n : ℕ} (hm : odd m) (hn : odd n) : even (m - n) :=
125+
(le_total n m).elim
126+
(λ h, by simp only [even_sub' h, *])
127+
(λ h, by simp only [sub_eq_zero_of_le h, even_zero])
128+
115129
@[parity_simps] theorem even_succ {n : ℕ} : even (succ n) ↔ ¬ even n :=
116130
by rw [succ_eq_add_one, even_add]; simp [not_even_one]
117131

@@ -130,10 +144,47 @@ if and only if `m` is even and `n` is positive. -/
130144
@[parity_simps] theorem even_pow {m n : ℕ} : even (m^n) ↔ even m ∧ n ≠ 0 :=
131145
by { induction n with n ih; simp [*, pow_succ', even_mul], tauto }
132146

133-
lemma even_div {a b : ℕ} : even (a / b) ↔ a % (2 * b) / b = 0 :=
147+
lemma even_div {m n : ℕ} : even (m / n) ↔ m % (2 * n) / n = 0 :=
134148
by rw [even_iff_two_dvd, dvd_iff_mod_eq_zero, nat.div_mod_eq_mod_mul_div, mul_comm]
135149

136-
theorem neg_one_pow_eq_one_iff_even {α : Type*} [ring α] {n : ℕ} (h1 : (-1 : α) ≠ 1):
150+
@[parity_simps] theorem odd_add {m n : ℕ} : odd (m + n) ↔ (odd m ↔ even n) :=
151+
begin
152+
by_contra hnot,
153+
rw [not_iff, ← even_iff_not_odd, even_add, odd_iff_not_even, ← not_iff] at hnot,
154+
exact (iff_not_self _).mp hnot,
155+
end
156+
157+
theorem odd.add_even {m n : ℕ} (hm : odd m) (hn : even n) : odd (m + n) :=
158+
odd_add.2 $ by simp only [*]
159+
160+
theorem odd_add' {m n : ℕ} : odd (m + n) ↔ (odd n ↔ even m) :=
161+
by rw [add_comm, odd_add]
162+
163+
theorem even.add_odd {m n : ℕ} (hm : even m) (hn : odd n) : odd (m + n) :=
164+
odd_add'.2 $ by simp only [*]
165+
166+
@[parity_simps] theorem odd_sub {m n : ℕ} (h : n ≤ m) : odd (m - n) ↔ (odd m ↔ even n) :=
167+
begin
168+
by_contra hnot,
169+
rw [not_iff, ← even_iff_not_odd, even_sub h, odd_iff_not_even, ← not_iff] at hnot,
170+
exact (iff_not_self _).mp hnot,
171+
end
172+
173+
theorem odd.sub_even {m n : ℕ} (h : n ≤ m) (hm : odd m) (hn : even n) : odd (m - n) :=
174+
(odd_sub h).mpr (iff_of_true hm hn)
175+
176+
theorem odd_sub' {m n : ℕ} (h : n ≤ m) : odd (m - n) ↔ (odd n ↔ even m) :=
177+
begin
178+
by_contra hnot,
179+
rw [not_iff, ← even_iff_not_odd, even_sub h, odd_iff_not_even, ← not_iff,
180+
@iff.comm _ (even n)] at hnot,
181+
exact (iff_not_self _).mp hnot,
182+
end
183+
184+
theorem even.sub_odd {m n : ℕ} (h : n ≤ m) (hm : even m) (hn : odd n) : odd (m - n) :=
185+
(odd_sub' h).mpr (iff_of_true hn hm)
186+
187+
theorem neg_one_pow_eq_one_iff_even {α : Type*} [ring α] {n : ℕ} (h1 : (-1 : α) ≠ 1) :
137188
(-1 : α) ^ n = 1 ↔ even n :=
138189
⟨λ h, n.mod_two_eq_zero_or_one.elim (dvd_iff_mod_eq_zero _ _).2
139190
(λ hn, by rw [neg_one_pow_eq_pow_mod_two, hn, pow_one] at h; exact (h1 h).elim),

0 commit comments

Comments
 (0)