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

Commit 5219ca1

Browse files
committed
doc(data/nat/modeq): add module docstring and lemma (#2528)
I add a simple docstrong and also a lemma which I found useful for a codewars kata.
1 parent ba4dc1a commit 5219ca1

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/data/nat/modeq.lean

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@
22
Copyright (c) 2017 Mario Carneiro. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Mario Carneiro
5-
6-
Modular equality relation.
75
-/
86
import data.int.gcd
97
import tactic.abel
108
import data.list.rotate
9+
/-
10+
# Congruences modulo a natural number
11+
12+
This file defines the equivalence relation `a ≡ b [MOD n]` on the natural numbers,
13+
and proves basic properties about it such as the Chinese Remainder Theorem
14+
`modeq_and_modeq_iff_modeq_mul`.
15+
16+
## Notations
1117
18+
`a ≡ b [MOD n]` is notation for `modeq n a b`, which is defined to mean `a % n = b % n`.
19+
20+
## Tags
21+
22+
modeq, congruence, mod, MOD, modulo
23+
-/
1224
namespace nat
1325

1426
/-- Modular equality. `modeq n a b`, or `a ≡ b [MOD n]`, means
@@ -37,6 +49,10 @@ by rw [modeq, eq_comm, ← int.coe_nat_inj', int.coe_nat_mod, int.coe_nat_mod,
3749
theorem modeq_of_dvd : (n:ℤ) ∣ b - a → a ≡ b [MOD n] := modeq_iff_dvd.2
3850
theorem dvd_of_modeq : a ≡ b [MOD n] → (n:ℤ) ∣ b - a := modeq_iff_dvd.1
3951

52+
/-- A variant of `modeq_iff_dvd` with `nat` divisibility -/
53+
theorem modeq_iff_dvd' (h : a ≤ b) : a ≡ b [MOD n] ↔ n ∣ b - a :=
54+
by rw [modeq_iff_dvd, ←int.coe_nat_dvd, int.coe_nat_sub h]
55+
4056
theorem mod_modeq (a n) : a % n ≡ a [MOD n] := nat.mod_mod _ _
4157

4258
theorem modeq_of_dvd_of_modeq (d : m ∣ n) (h : a ≡ b [MOD n]) : a ≡ b [MOD m] :=
@@ -80,6 +96,7 @@ by rw [modeq_iff_dvd] at *; exact dvd.trans (dvd_mul_left (n : ℤ) (m : ℤ)) h
8096
theorem modeq_of_modeq_mul_right (m : ℕ) : a ≡ b [MOD n * m] → a ≡ b [MOD n] :=
8197
mul_comm m n ▸ modeq_of_modeq_mul_left _
8298

99+
/-- The natural number less than `n*m` congruent to `a` mod `n` and `b` mod `m` -/
83100
def chinese_remainder (co : coprime n m) (a b : ℕ) : {k // k ≡ a [MOD n] ∧ k ≡ b [MOD m]} :=
84101
let (c, d) := xgcd n m in int.to_nat ((b * c * n + a * d * m) % (n * m)), begin
85102
rw xgcd_val, dsimp [chinese_remainder._match_1],

0 commit comments

Comments
 (0)