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

Commit 894fb0c

Browse files
committed
feat(data/nat/totient): totient_prime_pow (#8353)
Co-authored-by: Chris Hughes <33847686+ChrisHughes24@users.noreply.github.com>
1 parent 7249afb commit 894fb0c

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/data/nat/gcd.lean

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,18 @@ theorem coprime.pow_right {m k : ℕ} (n : ℕ) (H1 : coprime k m) : coprime k (
342342
theorem coprime.pow {k l : ℕ} (m n : ℕ) (H1 : coprime k l) : coprime (k ^ m) (l ^ n) :=
343343
(H1.pow_left _).pow_right _
344344

345+
lemma coprime_pow_left_iff {n : ℕ} (hn : 0 < n) (a b : ℕ) :
346+
nat.coprime (a ^ n) b ↔ nat.coprime a b :=
347+
begin
348+
obtain ⟨n, rfl⟩ := exists_eq_succ_of_ne_zero hn.ne',
349+
rw [pow_succ, nat.coprime_mul_iff_left],
350+
exact ⟨and.left, λ hab, ⟨hab, hab.pow_left _⟩⟩
351+
end
352+
353+
lemma coprime_pow_right_iff {n : ℕ} (hn : 0 < n) (a b : ℕ) :
354+
nat.coprime a (b ^ n) ↔ nat.coprime a b :=
355+
by rw [nat.coprime_comm, coprime_pow_left_iff hn, nat.coprime_comm]
356+
345357
theorem coprime.eq_one_of_dvd {k m : ℕ} (H : coprime k m) (d : k ∣ m) : k = 1 :=
346358
by rw [← H.gcd_eq_one, gcd_eq_left d]
347359

src/data/nat/totient.lean

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Chris Hughes
55
-/
66
import algebra.big_operators.basic
7+
import data.nat.prime
78
import data.zmod.basic
89

910
/-!
@@ -12,7 +13,8 @@ import data.zmod.basic
1213
This file defines [Euler's totient function][https://en.wikipedia.org/wiki/Euler's_totient_function]
1314
`nat.totient n` which counts the number of naturals less than `n` that are coprime with `n`.
1415
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`.
1618
-/
1719

1820
open finset
@@ -28,6 +30,11 @@ localized "notation `φ` := nat.totient" in nat
2830

2931
@[simp] theorem totient_zero : φ 0 = 0 := rfl
3032

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+
3138
lemma totient_le (n : ℕ) : φ n ≤ n :=
3239
calc totient n ≤ (range n).card : card_filter_le _ _
3340
... = n : card_range _
@@ -120,4 +127,52 @@ calc ∑ m in (range n.succ).filter (∣ n), φ m
120127
(gcd_dvd_left _ _))), gcd_dvd_left _ _⟩, mem_filter.2 ⟨hm, rfl⟩⟩⟩))
121128
... = n : card_range _
122129

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+
123178
end nat

0 commit comments

Comments
 (0)