@@ -4,6 +4,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
4
4
Authors: Chris Hughes
5
5
-/
6
6
import algebra.big_operators.basic
7
+ import data.nat.prime
7
8
import data.zmod.basic
8
9
9
10
/-!
@@ -12,7 +13,8 @@ import data.zmod.basic
12
13
This file defines [ Euler's totient function ] [https://en.wikipedia.org/wiki/Euler's_totient_function ]
13
14
`nat.totient n` which counts the number of naturals less than `n` that are coprime with `n`.
14
15
We prove the divisor sum formula, namely that `n` equals `φ` summed over the divisors of `n`. See
15
- `sum_totient`.
16
+ `sum_totient`. We also prove two lemmas to help compute totients, namely `totient_mul` and
17
+ `totient_prime_pow`.
16
18
-/
17
19
18
20
open finset
@@ -28,6 +30,11 @@ localized "notation `φ` := nat.totient" in nat
28
30
29
31
@[simp] theorem totient_zero : φ 0 = 0 := rfl
30
32
33
+ @[simp] theorem totient_one : φ 1 = 1 :=
34
+ by simp [totient]
35
+
36
+ lemma totient_eq_card_coprime (n : ℕ) : φ n = ((range n).filter (nat.coprime n)).card := rfl
37
+
31
38
lemma totient_le (n : ℕ) : φ n ≤ n :=
32
39
calc totient n ≤ (range n).card : card_filter_le _ _
33
40
... = n : card_range _
@@ -120,4 +127,52 @@ calc ∑ m in (range n.succ).filter (∣ n), φ m
120
127
(gcd_dvd_left _ _))), gcd_dvd_left _ _⟩, mem_filter.2 ⟨hm, rfl⟩⟩⟩))
121
128
... = n : card_range _
122
129
130
+ /-- When `p` is prime, then the totient of `p ^ (n + 1)` is `p ^ n * (p - 1)` -/
131
+ lemma totient_prime_pow_succ {p : ℕ} (hp : p.prime) (n : ℕ) :
132
+ φ (p ^ (n + 1 )) = p ^ n * (p - 1 ) :=
133
+ calc φ (p ^ (n + 1 ))
134
+ = ((range (p ^ (n + 1 ))).filter (coprime (p ^ (n + 1 )))).card :
135
+ totient_eq_card_coprime _
136
+ ... = (range (p ^ (n + 1 )) \ ((range (p ^ n)).image (* p))).card :
137
+ congr_arg card begin
138
+ rw [sdiff_eq_filter],
139
+ apply filter_congr,
140
+ simp only [mem_range, mem_filter, coprime_pow_left_iff n.succ_pos,
141
+ mem_image, not_exists, hp.coprime_iff_not_dvd],
142
+ intros a ha,
143
+ split,
144
+ { rintros hap b _ rfl,
145
+ exact hap (dvd_mul_left _ _) },
146
+ { rintros h ⟨b, rfl⟩,
147
+ rw [pow_succ] at ha,
148
+ exact h b (lt_of_mul_lt_mul_left ha (zero_le _)) (mul_comm _ _) }
149
+ end
150
+ ... = _ :
151
+ have h1 : set.inj_on (* p) (range (p ^ n)),
152
+ from λ x _ y _, (nat.mul_left_inj hp.pos).1 ,
153
+ have h2 : (range (p ^ n)).image (* p) ⊆ range (p ^ (n + 1 )),
154
+ from λ a, begin
155
+ simp only [mem_image, mem_range, exists_imp_distrib],
156
+ rintros b h rfl,
157
+ rw [pow_succ'],
158
+ exact (mul_lt_mul_right hp.pos).2 h
159
+ end ,
160
+ begin
161
+ rw [card_sdiff h2, card_image_of_inj_on h1, card_range,
162
+ card_range, ← one_mul (p ^ n), pow_succ, ← nat.mul_sub_right_distrib,
163
+ one_mul, mul_comm]
164
+ end
165
+
166
+ /-- When `p` is prime, then the totient of `p ^ ` is `p ^ (n - 1) * (p - 1)` -/
167
+ lemma totient_prime_pow {p : ℕ} (hp : p.prime) {n : ℕ} (hn : 0 < n) :
168
+ φ (p ^ n) = p ^ (n - 1 ) * (p - 1 ) :=
169
+ by rcases exists_eq_succ_of_ne_zero (pos_iff_ne_zero.1 hn) with ⟨m, rfl⟩;
170
+ exact totient_prime_pow_succ hp _
171
+
172
+ lemma totient_prime {p : ℕ} (hp : p.prime) : φ p = p - 1 :=
173
+ by rw [← pow_one p, totient_prime_pow hp]; simp
174
+
175
+ @[simp] lemma totient_two : φ 2 = 1 :=
176
+ (totient_prime prime_two).trans (by norm_num)
177
+
123
178
end nat
0 commit comments