doc-next-gen

Init.Data.Int.DivMod.Lemmas

Module docstring

{"# Further lemmas about integer division, now that omega is available. ","### dvd ","### *div zero ","### preliminaries for div equivalences ","### div equivalences ","### mod zero ","### mod definitions ","### mod equivalences ","### / ediv ","### emod ","### properties of / and % ","### / and ordering ","### tdiv ","There are no lemmas * add_mul_tdiv_right : c ≠ 0 → (a + b * c).tdiv c = a.tdiv c + b * add_mul_tdiv_left : b ≠ 0 → (a + b * c).tdiv b = a.tdiv b + c * (similarly mul_add_tdiv_right, mul_add_tdiv_left) * add_tdiv_of_dvd_right : c ∣ b → (a + b).tdiv c = a.tdiv c + b.tdiv c * add_tdiv_of_dvd_left : c ∣ a → (a + b).tdiv c = a.tdiv c + b.tdiv c because these statements are all incorrect, and require awkward conditional off-by-one corrections. ","### tmod ","properties of tdiv and tmod ","### tdiv and ordering ","### fdiv ","One could prove the following, but as the statements are quite awkward, so far it doesn't seem worthwhile. ``` theorem natAbs_fdiv {a b : Int} (h : b ≠ 0) : natAbs (a.fdiv b) = a.natAbs / b.natAbs + if a.sign = b.sign ∨ b ∣ a then 0 else 1 := ...

theorem sign_fdiv (a b : Int) : sign (a.fdiv b) = if a.sign = b.sign ∧ natAbs a < natAbs b then 0 else sign a * sign b := ... `` ","### fmod ","### properties offdivandfmod","###fdivand ordering ","### bmod ","Helper theorems fordvd` simproc "}

Int.exists_add_of_le theorem
{a b : Int} (h : a ≤ b) : ∃ (c : Nat), b = a + c
Full source
protected theorem exists_add_of_le {a b : Int} (h : a ≤ b) : ∃ (c : Nat), b = a + c :=
  ⟨(b - a).toNat, by omega⟩
Existence of Natural Difference for Integer Ordering: $a \leq b \implies \exists c \in \mathbb{N}, b = a + c$
Informal description
For any integers $a$ and $b$ such that $a \leq b$, there exists a natural number $c$ such that $b = a + c$.
Int.dvd_antisymm theorem
{a b : Int} (H1 : 0 ≤ a) (H2 : 0 ≤ b) : a ∣ b → b ∣ a → a = b
Full source
theorem dvd_antisymm {a b : Int} (H1 : 0 ≤ a) (H2 : 0 ≤ b) : a ∣ bb ∣ a → a = b := by
  rw [← natAbs_of_nonneg H1, ← natAbs_of_nonneg H2]
  rw [ofNat_dvd, ofNat_dvd, ofNat_inj]
  apply Nat.dvd_antisymm
Antisymmetry of Divisibility for Nonnegative Integers: $a \mid b \land b \mid a \implies a = b$
Informal description
For any nonnegative integers $a$ and $b$ (i.e., $0 \leq a$ and $0 \leq b$), if $a$ divides $b$ and $b$ divides $a$, then $a = b$.
Int.dvd_add theorem
: ∀ {a b c : Int}, a ∣ b → a ∣ c → a ∣ b + c
Full source
protected theorem dvd_add : ∀ {a b c : Int}, a ∣ ba ∣ ca ∣ b + c
  | _, _, _, ⟨d, rfl⟩, ⟨e, rfl⟩ => ⟨d + e, by rw [Int.mul_add]⟩
Divisibility is Additive: $a \mid b \land a \mid c \implies a \mid (b + c)$
Informal description
For any integers $a$, $b$, and $c$, if $a$ divides $b$ and $a$ divides $c$, then $a$ divides the sum $b + c$.
Int.dvd_sub theorem
: ∀ {a b c : Int}, a ∣ b → a ∣ c → a ∣ b - c
Full source
protected theorem dvd_sub : ∀ {a b c : Int}, a ∣ ba ∣ ca ∣ b - c
  | _, _, _, ⟨d, rfl⟩, ⟨e, rfl⟩ => ⟨d - e, by rw [Int.mul_sub]⟩
Divisibility is Subtractive: $a \mid b \land a \mid c \implies a \mid (b - c)$
Informal description
For any integers $a$, $b$, and $c$, if $a$ divides $b$ and $a$ divides $c$, then $a$ divides the difference $b - c$.
Int.dvd_add_left theorem
{a b c : Int} (H : a ∣ c) : a ∣ b + c ↔ a ∣ b
Full source
protected theorem dvd_add_left {a b c : Int} (H : a ∣ c) : a ∣ b + ca ∣ b + c ↔ a ∣ b :=
  ⟨fun h => by have := Int.dvd_sub h H; rwa [Int.add_sub_cancel] at this, (Int.dvd_add · H)⟩
Divisibility of Sum by Left Term: $a \mid c \implies (a \mid (b + c) \leftrightarrow a \mid b)$
Informal description
For any integers $a$, $b$, and $c$, if $a$ divides $c$, then $a$ divides $b + c$ if and only if $a$ divides $b$.
Int.dvd_iff_dvd_of_dvd_sub theorem
{a b c : Int} (H : a ∣ b - c) : a ∣ b ↔ a ∣ c
Full source
protected theorem dvd_iff_dvd_of_dvd_sub {a b c : Int} (H : a ∣ b - c) : a ∣ ba ∣ b ↔ a ∣ c :=
  ⟨fun h => Int.sub_sub_self b c ▸ Int.dvd_sub h H,
   fun h => Int.sub_add_cancel b c ▸ Int.dvd_add H h⟩
Divisibility Transfer Through Subtraction: $a \mid (b - c) \implies (a \mid b \leftrightarrow a \mid c)$
Informal description
For any integers $a$, $b$, and $c$, if $a$ divides the difference $b - c$, then $a$ divides $b$ if and only if $a$ divides $c$.
Int.dvd_iff_dvd_of_dvd_add theorem
{a b c : Int} (H : a ∣ b + c) : a ∣ b ↔ a ∣ c
Full source
protected theorem dvd_iff_dvd_of_dvd_add {a b c : Int} (H : a ∣ b + c) : a ∣ ba ∣ b ↔ a ∣ c := by
  rw [← Int.sub_neg] at H; rw [Int.dvd_iff_dvd_of_dvd_sub H, Int.dvd_neg]
Divisibility Transfer Through Addition: $a \mid (b + c) \implies (a \mid b \leftrightarrow a \mid c)$
Informal description
For any integers $a$, $b$, and $c$, if $a$ divides the sum $b + c$, then $a$ divides $b$ if and only if $a$ divides $c$.
Int.le_of_dvd theorem
{a b : Int} (bpos : 0 < b) (H : a ∣ b) : a ≤ b
Full source
theorem le_of_dvd {a b : Int} (bpos : 0 < b) (H : a ∣ b) : a ≤ b :=
  match a, b, eq_succ_of_zero_lt bpos, H with
  | ofNat _, _, ⟨n, rfl⟩, H => ofNat_le.2 <| Nat.le_of_dvd n.succ_pos <| ofNat_dvd.1 H
  | -[_+1], _, ⟨_, rfl⟩, _ => Int.le_trans (Int.le_of_lt <| negSucc_lt_zero _) (ofNat_zero_le _)
Divisor of Positive Integer is Less Than or Equal to It
Informal description
For any integers $a$ and $b$ with $b > 0$, if $a$ divides $b$, then $a \leq b$.
Int.natAbs_dvd theorem
{a b : Int} : (a.natAbs : Int) ∣ b ↔ a ∣ b
Full source
theorem natAbs_dvd {a b : Int} : (a.natAbs : Int) ∣ b(a.natAbs : Int) ∣ b ↔ a ∣ b :=
  match natAbs_eq a with
  | .inl e => by rw [← e]
  | .inr e => by rw [← Int.neg_dvd, ← e]
Divisibility via Natural Absolute Value: $\text{natAbs}(a) \mid b \leftrightarrow a \mid b$
Informal description
For any integers $a$ and $b$, the natural absolute value of $a$ (as an integer) divides $b$ if and only if $a$ divides $b$. In other words, $\text{natAbs}(a) \mid b \leftrightarrow a \mid b$.
Int.dvd_natAbs theorem
{a b : Int} : a ∣ b.natAbs ↔ a ∣ b
Full source
theorem dvd_natAbs {a b : Int} : a ∣ b.natAbsa ∣ b.natAbs ↔ a ∣ b :=
  match natAbs_eq b with
  | .inl e => by rw [← e]
  | .inr e => by rw [← Int.dvd_neg, ← e]
Divisibility of Integer Absolute Value: $a \mid |b| \leftrightarrow a \mid b$
Informal description
For any integers $a$ and $b$, the integer $a$ divides the natural absolute value of $b$ if and only if $a$ divides $b$ itself. In other words, $a \mid |b| \leftrightarrow a \mid b$.
Int.natAbs_dvd_self theorem
{a : Int} : (a.natAbs : Int) ∣ a
Full source
theorem natAbs_dvd_self {a : Int} : (a.natAbs : Int) ∣ a := by
  rw [Int.natAbs_dvd]
  exact Int.dvd_refl a
Divisibility of Integer by Its Absolute Value: $|a| \mid a$
Informal description
For any integer $a$, the natural absolute value of $a$ (as an integer) divides $a$ itself, i.e., $|a| \mid a$.
Int.dvd_natAbs_self theorem
{a : Int} : a ∣ (a.natAbs : Int)
Full source
theorem dvd_natAbs_self {a : Int} : a ∣ (a.natAbs : Int) := by
  rw [Int.dvd_natAbs]
  exact Int.dvd_refl a
Integer Divisibility of Own Absolute Value: $a \mid |a|$
Informal description
For any integer $a$, $a$ divides its own absolute value (as an integer). That is, $a \mid |a|$ where $|a|$ is the natural absolute value of $a$ cast back to an integer.
Int.ofNat_dvd_right theorem
{n : Nat} {z : Int} : z ∣ (↑n : Int) ↔ z.natAbs ∣ n
Full source
theorem ofNat_dvd_right {n : Nat} {z : Int} : z ∣ (↑n : Int)z ∣ (↑n : Int) ↔ z.natAbs ∣ n := by
  rw [← natAbs_dvd_natAbs, natAbs_ofNat]
Divisibility of Cast Natural Number via Absolute Value: $z \mid n \leftrightarrow |z| \mid n$
Informal description
For any natural number $n$ and any integer $z$, $z$ divides the integer $n$ (obtained by casting $n$ to $\mathbb{Z}$) if and only if the absolute value of $z$ (as a natural number) divides $n$.
Int.negSucc_dvd theorem
{a : Nat} {b : Int} : -[a+1] ∣ b ↔ ((a + 1 : Nat) : Int) ∣ b
Full source
@[simp] theorem negSucc_dvd {a : Nat} {b : Int} : -[a+1]-[a+1] ∣ b-[a+1] ∣ b ↔ ((a + 1 : Nat) : Int) ∣ b := by
  rw [← natAbs_dvd]
  norm_cast
Divisibility of Negative Successor: $- (a + 1) \mid b \leftrightarrow (a + 1) \mid b$
Informal description
For any natural number $a$ and integer $b$, the negative successor $- (a + 1)$ divides $b$ if and only if the positive successor $(a + 1)$ (as an integer) divides $b$. In other words: $$ -[a+1] \mid b \leftrightarrow (a + 1) \mid b $$
Int.dvd_negSucc theorem
{a : Int} {b : Nat} : a ∣ -[b+1] ↔ a ∣ ((b + 1 : Nat) : Int)
Full source
@[simp] theorem dvd_negSucc {a : Int} {b : Nat} : a ∣ -[b+1]a ∣ -[b+1] ↔ a ∣ ((b + 1 : Nat) : Int) := by
  rw [← dvd_natAbs]
  norm_cast
Divisibility of Negative Successor: $a \mid -[b+1] \leftrightarrow a \mid (b+1)$
Informal description
For any integer $a$ and natural number $b$, the integer $a$ divides the negative successor $-([b+1])$ if and only if $a$ divides the positive successor $(b + 1)$ in the integers.
Int.decidableDvd instance
: DecidableRel (α := Int) (· ∣ ·)
Full source
instance decidableDvd : DecidableRel (α := Int) (· ∣ ·) := fun _ _ =>
  decidable_of_decidable_of_iff (dvd_iff_emod_eq_zero ..).symm
Decidability of Integer Divisibility
Informal description
The divisibility relation $\mid$ on the integers $\mathbb{Z}$ is decidable. That is, for any two integers $a$ and $b$, it is constructively decidable whether $a$ divides $b$.
Int.mul_dvd_mul_iff_left theorem
{a b c : Int} (h : a ≠ 0) : (a * b) ∣ (a * c) ↔ b ∣ c
Full source
protected theorem mul_dvd_mul_iff_left {a b c : Int} (h : a ≠ 0) : (a * b) ∣ (a * c)(a * b) ∣ (a * c) ↔ b ∣ c :=
  ⟨by rintro ⟨d, h'⟩; exact ⟨d, by rw [Int.mul_assoc] at h'; exact (mul_eq_mul_left_iff h).mp h'⟩,
    by rintro ⟨d, rfl⟩; exact ⟨d, by simp [Int.mul_assoc]⟩⟩
Left Cancellation Property for Divisibility in Integer Multiplication: $(a \cdot b) \mid (a \cdot c) \leftrightarrow b \mid c$
Informal description
For any integers $a$, $b$, and $c$ with $a \neq 0$, the product $a \cdot b$ divides $a \cdot c$ if and only if $b$ divides $c$.
Int.mul_dvd_mul_iff_right theorem
{a b c : Int} (h : a ≠ 0) : (b * a) ∣ (c * a) ↔ b ∣ c
Full source
protected theorem mul_dvd_mul_iff_right {a b c : Int} (h : a ≠ 0) : (b * a) ∣ (c * a)(b * a) ∣ (c * a) ↔ b ∣ c := by
  rw [Int.mul_comm b a, Int.mul_comm c a]
  exact Int.mul_dvd_mul_iff_left h
Right Cancellation Property for Divisibility in Integer Multiplication: $(b \cdot a) \mid (c \cdot a) \leftrightarrow b \mid c$
Informal description
For any integers $a$, $b$, and $c$ with $a \neq 0$, the product $b \cdot a$ divides $c \cdot a$ if and only if $b$ divides $c$.
Int.zero_tdiv theorem
: ∀ b : Int, tdiv 0 b = 0
Full source
@[simp] protected theorem zero_tdiv : ∀ b : Int, tdiv 0 b = 0
  | ofNat _ => show ofNat _ = _ by simp
  | -[_+1] => show -ofNat _ = _ by simp
Truncating Division of Zero by Any Integer is Zero
Informal description
For any integer $b$, the truncating division of $0$ by $b$ equals $0$, i.e., $0 \ \mathrm{tdiv} \ b = 0$.
Int.tdiv_zero theorem
: ∀ a : Int, tdiv a 0 = 0
Full source
@[simp] protected theorem tdiv_zero : ∀ a : Int, tdiv a 0 = 0
  | ofNat _ => show ofNat _ = _ by simp
  | -[_+1] => by simp [tdiv]
Truncating Division by Zero Yields Zero: $a \ \text{tdiv} \ 0 = 0$
Informal description
For any integer $a$, the truncating division of $a$ by zero equals zero, i.e., $a \ \text{tdiv} \ 0 = 0$.
Int.zero_fdiv theorem
(b : Int) : fdiv 0 b = 0
Full source
@[simp] theorem zero_fdiv (b : Int) : fdiv 0 b = 0 := by cases b <;> rfl
Floor Division of Zero by Any Integer is Zero
Informal description
For any integer $b$, the flooring division of $0$ by $b$ equals $0$, i.e., $\lfloor 0 / b \rfloor = 0$.
Int.fdiv_zero theorem
: ∀ a : Int, fdiv a 0 = 0
Full source
@[simp] protected theorem fdiv_zero : ∀ a : Int, fdiv a 0 = 0
  | 0      => rfl
  | succ _ => by simp [fdiv]
  | -[_+1] => rfl
Floor Division by Zero Yields Zero: $\lfloor a / 0 \rfloor = 0$
Informal description
For any integer $a$, the flooring division of $a$ by zero equals zero, i.e., $\lfloor a / 0 \rfloor = 0$.
Int.negSucc_emod_ofNat_succ_eq_zero_iff theorem
{a b : Nat} : -[a+1] % (b + 1 : Int) = 0 ↔ (a + 1) % (b + 1) = 0
Full source
theorem negSucc_emod_ofNat_succ_eq_zero_iff {a b : Nat} :
    -[a+1]-[a+1] % (b + 1 : Int) = 0 ↔ (a + 1) % (b + 1) = 0 := by
  rw [← Int.natCast_one, ← Int.natCast_add]
  change Int.emodInt.emod _ _ = 0 ↔ _
  rw [emod, natAbs_ofNat]
  simp only [Nat.succ_eq_add_one, subNat_eq_zero_iff, Nat.add_right_cancel_iff]
  rw [eq_comm]
  apply Nat.succ_mod_succ_eq_zero_iff.symm
Euclidean Modulus of Negative Integer Equals Zero iff Modulus of Positive Counterpart Equals Zero
Informal description
For any natural numbers $a$ and $b$, the Euclidean modulus of the negative integer $- (a + 1)$ by the positive integer $b + 1$ equals zero if and only if the modulus of $a + 1$ by $b + 1$ equals zero, i.e., $$- (a + 1) \bmod (b + 1) = 0 \leftrightarrow (a + 1) \bmod (b + 1) = 0.$$
Int.negSucc_emod_negSucc_eq_zero_iff theorem
{a b : Nat} : -[a+1] % -[b+1] = 0 ↔ (a + 1) % (b + 1) = 0
Full source
theorem negSucc_emod_negSucc_eq_zero_iff {a b : Nat} :
    -[a+1]-[a+1] % -[b+1] = 0 ↔ (a + 1) % (b + 1) = 0 := by
  change Int.emodInt.emod _ _ = 0 ↔ _
  rw [emod, natAbs_negSucc]
  simp only [Nat.succ_eq_add_one, subNat_eq_zero_iff, Nat.add_right_cancel_iff]
  rw [eq_comm]
  apply Nat.succ_mod_succ_eq_zero_iff.symm
Modulus Equivalence for Negative Successor Integers: $(-a-1) \bmod (-b-1) = 0 \leftrightarrow (a+1) \bmod (b+1) = 0$
Informal description
For any natural numbers $a$ and $b$, the Euclidean modulus of the negative successor integer $-a-1$ by $-b-1$ equals zero if and only if the modulus of $a+1$ by $b+1$ equals zero, i.e., $$(-a-1) \bmod (-b-1) = 0 \leftrightarrow (a+1) \bmod (b+1) = 0.$$
Int.tdiv_eq_ediv_of_nonneg theorem
: ∀ {a b : Int}, 0 ≤ a → a.tdiv b = a / b
Full source
theorem tdiv_eq_ediv_of_nonneg : ∀ {a b : Int}, 0 ≤ a → a.tdiv b = a / b
  | 0, _, _
  | _, 0, _ => by simp
  | succ _, succ _, _ => rfl
  | succ _, -[_+1], _ => rfl
Equality of Truncating and Euclidean Division for Non-Negative Integers: $a \geq 0 \Rightarrow a \ \mathrm{tdiv} \ b = a / b$
Informal description
For any integers $a$ and $b$ with $a \geq 0$, the truncating division of $a$ by $b$ equals the Euclidean division of $a$ by $b$, i.e., $a \ \mathrm{tdiv} \ b = a / b$.
Int.tdiv_eq_ediv theorem
{a b : Int} : a.tdiv b = a / b + if 0 ≤ a ∨ b ∣ a then 0 else sign b
Full source
theorem tdiv_eq_ediv {a b : Int} :
    a.tdiv b = a / b + if 0 ≤ a ∨ b ∣ a then 0 else sign b := by
  simp only [dvd_iff_emod_eq_zero]
  match a, b with
  | ofNat a, ofNat b => simp [tdiv_eq_ediv_of_nonneg]
  | ofNat a, -[b+1] => simp [tdiv_eq_ediv_of_nonneg]
  | -[a+1], 0 => simp
  | -[a+1], ofNat (succ b) =>
    simp only [tdiv, Nat.succ_eq_add_one, ofNat_eq_coe, Int.natCast_add, cast_ofNat_Int,
      negSucc_not_nonneg, sign_of_add_one]
    simp only [negSucc_emod_ofNat_succ_eq_zero_iff]
    norm_cast
    simp only [subNat_eq_zero_iff, Nat.succ_eq_add_one, sign_negSucc, Int.sub_neg, false_or]
    split <;> rename_i h
    · rw [Int.add_zero, neg_ofNat_eq_negSucc_iff]
      exact Nat.succ_div_of_mod_eq_zero h
    · rw [neg_ofNat_eq_negSucc_add_one_iff]
      exact Nat.succ_div_of_mod_ne_zero h
  | -[a+1], -[b+1] =>
    simp only [tdiv, ofNat_eq_coe, negSucc_not_nonneg, false_or, sign_negSucc]
    norm_cast
    simp only [negSucc_ediv_negSucc]
    rw [Int.natCast_add, Int.natCast_one]
    simp only [negSucc_emod_negSucc_eq_zero_iff]
    split <;> rename_i h
    · norm_cast
      exact Nat.succ_div_of_mod_eq_zero h
    · rw [Int.add_neg_eq_sub, Int.add_sub_cancel]
      norm_cast
      exact Nat.succ_div_of_mod_ne_zero h
Relation between Truncating and Euclidean Division: $a \ \mathrm{tdiv} \ b = a / b + \text{adjustment}$
Informal description
For any integers $a$ and $b$, the truncating division $a \ \mathrm{tdiv} \ b$ equals the Euclidean division $a / b$ plus an adjustment term: if $a$ is nonnegative or $b$ divides $a$, the adjustment is $0$; otherwise, it is the sign of $b$ (i.e., $1$ if $b>0$, $-1$ if $b<0$). In symbols: $$ a \ \mathrm{tdiv} \ b = a / b + \begin{cases} 0 & \text{if } 0 \leq a \text{ or } b \mid a \\ \mathrm{sign}(b) & \text{otherwise} \end{cases} $$
Int.ediv_eq_tdiv theorem
{a b : Int} : a / b = a.tdiv b - if 0 ≤ a ∨ b ∣ a then 0 else sign b
Full source
theorem ediv_eq_tdiv {a b : Int} :
    a / b = a.tdiv b - if 0 ≤ a ∨ b ∣ a then 0 else sign b := by
  simp [tdiv_eq_ediv]
Relation between Euclidean and Truncating Division: $a / b = a \ \mathrm{tdiv} \ b - \text{adjustment}$
Informal description
For any integers $a$ and $b$, the Euclidean division $a / b$ equals the truncating division $a \ \mathrm{tdiv} \ b$ minus an adjustment term: if $a$ is nonnegative or $b$ divides $a$, the adjustment is $0$; otherwise, it is the sign of $b$ (i.e., $1$ if $b>0$, $-1$ if $b<0$). In symbols: $$ a / b = a \ \mathrm{tdiv} \ b - \begin{cases} 0 & \text{if } 0 \leq a \text{ or } b \mid a \\ \mathrm{sign}(b) & \text{otherwise} \end{cases} $$
Int.fdiv_eq_ediv_of_nonneg theorem
: ∀ (a : Int) {b : Int}, 0 ≤ b → fdiv a b = a / b
Full source
theorem fdiv_eq_ediv_of_nonneg : ∀ (a : Int) {b : Int}, 0 ≤ b → fdiv a b = a / b
  | 0, _, _ | -[_+1], 0, _ => by simp
  | succ _, ofNat _, _ | -[_+1], succ _, _ => rfl
Floor Division Equals Euclidean Division for Nonnegative Divisors: $\lfloor a / b \rfloor = a / b$ when $b \geq 0$
Informal description
For any integer $a$ and any nonnegative integer $b$ (i.e., $0 \leq b$), the flooring division of $a$ by $b$ equals the Euclidean division of $a$ by $b$, i.e., $\lfloor a / b \rfloor = a / b$.
Int.fdiv_eq_ediv theorem
{a b : Int} : a.fdiv b = a / b - if 0 ≤ b ∨ b ∣ a then 0 else 1
Full source
theorem fdiv_eq_ediv {a b : Int} :
    a.fdiv b = a / b - if 0 ≤ b ∨ b ∣ a then 0 else 1 := by
  match a, b with
  | ofNat a, ofNat b => simp [fdiv_eq_ediv_of_nonneg]
  | -[a+1], ofNat b => simp [fdiv_eq_ediv_of_nonneg]
  | 0, -[b+1] => simp
  | ofNat (a + 1), -[b+1] =>
    simp only [fdiv, ofNat_ediv_negSucc, negSucc_not_nonneg, negSucc_dvd, false_or]
    simp only [ofNat_eq_coe, ofNat_dvd]
    norm_cast
    rw [Nat.succ_div, negSucc_eq]
    split <;> rename_i h
    · simp
    · simp [Int.neg_add]
      norm_cast
  | -[a+1], -[b+1] =>
    simp only [fdiv, ofNat_eq_coe, negSucc_ediv_negSucc, negSucc_not_nonneg, dvd_negSucc, negSucc_dvd,
      false_or]
    norm_cast
    rw [Int.natCast_add, Int.natCast_one, Nat.succ_div]
    split <;> simp
Floor Division Equals Euclidean Division Minus One for Negative Non-Divisors: $\lfloor a / b \rfloor = a / b - \mathbb{1}_{b < 0 \land b \nmid a}$
Informal description
For any integers $a$ and $b$, the flooring division $\lfloor a / b \rfloor$ equals the Euclidean division $a / b$ minus $1$ if $b$ is negative and does not divide $a$, and equals $a / b$ otherwise. In other words: \[ \lfloor a / b \rfloor = \begin{cases} a / b & \text{if } b \geq 0 \text{ or } b \mid a \\ a / b - 1 & \text{otherwise} \end{cases} \]
Int.ediv_eq_fdiv theorem
{a b : Int} : a / b = a.fdiv b + if 0 ≤ b ∨ b ∣ a then 0 else 1
Full source
theorem ediv_eq_fdiv {a b : Int} :
    a / b = a.fdiv b + if 0 ≤ b ∨ b ∣ a then 0 else 1 := by
  simp [fdiv_eq_ediv]
Euclidean Division Equals Floor Division Plus One for Negative Non-Divisors: $a / b = \lfloor a / b \rfloor + \mathbb{1}_{b < 0 \land b \nmid a}$
Informal description
For any integers $a$ and $b$, the Euclidean division $a / b$ equals the flooring division $\lfloor a / b \rfloor$ plus $1$ if $b$ is negative and does not divide $a$, and equals $\lfloor a / b \rfloor$ otherwise. In other words: \[ a / b = \begin{cases} \lfloor a / b \rfloor & \text{if } b \geq 0 \text{ or } b \mid a \\ \lfloor a / b \rfloor + 1 & \text{otherwise} \end{cases} \]
Int.fdiv_eq_tdiv_of_nonneg theorem
{a b : Int} (Ha : 0 ≤ a) (Hb : 0 ≤ b) : fdiv a b = tdiv a b
Full source
theorem fdiv_eq_tdiv_of_nonneg {a b : Int} (Ha : 0 ≤ a) (Hb : 0 ≤ b) : fdiv a b = tdiv a b :=
  tdiv_eq_ediv_of_nonneg Ha ▸ fdiv_eq_ediv_of_nonneg _ Hb
Equality of Flooring and Truncating Division for Nonnegative Integers: $\lfloor a / b \rfloor = \text{trunc}(a / b)$ when $a, b \geq 0$
Informal description
For any integers $a$ and $b$ such that $a \geq 0$ and $b \geq 0$, the flooring division of $a$ by $b$ equals the truncating division of $a$ by $b$, i.e., $\lfloor a / b \rfloor = \text{trunc}(a / b)$.
Int.fdiv_eq_tdiv theorem
{a b : Int} : a.fdiv b = a.tdiv b - if b ∣ a then 0 else if 0 ≤ a then if 0 ≤ b then 0 else 1 else if 0 ≤ b then b.sign else 1 + b.sign
Full source
theorem fdiv_eq_tdiv {a b : Int} :
    a.fdiv b = a.tdiv b -
      if b ∣ a then 0
      else
        if 0 ≤ a then
          if 0 ≤ b then 0
          else 1
        else
          if 0 ≤ b then b.sign
          else 1 + b.sign := by
  rw [fdiv_eq_ediv, tdiv_eq_ediv]
  by_cases h : b ∣ a <;> simp [h] <;> omega
Relation between Flooring and Truncating Division on Integers: $\lfloor a / b \rfloor = \text{trunc}(a / b) - \text{adjustment}$
Informal description
For any integers $a$ and $b$, the flooring division $\lfloor a / b \rfloor$ equals the truncating division $\text{trunc}(a / b)$ minus an adjustment term: \[ \lfloor a / b \rfloor = \text{trunc}(a / b) - \begin{cases} 0 & \text{if } b \mid a \\ 0 & \text{if } a \geq 0 \text{ and } b \geq 0 \\ 1 & \text{if } a \geq 0 \text{ and } b < 0 \\ \text{sign}(b) & \text{if } a < 0 \text{ and } b \geq 0 \\ 1 + \text{sign}(b) & \text{if } a < 0 \text{ and } b < 0 \end{cases} \]
Int.tdiv_eq_fdiv theorem
{a b : Int} : a.tdiv b = a.fdiv b + if b ∣ a then 0 else if 0 ≤ a then if 0 ≤ b then 0 else 1 else if 0 ≤ b then b.sign else 1 + b.sign
Full source
theorem tdiv_eq_fdiv {a b : Int} :
    a.tdiv b = a.fdiv b +
      if b ∣ a then 0
      else
        if 0 ≤ a then
          if 0 ≤ b then 0
          else 1
        else
          if 0 ≤ b then b.sign
          else 1 + b.sign := by
  rw [fdiv_eq_tdiv]
  omega
Relation between Truncating and Flooring Division on Integers: $\text{trunc}(a / b) = \lfloor a / b \rfloor + \text{adjustment}$
Informal description
For any integers $a$ and $b$, the truncating division $\text{trunc}(a / b)$ equals the flooring division $\lfloor a / b \rfloor$ plus an adjustment term: \[ \text{trunc}(a / b) = \lfloor a / b \rfloor + \begin{cases} 0 & \text{if } b \mid a \\ 0 & \text{if } a \geq 0 \text{ and } b \geq 0 \\ 1 & \text{if } a \geq 0 \text{ and } b < 0 \\ \text{sign}(b) & \text{if } a < 0 \text{ and } b \geq 0 \\ 1 + \text{sign}(b) & \text{if } a < 0 \text{ and } b < 0 \end{cases} \]
Int.tdiv_eq_ediv_of_dvd theorem
{a b : Int} (h : b ∣ a) : a.tdiv b = a / b
Full source
theorem tdiv_eq_ediv_of_dvd {a b : Int} (h : b ∣ a) : a.tdiv b = a / b := by
  simp [tdiv_eq_ediv, h]
Truncating Division Equals Euclidean Division for Divisors: $a \ \mathrm{tdiv} \ b = a / b$ when $b \mid a$
Informal description
For any integers $a$ and $b$ such that $b$ divides $a$, the truncating division of $a$ by $b$ equals the Euclidean division of $a$ by $b$, i.e., $a \ \mathrm{tdiv} \ b = a / b$.
Int.fdiv_eq_ediv_of_dvd theorem
{a b : Int} (h : b ∣ a) : a.fdiv b = a / b
Full source
theorem fdiv_eq_ediv_of_dvd {a b : Int} (h : b ∣ a) : a.fdiv b = a / b := by
  simp [fdiv_eq_ediv, h]
Floor Division Equals Euclidean Division for Divisors: $\lfloor a / b \rfloor = a / b$ when $b \mid a$
Informal description
For any integers $a$ and $b$ such that $b$ divides $a$, the flooring division of $a$ by $b$ equals the Euclidean division of $a$ by $b$, i.e., $\lfloor a / b \rfloor = a / b$.
Int.zero_tmod theorem
(b : Int) : tmod 0 b = 0
Full source
@[simp] theorem zero_tmod (b : Int) : tmod 0 b = 0 := by cases b <;> simp [tmod]
Truncating Modulo of Zero: $\operatorname{tmod}(0, b) = 0$
Informal description
For any integer $b$, the truncating modulo operation satisfies $\operatorname{tmod}(0, b) = 0$.
Int.tmod_zero theorem
: ∀ a : Int, tmod a 0 = a
Full source
@[simp] theorem tmod_zero : ∀ a : Int, tmod a 0 = a
  | ofNat _ => congrArg ofNat <| Nat.mod_zero _
  | -[_+1] => congrArg (fun n => -ofNat n) <| Nat.mod_zero _
Truncating Modulo by Zero Identity: $\operatorname{tmod}(a, 0) = a$
Informal description
For any integer $a$, the truncating modulo operation with divisor zero satisfies $\operatorname{tmod}(a, 0) = a$.
Int.zero_fmod theorem
(b : Int) : fmod 0 b = 0
Full source
@[simp] theorem zero_fmod (b : Int) : fmod 0 b = 0 := by cases b <;> rfl
Flooring Modulus of Zero: $\operatorname{fmod}(0, b) = 0$
Informal description
For any integer $b$, the flooring modulus of $0$ and $b$ is $0$, i.e., $\operatorname{fmod}(0, b) = 0$.
Int.fmod_zero theorem
: ∀ a : Int, fmod a 0 = a
Full source
@[simp] theorem fmod_zero : ∀ a : Int, fmod a 0 = a
  | 0 => rfl
  | succ _ => congrArg ofNat <| Nat.mod_zero _
  | -[_+1]  => congrArg negSucc <| Nat.mod_zero _
Flooring Modulus Identity: $\operatorname{fmod}(a, 0) = a$
Informal description
For any integer $a$, the flooring modulus operation satisfies $\operatorname{fmod}(a, 0) = a$.
Int.tmod_add_tdiv theorem
: ∀ a b : Int, tmod a b + b * (a.tdiv b) = a
Full source
theorem tmod_add_tdiv : ∀ a b : Int, tmod a b + b * (a.tdiv b) = a
  | ofNat _, ofNat _ => congrArg ofNat (Nat.mod_add_div ..)
  | ofNat m, -[n+1] => by
    show (m % succ n + -↑(succ n) * -↑(m / succ n) : Int) = m
    rw [Int.neg_mul_neg]; exact congrArg ofNat (Nat.mod_add_div ..)
  | -[m+1], 0 => by
    show -(↑((succ m) % 0) : Int) + 0 * -↑(succ m / 0) = -↑(succ m)
    rw [Nat.mod_zero, Int.zero_mul, Int.add_zero]
  | -[m+1], ofNat n => by
    show -(↑((succ m) % n) : Int) + ↑n * -↑(succ m / n) = -↑(succ m)
    rw [Int.mul_neg, ← Int.neg_add]
    exact congrArg (-ofNat ·) (Nat.mod_add_div ..)
  | -[m+1], -[n+1] => by
    show -(↑(succ m % succ n) : Int) + -↑(succ n) * ↑(succ m / succ n) = -↑(succ m)
    rw [Int.neg_mul, ← Int.neg_add]
    exact congrArg (-ofNat ·) (Nat.mod_add_div ..)
Division-Remainder Decomposition for Truncating Integer Division: $\operatorname{tmod}(a, b) + b \cdot \operatorname{tdiv}(a, b) = a$
Informal description
For any integers $a$ and $b$, the sum of the truncating modulus $\operatorname{tmod}(a, b)$ and the product $b \cdot \operatorname{tdiv}(a, b)$ equals $a$, i.e., \[ \operatorname{tmod}(a, b) + b \cdot \operatorname{tdiv}(a, b) = a. \]
Int.tdiv_add_tmod theorem
(a b : Int) : b * a.tdiv b + tmod a b = a
Full source
theorem tdiv_add_tmod (a b : Int) : b * a.tdiv b + tmod a b = a := by
  rw [Int.add_comm]; apply tmod_add_tdiv ..
Division-Remainder Decomposition for Truncating Integer Division: $b \cdot \lfloor a / b \rfloor + (a \bmod b) = a$
Informal description
For any integers $a$ and $b$, the sum of the product $b \cdot \lfloor a / b \rfloor$ and the remainder $a \bmod b$ equals $a$, i.e., \[ b \cdot \left\lfloor \frac{a}{b} \right\rfloor + (a \bmod b) = a. \]
Int.tmod_add_tdiv' theorem
(m k : Int) : tmod m k + m.tdiv k * k = m
Full source
/-- Variant of `tmod_add_tdiv` with the multiplication written the other way around. -/
theorem tmod_add_tdiv' (m k : Int) : tmod m k + m.tdiv k * k = m := by
  rw [Int.mul_comm]; apply tmod_add_tdiv
Division-Remainder Decomposition for Truncating Integer Division (Multiplicative Variant)
Informal description
For any integers $m$ and $k$, the sum of the truncating modulus $\operatorname{tmod}(m, k)$ and the product $\operatorname{tdiv}(m, k) \cdot k$ equals $m$, i.e., \[ \operatorname{tmod}(m, k) + \operatorname{tdiv}(m, k) \cdot k = m. \]
Int.tdiv_add_tmod' theorem
(m k : Int) : m.tdiv k * k + tmod m k = m
Full source
/-- Variant of `tdiv_add_tmod` with the multiplication written the other way around. -/
theorem tdiv_add_tmod' (m k : Int) : m.tdiv k * k + tmod m k = m := by
  rw [Int.mul_comm]; apply tdiv_add_tmod
Truncating Division-Modulus Decomposition: $\operatorname{tdiv}(m, k) \cdot k + \operatorname{tmod}(m, k) = m$
Informal description
For any integers $m$ and $k$, the sum of the product of the truncating division $\operatorname{tdiv}(m, k)$ and $k$, and the truncating modulus $\operatorname{tmod}(m, k)$, equals $m$. That is, \[ \operatorname{tdiv}(m, k) \cdot k + \operatorname{tmod}(m, k) = m. \]
Int.tmod_def theorem
(a b : Int) : tmod a b = a - b * a.tdiv b
Full source
theorem tmod_def (a b : Int) : tmod a b = a - b * a.tdiv b := by
  rw [← Int.add_sub_cancel (tmod a b), tmod_add_tdiv]
Definition of Truncating Modulus via Division: $\operatorname{tmod}(a, b) = a - b \cdot \operatorname{tdiv}(a, b)$
Informal description
For any integers $a$ and $b$, the truncating modulus $\operatorname{tmod}(a, b)$ satisfies the equation: \[ \operatorname{tmod}(a, b) = a - b \cdot \operatorname{tdiv}(a, b). \]
Int.fmod_add_fdiv theorem
: ∀ a b : Int, a.fmod b + b * a.fdiv b = a
Full source
theorem fmod_add_fdiv : ∀ a b : Int, a.fmod b + b * a.fdiv b = a
  | 0, ofNat _ | 0, -[_+1] => congrArg ofNat <| by simp
  | succ _, ofNat _ => congrArg ofNat <| Nat.mod_add_div ..
  | succ m, -[n+1] => by
    show subNatNat (m % succ n) n + (↑(succ n * (m / succ n)) + n + 1) = (m + 1)
    rw [Int.add_comm _ n, ← Int.add_assoc, ← Int.add_assoc,
      Int.subNatNat_eq_coe, Int.sub_add_cancel]
    exact congrArg (ofNat · + 1) <| Nat.mod_add_div ..
  | -[_+1], 0 => by rw [fmod_zero]; rfl
  | -[m+1], succ n => by
    show subNatNat .. - (↑(succ n * (m / succ n)) + ↑(succ n)) = -↑(succ m)
    rw [Int.subNatNat_eq_coe, ← Int.sub_sub, ← Int.neg_sub, Int.sub_sub, Int.sub_sub_self]
    exact congrArg (-ofNat ·) <| Nat.succ_add .. ▸ Nat.mod_add_div .. ▸ rfl
  | -[m+1], -[n+1] => by
    show -(↑(succ m % succ n) : Int) + -↑(succ n * (succ m / succ n)) = -↑(succ m)
    rw [← Int.neg_add]; exact congrArg (-ofNat ·) <| Nat.mod_add_div ..
Flooring Division-Modulus Decomposition: $\operatorname{fmod}(a, b) + b \cdot \operatorname{fdiv}(a, b) = a$
Informal description
For any integers $a$ and $b$, the flooring modulus $\operatorname{fmod}(a, b)$ plus $b$ multiplied by the flooring division $\operatorname{fdiv}(a, b)$ equals $a$, i.e., \[ \operatorname{fmod}(a, b) + b \cdot \operatorname{fdiv}(a, b) = a. \]
Int.fmod_add_fdiv' theorem
(a b : Int) : a.fmod b + (a.fdiv b) * b = a
Full source
/-- Variant of `fmod_add_fdiv` with the multiplication written the other way around. -/
theorem fmod_add_fdiv' (a b : Int) : a.fmod b + (a.fdiv b) * b = a := by
  rw [Int.mul_comm]; exact fmod_add_fdiv ..
Flooring Division-Modulus Decomposition (Multiplicative Commuted): $\operatorname{fmod}(a, b) + \operatorname{fdiv}(a, b) \cdot b = a$
Informal description
For any integers $a$ and $b$, the flooring modulus $\operatorname{fmod}(a, b)$ plus the flooring division $\operatorname{fdiv}(a, b)$ multiplied by $b$ equals $a$, i.e., \[ \operatorname{fmod}(a, b) + \operatorname{fdiv}(a, b) \cdot b = a. \]
Int.fdiv_add_fmod theorem
(a b : Int) : b * a.fdiv b + a.fmod b = a
Full source
theorem fdiv_add_fmod (a b : Int) : b * a.fdiv b + a.fmod b = a := by
  rw [Int.add_comm]; exact fmod_add_fdiv ..
Flooring Division-Modulus Identity: $b \cdot \lfloor a / b \rfloor + (a \operatorname{fmod} b) = a$
Informal description
For any integers $a$ and $b$, the product $b \cdot \lfloor a / b \rfloor$ plus the remainder $a \operatorname{fmod} b$ equals $a$, i.e., \[ b \cdot \lfloor a / b \rfloor + (a \operatorname{fmod} b) = a. \]
Int.fdiv_add_fmod' theorem
(a b : Int) : (a.fdiv b) * b + a.fmod b = a
Full source
/-- Variant of `fdiv_add_fmod` with the multiplication written the other way around. -/
theorem fdiv_add_fmod' (a b : Int) : (a.fdiv b) * b + a.fmod b = a := by
  rw [Int.mul_comm]; exact fdiv_add_fmod ..
Flooring Division-Modulus Identity (Multiplicative Commuted): $\lfloor a/b \rfloor \cdot b + (a \operatorname{fmod} b) = a$
Informal description
For any integers $a$ and $b$, the flooring division $\lfloor a / b \rfloor$ multiplied by $b$ plus the flooring modulus $(a \operatorname{fmod} b)$ equals $a$, i.e., \[ \lfloor a / b \rfloor \cdot b + (a \operatorname{fmod} b) = a. \]
Int.fmod_def theorem
(a b : Int) : a.fmod b = a - b * a.fdiv b
Full source
theorem fmod_def (a b : Int) : a.fmod b = a - b * a.fdiv b := by
  rw [← Int.add_sub_cancel (a.fmod b), fmod_add_fdiv]
Definition of Flooring Modulus: $\operatorname{fmod}(a,b) = a - b \cdot \lfloor a/b \rfloor$
Informal description
For any integers $a$ and $b$, the flooring modulus satisfies $a \operatorname{fmod} b = a - b \cdot \lfloor a / b \rfloor$.
Int.fmod_eq_emod_of_nonneg theorem
(a : Int) {b : Int} (hb : 0 ≤ b) : fmod a b = a % b
Full source
theorem fmod_eq_emod_of_nonneg (a : Int) {b : Int} (hb : 0 ≤ b) : fmod a b = a % b := by
  simp [fmod_def, emod_def, fdiv_eq_ediv_of_nonneg _ hb]
Flooring Modulus Equals Euclidean Modulus for Nonnegative Divisors: $\operatorname{fmod}(a,b) = a \% b$ when $b \geq 0$
Informal description
For any integer $a$ and any nonnegative integer $b$ (i.e., $b \geq 0$), the flooring modulus of $a$ modulo $b$ equals the Euclidean modulus of $a$ modulo $b$, i.e., $\operatorname{fmod}(a, b) = a \% b$.
Int.fmod_eq_emod theorem
{a b : Int} : fmod a b = a % b + if 0 ≤ b ∨ b ∣ a then 0 else b
Full source
theorem fmod_eq_emod {a b : Int} :
    fmod a b = a % b + if 0 ≤ b ∨ b ∣ a then 0 else b := by
  simp [fmod_def, emod_def, fdiv_eq_ediv]
  split <;> simp [Int.mul_sub]
  omega
Flooring Modulus Equals Euclidean Modulus Plus Adjustment: $\operatorname{fmod}(a,b) = a \% b + b \cdot \mathbb{1}_{b < 0 \land b \nmid a}$
Informal description
For any integers $a$ and $b$, the flooring modulus $\operatorname{fmod}(a, b)$ equals the Euclidean modulus $a \% b$ plus $b$ if $b$ is negative and does not divide $a$, and equals $a \% b$ otherwise. In other words: \[ \operatorname{fmod}(a, b) = \begin{cases} a \% b & \text{if } b \geq 0 \text{ or } b \mid a \\ a \% b + b & \text{otherwise} \end{cases} \]
Int.emod_eq_fmod theorem
{a b : Int} : a % b = fmod a b - if 0 ≤ b ∨ b ∣ a then 0 else b
Full source
theorem emod_eq_fmod {a b : Int} :
    a % b = fmod a b - if 0 ≤ b ∨ b ∣ a then 0 else b := by
  simp [fmod_eq_emod]
Euclidean Modulus Equals Flooring Modulus Minus Adjustment: $a \% b = \operatorname{fmod}(a, b) - b \cdot \mathbb{1}_{b < 0 \land b \nmid a}$
Informal description
For any integers $a$ and $b$, the Euclidean modulus $a \% b$ equals the flooring modulus $\operatorname{fmod}(a, b)$ minus $b$ if $b$ is negative and does not divide $a$, and equals $\operatorname{fmod}(a, b)$ otherwise. In other words: \[ a \% b = \begin{cases} \operatorname{fmod}(a, b) & \text{if } b \geq 0 \text{ or } b \mid a \\ \operatorname{fmod}(a, b) - b & \text{otherwise} \end{cases} \]
Int.tmod_eq_emod_of_nonneg theorem
{a b : Int} (ha : 0 ≤ a) : tmod a b = a % b
Full source
theorem tmod_eq_emod_of_nonneg {a b : Int} (ha : 0 ≤ a) : tmod a b = a % b := by
  simp [emod_def, tmod_def, tdiv_eq_ediv_of_nonneg ha]
Equality of Truncating and Euclidean Modulus for Non-Negative Integers: $\operatorname{tmod}(a, b) = a \% b$ when $a \geq 0$
Informal description
For any integers $a$ and $b$ with $a \geq 0$, the truncating modulus $\operatorname{tmod}(a, b)$ equals the Euclidean modulus $a \% b$.
Int.tmod_eq_emod theorem
{a b : Int} : tmod a b = a % b - if 0 ≤ a ∨ b ∣ a then 0 else b.natAbs
Full source
theorem tmod_eq_emod {a b : Int} :
    tmod a b = a % b - if 0 ≤ a ∨ b ∣ a then 0 else b.natAbs := by
  rw [tmod_def, tdiv_eq_ediv]
  simp only [dvd_iff_emod_eq_zero]
  split
  · simp [emod_def]
  · rw [Int.mul_add, ← Int.sub_sub, emod_def]
    simp
Relation between Truncating and Euclidean Modulus: $\operatorname{tmod}(a, b) = a \% b - \text{adjustment}$
Informal description
For any integers $a$ and $b$, the truncating modulus $\operatorname{tmod}(a, b)$ equals the Euclidean modulus $a \% b$ minus an adjustment term: if $a$ is nonnegative or $b$ divides $a$, the adjustment is $0$; otherwise, it is the absolute value of $b$ (as a natural number). In symbols: $$ \operatorname{tmod}(a, b) = a \% b - \begin{cases} 0 & \text{if } 0 \leq a \text{ or } b \mid a \\ |b| & \text{otherwise} \end{cases} $$
Int.emod_eq_tmod theorem
{a b : Int} : a % b = tmod a b + if 0 ≤ a ∨ b ∣ a then 0 else b.natAbs
Full source
theorem emod_eq_tmod {a b : Int} :
    a % b = tmod a b + if 0 ≤ a ∨ b ∣ a then 0 else b.natAbs := by
  simp [tmod_eq_emod]
Euclidean Modulus Equals Truncating Modulus Plus Adjustment: $a \% b = \operatorname{tmod}(a, b) + \text{adjustment}$
Informal description
For any integers $a$ and $b$, the Euclidean modulus $a \% b$ equals the truncating modulus $\operatorname{tmod}(a, b)$ plus an adjustment term: if $a$ is nonnegative or $b$ divides $a$, the adjustment is $0$; otherwise, it is the absolute value of $b$ (as a natural number). In symbols: $$ a \% b = \operatorname{tmod}(a, b) + \begin{cases} 0 & \text{if } 0 \leq a \text{ or } b \mid a \\ |b| & \text{otherwise} \end{cases} $$
Int.fmod_eq_tmod_of_nonneg theorem
{a b : Int} (ha : 0 ≤ a) (hb : 0 ≤ b) : fmod a b = tmod a b
Full source
theorem fmod_eq_tmod_of_nonneg {a b : Int} (ha : 0 ≤ a) (hb : 0 ≤ b) : fmod a b = tmod a b :=
  tmod_eq_emod_of_nonneg ha ▸ fmod_eq_emod_of_nonneg _ hb
Equality of Flooring and Truncating Modulus for Nonnegative Integers: $\operatorname{fmod}(a, b) = \operatorname{tmod}(a, b)$ when $a, b \geq 0$
Informal description
For any integers $a$ and $b$ such that $a \geq 0$ and $b \geq 0$, the flooring modulus $\operatorname{fmod}(a, b)$ equals the truncating modulus $\operatorname{tmod}(a, b)$.
Int.fmod_eq_tmod theorem
{a b : Int} : fmod a b = tmod a b + if b ∣ a then 0 else if 0 ≤ a then if 0 ≤ b then 0 else b else if 0 ≤ b then b.natAbs else 2 * b.toNat
Full source
theorem fmod_eq_tmod {a b : Int} :
    fmod a b = tmod a b +
      if b ∣ a then 0
      else
        if 0 ≤ a then
          if 0 ≤ b then 0
          else b
        else
          if 0 ≤ b then b.natAbs
          else 2 * b.toNat := by
  simp [fmod_eq_emod, tmod_eq_emod]
  by_cases h : b ∣ a <;> simp [h]
  split <;> split <;> omega
Relation between flooring and truncating modulus: $\operatorname{fmod}(a,b) = \operatorname{tmod}(a,b) + \text{adjustment}$
Informal description
For any integers $a$ and $b$, the flooring modulus $\operatorname{fmod}(a, b)$ equals the truncating modulus $\operatorname{tmod}(a, b)$ plus an adjustment term: \[ \operatorname{fmod}(a, b) = \operatorname{tmod}(a, b) + \begin{cases} 0 & \text{if } b \mid a \\ 0 & \text{if } 0 \leq a \text{ and } 0 \leq b \\ b & \text{if } 0 \leq a \text{ and } b < 0 \\ |b| & \text{if } a < 0 \text{ and } 0 \leq b \\ 2b^+ & \text{otherwise (where } b^+ = \max(b, 0)\text{)} \end{cases} \]
Int.tmod_eq_fmod theorem
{a b : Int} : tmod a b = fmod a b - if b ∣ a then 0 else if 0 ≤ a then if 0 ≤ b then 0 else b else if 0 ≤ b then b.natAbs else 2 * b.toNat
Full source
theorem tmod_eq_fmod {a b : Int} :
    tmod a b = fmod a b -
      if b ∣ a then 0
      else
        if 0 ≤ a then
          if 0 ≤ b then 0
          else b
        else
          if 0 ≤ b then b.natAbs
          else 2 * b.toNat := by
  simp [fmod_eq_tmod]
Relation between Truncating and Flooring Modulus: $\operatorname{tmod}(a,b) = \operatorname{fmod}(a,b) - \text{adjustment}$
Informal description
For any integers $a$ and $b$, the truncating modulus $\operatorname{tmod}(a, b)$ equals the flooring modulus $\operatorname{fmod}(a, b)$ minus an adjustment term: \[ \operatorname{tmod}(a, b) = \operatorname{fmod}(a, b) - \begin{cases} 0 & \text{if } b \mid a \\ 0 & \text{if } 0 \leq a \text{ and } 0 \leq b \\ b & \text{if } 0 \leq a \text{ and } b < 0 \\ |b| & \text{if } a < 0 \text{ and } 0 \leq b \\ 2b^+ & \text{otherwise (where } b^+ = \max(b, 0)\text{)} \end{cases} \]
Int.mul_add_ediv_right theorem
(a c : Int) {b : Int} (H : b ≠ 0) : (a * b + c) / b = a + c / b
Full source
theorem mul_add_ediv_right (a c : Int) {b : Int} (H : b ≠ 0) : (a * b + c) / b = a + c / b := by
  rw [Int.add_comm, add_mul_ediv_right _ _ H, Int.add_comm]
Integer Division Identity: $(a \cdot b + c)/b = a + c/b$ for $b \neq 0$
Informal description
For any integers $a$, $b$, and $c$ with $b \neq 0$, the integer division $(a \cdot b + c) / b$ equals $a + c / b$.
Int.mul_add_ediv_left theorem
(b : Int) {a : Int} (c : Int) (H : a ≠ 0) : (a * b + c) / a = b + c / a
Full source
theorem mul_add_ediv_left (b : Int) {a : Int}
    (c : Int) (H : a ≠ 0) : (a * b + c) / a = b + c / a := by
  rw [Int.add_comm, add_mul_ediv_left _ _ H, Int.add_comm]
Integer Division Identity: $(a \cdot b + c)/a = b + c/a$ for $a \neq 0$
Informal description
For any integers $a$, $b$, and $c$ with $a \neq 0$, the integer division $(a \cdot b + c) / a$ equals $b + c / a$.
Int.sub_mul_ediv_right theorem
(a b : Int) {c : Int} (H : c ≠ 0) : (a - b * c) / c = a / c - b
Full source
theorem sub_mul_ediv_right (a b : Int) {c : Int} (H : c ≠ 0) : (a - b * c) / c = a / c - b := by
  rw [Int.sub_eq_add_neg, ← Int.neg_mul, add_mul_ediv_right _ _ H, Int.sub_eq_add_neg]
Integer Division Identity: $(a - b \cdot c)/c = a/c - b$ for $c \neq 0$
Informal description
For any integers $a$, $b$, and $c$ with $c \neq 0$, the integer division $(a - b \cdot c) / c$ equals $a / c - b$.
Int.sub_mul_ediv_left theorem
(a : Int) {b : Int} (c : Int) (H : b ≠ 0) : (a - b * c) / b = a / b - c
Full source
theorem sub_mul_ediv_left (a : Int) {b : Int}
    (c : Int) (H : b ≠ 0) : (a - b * c) / b = a / b - c := by
  rw [Int.sub_eq_add_neg, ← Int.mul_neg, add_mul_ediv_left _ _ H, Int.sub_eq_add_neg]
Integer Division Identity: $(a - b \cdot c)/b = a/b - c$ for $b \neq 0$
Informal description
For any integers $a$, $b$, and $c$ with $b \neq 0$, the integer division $(a - b \cdot c) / b$ equals $a / b - c$.
Int.mul_sub_ediv_right theorem
(a c : Int) {b : Int} (H : b ≠ 0) : (a * b - c) / b = a + -c / b
Full source
theorem mul_sub_ediv_right (a c : Int) {b : Int} (H : b ≠ 0) : (a * b - c) / b = a + -c / b := by
  rw [Int.sub_eq_add_neg, Int.add_comm, add_mul_ediv_right _ _ H, Int.add_comm]
Integer Division Identity: $(a \cdot b - c)/b = a + (-c)/b$ for $b \neq 0$
Informal description
For any integers $a$, $c$, and nonzero integer $b$, the integer division $(a \cdot b - c) / b$ equals $a + (-c) / b$.
Int.mul_sub_ediv_left theorem
(b : Int) {a : Int} (c : Int) (H : a ≠ 0) : (a * b - c) / a = b + -c / a
Full source
theorem mul_sub_ediv_left (b : Int) {a : Int}
    (c : Int) (H : a ≠ 0) : (a * b - c) / a = b + -c / a := by
  rw [Int.sub_eq_add_neg, Int.add_comm, add_mul_ediv_left _ _ H, Int.add_comm]
Integer Division Identity: $(a \cdot b - c)/a = b + (-c)/a$ for $a \neq 0$
Informal description
For any integers $b$, $c$, and nonzero integer $a$, the integer division $(a \cdot b - c) / a$ equals $b + (-c) / a$.
Int.ediv_neg_of_neg_of_pos theorem
{a b : Int} (Ha : a < 0) (Hb : 0 < b) : a / b < 0
Full source
theorem ediv_neg_of_neg_of_pos {a b : Int} (Ha : a < 0) (Hb : 0 < b) : a / b < 0 :=
  match a, b, eq_negSucc_of_lt_zero Ha, eq_succ_of_zero_lt Hb with
  | _, _, ⟨_, rfl⟩, ⟨_, rfl⟩ => negSucc_lt_zero _
Negative Divided by Positive is Negative
Informal description
For any integers $a$ and $b$ such that $a < 0$ and $b > 0$, the result of the integer division $a / b$ is strictly negative, i.e., $a / b < 0$.
Int.ediv_neg' abbrev
Full source
@[deprecated ediv_neg_of_neg_of_pos (since := "2025-03-04")]
abbrev ediv_neg' := @ediv_neg_of_neg_of_pos
Negative Divided by Negative is Positive
Informal description
For any integers $a$ and $b$ such that $a < 0$ and $b < 0$, the result of the integer division $a / b$ is strictly positive, i.e., $a / b > 0$.
Int.negSucc_ediv theorem
(m : Nat) {b : Int} (H : 0 < b) : -[m+1] / b = -(ediv m b + 1)
Full source
theorem negSucc_ediv (m : Nat) {b : Int} (H : 0 < b) : -[m+1] / b = -(ediv m b + 1) :=
  match b, eq_succ_of_zero_lt H with
  | _, ⟨_, rfl⟩ => rfl
Euclidean Division of Negative Integers: $(-m-1)/b = -(\lfloor m/b \rfloor + 1)$ for $b > 0$
Informal description
For any natural number $m$ and any positive integer $b > 0$, the Euclidean division of the negative integer $-m-1$ by $b$ satisfies: \[ (-m-1) / b = -(\lfloor m / b \rfloor + 1) \]
Int.ediv_nonneg theorem
{a b : Int} (Ha : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a / b
Full source
theorem ediv_nonneg {a b : Int} (Ha : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a / b :=
  match a, b, eq_ofNat_of_zero_le Ha, eq_ofNat_of_zero_le Hb with
  | _, _, ⟨_, rfl⟩, ⟨_, rfl⟩ => ofNat_zero_le _
Nonnegativity of Euclidean Division for Nonnegative Integers: $0 \leq a / b$
Informal description
For any integers $a$ and $b$ such that $0 \leq a$ and $0 \leq b$, the result of the Euclidean division $a / b$ is nonnegative, i.e., $0 \leq a / b$.
Int.ediv_nonneg_of_nonpos_of_nonpos theorem
{a b : Int} (Ha : a ≤ 0) (Hb : b ≤ 0) : 0 ≤ a / b
Full source
theorem ediv_nonneg_of_nonpos_of_nonpos {a b : Int} (Ha : a ≤ 0) (Hb : b ≤ 0) : 0 ≤ a / b := by
  match a, b with
  | ofNat a, b =>
    match Int.le_antisymm Ha (ofNat_zero_le a) with
    | h1 =>
      rw [h1, zero_ediv]
      exact Int.le_refl 0
  | a, ofNat b =>
    match Int.le_antisymm Hb (ofNat_zero_le  b) with
    | h1 =>
      rw [h1, Int.ediv_zero]
      exact Int.le_refl 0
  | negSucc a, negSucc b =>
    rw [Int.div_def, ediv]
    exact le_add_one (ediv_nonneg (ofNat_zero_le a) (Int.le_trans (ofNat_zero_le b) (le.intro 1 rfl)))
Nonnegativity of Euclidean Division for Nonpositive Integers: $0 \leq a / b$ when $a, b \leq 0$
Informal description
For any integers $a$ and $b$ such that $a \leq 0$ and $b \leq 0$, the result of the Euclidean division $a / b$ is nonnegative, i.e., $0 \leq a / b$.
Int.ediv_pos_of_neg_of_neg theorem
{a b : Int} (ha : a < 0) (hb : b < 0) : 0 < a / b
Full source
theorem ediv_pos_of_neg_of_neg {a b : Int} (ha : a < 0) (hb : b < 0) : 0 < a / b := by
  rw [Int.div_def]
  match a, b, ha, hb with
  | .negSucc a, .negSucc b, _, _ => apply ofNat_succ_pos
Positivity of Euclidean Division for Negative Integers: $0 < a / b$ when $a, b < 0$
Informal description
For any integers $a$ and $b$ such that $a < 0$ and $b < 0$, the result of the Euclidean division $a / b$ is strictly positive, i.e., $0 < a / b$.
Int.ediv_nonpos_of_nonneg_of_nonpos theorem
{a b : Int} (Ha : 0 ≤ a) (Hb : b ≤ 0) : a / b ≤ 0
Full source
theorem ediv_nonpos_of_nonneg_of_nonpos {a b : Int} (Ha : 0 ≤ a) (Hb : b ≤ 0) : a / b ≤ 0 :=
  Int.nonpos_of_neg_nonneg <| Int.ediv_neg .. ▸ Int.ediv_nonneg Ha (Int.neg_nonneg_of_nonpos Hb)
Nonpositivity of Euclidean Division for Nonnegative and Nonpositive Integers: $a / b \leq 0$ when $0 \leq a$ and $b \leq 0$
Informal description
For any integers $a$ and $b$ such that $0 \leq a$ and $b \leq 0$, the result of the Euclidean division $a / b$ is nonpositive, i.e., $a / b \leq 0$.
Int.ediv_nonpos abbrev
Full source
@[deprecated ediv_nonpos_of_nonneg_of_nonpos (since := "2025-03-04")]
abbrev ediv_nonpos := @ediv_nonpos_of_nonneg_of_nonpos
Nonpositivity of Euclidean Division for Nonnegative and Nonpositive Integers: $a / b \leq 0$ when $0 \leq a$ and $b \leq 0$
Informal description
For any integers $a$ and $b$ such that $0 \leq a$ and $b \leq 0$, the result of the Euclidean division $a / b$ is nonpositive, i.e., $a / b \leq 0$.
Int.ediv_eq_zero_of_lt theorem
{a b : Int} (H1 : 0 ≤ a) (H2 : a < b) : a / b = 0
Full source
theorem ediv_eq_zero_of_lt {a b : Int} (H1 : 0 ≤ a) (H2 : a < b) : a / b = 0 :=
  match a, b, eq_ofNat_of_zero_le H1, eq_succ_of_zero_lt (Int.lt_of_le_of_lt H1 H2) with
  | _, _, ⟨_, rfl⟩, ⟨_, rfl⟩ => congrArg Nat.cast <| Nat.div_eq_of_lt <| ofNat_lt.1 H2
Euclidean Division Yields Zero for Nonnegative Dividend Less Than Divisor: $0 \leq a < b \Rightarrow a / b = 0$
Informal description
For any integers $a$ and $b$ such that $0 \leq a$ and $a < b$, the Euclidean division of $a$ by $b$ equals zero, i.e., $a / b = 0$.
Int.ediv_eq_neg_one_of_neg_of_le theorem
{a b : Int} (H1 : a < 0) (H2 : -a ≤ b) : a / b = -1
Full source
theorem ediv_eq_neg_one_of_neg_of_le {a b : Int} (H1 : a < 0) (H2 : -a ≤ b) : a / b = -1 := by
  match a, b, H1, H2 with
  | negSucc a', ofNat (b' + 1), H1, H2 =>
    rw [Int.div_def, ediv, Int.negSucc_eq, Int.neg_inj]
    norm_cast
    rw [Nat.add_left_eq_self, Nat.div_eq_zero_iff_lt (by omega)]
    simp [Int.negSucc_eq] at H2
    omega
Euclidean Division Yields $-1$ for Negative Dividend with Small Divisor
Informal description
For any integers $a$ and $b$ such that $a < 0$ and $-a \leq b$, the Euclidean division of $a$ by $b$ equals $-1$, i.e., $a / b = -1$.
Int.ediv_eq_one_of_neg_of_le theorem
{a b : Int} (H1 : a < 0) (H2 : b ≤ a) : a / b = 1
Full source
theorem ediv_eq_one_of_neg_of_le {a b : Int} (H1 : a < 0) (H2 : b ≤ a) : a / b = 1 := by
  match a, b, H1, H2 with
  | negSucc a', ofNat n', H1, H2 => simp [Int.negSucc_eq] at H2; omega
  | negSucc a', negSucc b', H1, H2 =>
    rw [Int.div_def, ediv, ofNat_eq_coe]
    norm_cast
    rw [Nat.succ_eq_add_one, Nat.add_left_eq_self, Nat.div_eq_zero_iff_lt (by omega)]
    simp [Int.negSucc_eq] at H2
    omega
Euclidean Division of Negative Integers: $a / b = 1$ when $a < 0$ and $b \leq a$
Informal description
For any integers $a$ and $b$ such that $a < 0$ and $b \leq a$, the Euclidean division of $a$ by $b$ equals $1$, i.e., $a / b = 1$.
Int.one_ediv theorem
(b : Int) : 1 / b = if b.natAbs = 1 then b else 0
Full source
theorem one_ediv (b : Int) : 1 / b = if b.natAbs = 1 then b else 0 := by
  induction b using wlog_sign
  case inv => simp; split <;> simp
  case w b =>
    match b with
    | 0 => simp
    | 1 => simp
    | b + 2 =>
      apply ediv_eq_zero_of_lt (by decide) (by omega)
Division of One by Integer: $1 / b = b$ if $|b| = 1$, else $0$
Informal description
For any integer $b$, the Euclidean division of $1$ by $b$ equals $b$ if the absolute value of $b$ is $1$, and equals $0$ otherwise, i.e., $1 / b = \begin{cases} b & \text{if } |b| = 1 \\ 0 & \text{otherwise} \end{cases}$.
Int.neg_one_ediv theorem
(b : Int) : -1 / b = -b.sign
Full source
theorem neg_one_ediv (b : Int) : -1 / b = -b.sign :=
  match b with
  | ofNat 0 => by simp
  | ofNat (b + 1) =>
    ediv_eq_neg_one_of_neg_of_le (by decide) (by simp [ofNat_eq_coe]; omega)
  | negSucc b =>
    ediv_eq_one_of_neg_of_le (by decide) (by omega)
Division of Negative One by Integer Yields Negated Sign: $-1 / b = -\text{sign}(b)$
Informal description
For any integer $b$, the Euclidean division of $-1$ by $b$ equals the negation of the sign of $b$, i.e., $-1 / b = -\text{sign}(b)$.
Int.mul_ediv_mul_of_pos theorem
{a : Int} (b c : Int) (H : 0 < a) : (a * b) / (a * c) = b / c
Full source
@[simp] theorem mul_ediv_mul_of_pos {a : Int}
    (b c : Int) (H : 0 < a) : (a * b) / (a * c) = b / c :=
  suffices ∀ (m k : Nat) (b : Int), (m.succ * b) / (m.succ * k) = b / k from
    match a, eq_succ_of_zero_lt H, c, Int.eq_nat_or_neg c with
    | _, ⟨m, rfl⟩, _, ⟨k, .inl rfl⟩ => this _ ..
    | _, ⟨m, rfl⟩, _, ⟨k, .inr rfl⟩ => by
      rw [Int.mul_neg, Int.ediv_neg, Int.ediv_neg]; apply congrArg Neg.neg; apply this
  fun m k b =>
  match b, k with
  | ofNat _, _ => congrArg ofNat (Nat.mul_div_mul_left _ _ m.succ_pos)
  | -[n+1], 0 => by
    rw [Int.ofNat_zero, Int.mul_zero, Int.ediv_zero, Int.ediv_zero]
  | -[n+1], succ k => congrArg negSucc <|
    show (m.succ * n + m) / (m.succ * k.succ) = n / k.succ by
      apply Nat.div_eq_of_lt_le
      · refine Nat.le_trans ?_ (Nat.le_add_right _ _)
        rw [← Nat.mul_div_mul_left _ _ m.succ_pos]
        apply Nat.div_mul_le_self
      · show m.succ * n.succ ≤ _
        rw [Nat.mul_left_comm]
        apply Nat.mul_le_mul_left
        apply (Nat.div_lt_iff_lt_mul k.succ_pos).1
        apply Nat.lt_succ_self
Cancellation of Positive Factor in Integer Division: $(a \cdot b)/(a \cdot c) = b/c$ for $a > 0$
Informal description
For any integer $a > 0$ and integers $b, c$, the integer division $(a \cdot b) / (a \cdot c)$ equals $b / c$.
Int.mul_ediv_mul_of_pos_left theorem
(a : Int) {b : Int} (c : Int) (H : 0 < b) : (a * b) / (c * b) = a / c
Full source
@[simp] theorem mul_ediv_mul_of_pos_left
    (a : Int) {b : Int} (c : Int) (H : 0 < b) : (a * b) / (c * b) = a / c := by
  rw [Int.mul_comm, Int.mul_comm c, mul_ediv_mul_of_pos _ _ H]
Cancellation of Positive Factor in Integer Division: $(a \cdot b)/(c \cdot b) = a/c$ for $b > 0$
Informal description
For any integers $a$ and $c$, and any positive integer $b > 0$, the integer division $(a \cdot b) / (c \cdot b)$ equals $a / c$.
Int.ediv_eq_of_eq_mul_right theorem
{a b c : Int} (H1 : b ≠ 0) (H2 : a = b * c) : a / b = c
Full source
protected theorem ediv_eq_of_eq_mul_right {a b c : Int}
    (H1 : b ≠ 0) (H2 : a = b * c) : a / b = c := by rw [H2, Int.mul_ediv_cancel_left _ H1]
Integer Division Equals Factor When Dividend is Product: $a = b \cdot c \Rightarrow a/b = c$ for $b \neq 0$
Informal description
For any integers $a, b, c$ with $b \neq 0$, if $a = b \cdot c$, then the integer division $a / b$ equals $c$.
Int.ediv_eq_of_eq_mul_left theorem
{a b c : Int} (H1 : b ≠ 0) (H2 : a = c * b) : a / b = c
Full source
protected theorem ediv_eq_of_eq_mul_left {a b c : Int}
    (H1 : b ≠ 0) (H2 : a = c * b) : a / b = c :=
  Int.ediv_eq_of_eq_mul_right H1 (by rw [Int.mul_comm, H2])
Integer Division Equals Factor When Dividend is Product (Left Variant): $a = c \cdot b \Rightarrow a/b = c$ for $b \neq 0$
Informal description
For any integers $a, b, c$ with $b \neq 0$, if $a = c \cdot b$, then the integer division $a / b$ equals $c$.
Int.ediv_self theorem
{a : Int} (H : a ≠ 0) : a / a = 1
Full source
@[simp] protected theorem ediv_self {a : Int} (H : a ≠ 0) : a / a = 1 := by
  have := Int.mul_ediv_cancel 1 H; rwa [Int.one_mul] at this
Self-Division Identity for Nonzero Integers: $a / a = 1$
Informal description
For any nonzero integer $a$, the integer division of $a$ by itself equals $1$, i.e., $a / a = 1$.
Int.neg_ediv_self theorem
(a : Int) (h : a ≠ 0) : (-a) / a = -1
Full source
@[simp] protected theorem neg_ediv_self (a : Int) (h : a ≠ 0) : (-a) / a = -1 := by
  rw [neg_ediv_of_dvd (Int.dvd_refl a), Int.ediv_self h]
Negation of Self-Division Identity: $(-a)/a = -1$ for $a \neq 0$
Informal description
For any nonzero integer $a$, the integer division of $-a$ by $a$ equals $-1$, i.e., $(-a)/a = -1$.
Int.sign_ediv theorem
(a b : Int) : sign (a / b) = if 0 ≤ a ∧ a < b.natAbs then 0 else sign a * sign b
Full source
theorem sign_ediv (a b : Int) : sign (a / b) = if 0 ≤ a ∧ a < b.natAbs then 0 else sign a * sign b := by
  induction b using wlog_sign
  case inv => simp; split <;> simp [Int.mul_neg]
  case w b =>
    match b with
    | 0 => simp
    | b + 1 =>
      have : ((b + 1 : Nat) : Int).natAbs = b + 1 := by omega
      rw [this]
      match a with
      | 0 => simp
      | (a + 1 : Nat) =>
        norm_cast
        simp only [Nat.le_add_left, Nat.add_lt_add_iff_right, true_and, Int.natCast_add,
          cast_ofNat_Int, sign_of_add_one, Int.mul_one]
        split
        · rw [Nat.div_eq_of_lt (by omega)]
          simp
        · have := Nat.div_pos (a := a + 1) (b := b + 1) (by omega) (by omega)
          rw [sign_eq_one_of_pos (mod_cast this)]
      | negSucc a =>
        norm_cast
Sign of Integer Division: $\text{sign}(a / b) = \text{sign}(a) \cdot \text{sign}(b)$ unless $0 \leq a < |b|$
Informal description
For any integers $a$ and $b$, the sign of the integer division $a / b$ is given by: \[ \text{sign}(a / b) = \begin{cases} 0 & \text{if } 0 \leq a < |b| \\ \text{sign}(a) \cdot \text{sign}(b) & \text{otherwise} \end{cases} \]
Int.mod_def' theorem
(m n : Int) : m % n = emod m n
Full source
theorem mod_def' (m n : Int) : m % n = emod m n := rfl
Definition of Integer Modulus via Euclidean Modulus
Informal description
For any integers $m$ and $n$, the modulus operation $m \% n$ is equal to the Euclidean modulus $\mathrm{emod}\, m\, n$.
Int.negSucc_emod theorem
(m : Nat) {b : Int} (bpos : 0 < b) : -[m+1] % b = b - 1 - m % b
Full source
theorem negSucc_emod (m : Nat) {b : Int} (bpos : 0 < b) : -[m+1] % b = b - 1 - m % b := by
  rw [Int.sub_sub, Int.add_comm]
  match b, eq_succ_of_zero_lt bpos with
  | _, ⟨n, rfl⟩ => rfl
Euclidean Modulus of Negative Successor: $-(m+1) \bmod b = b - 1 - (m \bmod b)$
Informal description
For any natural number $m$ and integer $b > 0$, the Euclidean modulus of the negative integer $- (m + 1)$ by $b$ satisfies: $$- (m + 1) \bmod b = b - 1 - (m \bmod b)$$
Int.emod_negSucc theorem
(m : Nat) (n : Int) : (Int.negSucc m) % n = Int.subNatNat (Int.natAbs n) (Nat.succ (m % Int.natAbs n))
Full source
theorem emod_negSucc (m : Nat) (n : Int) :
  (Int.negSucc m) % n = Int.subNatNat (Int.natAbs n) (Nat.succ (m % Int.natAbs n)) := rfl
Euclidean Modulus of Negative Successor: $(-(m+1)) \bmod n = |n| - (m \% |n| + 1)$
Informal description
For any natural number $m$ and integer $n$, the Euclidean modulus of the negative integer $- (m + 1)$ by $n$ is equal to the integer obtained by subtracting $(m \% |n|) + 1$ from $|n|$, where $|n|$ denotes the absolute value of $n$ and $\%$ is the natural number modulus operation. More precisely: $$(- (m + 1)) \bmod n = |n| - (m \% |n| + 1)$$
Int.ofNat_mod_ofNat theorem
(m n : Nat) : (m % n : Int) = ↑(m % n)
Full source
theorem ofNat_mod_ofNat (m n : Nat) : (m % n : Int) = ↑(m % n) := rfl
Natural Modulus Coerces to Integer Modulus
Informal description
For any natural numbers $m$ and $n$, the integer obtained by coercing the natural number $m \% n$ (the modulus of $m$ by $n$) is equal to the coercion of $m \% n$ as an integer. In other words, the modulus operation commutes with the natural number to integer coercion.
Int.add_neg_mul_emod_self theorem
{a b c : Int} : (a + -(b * c)) % c = a % c
Full source
@[simp] theorem add_neg_mul_emod_self {a b c : Int} : (a + -(b * c)) % c = a % c := by
  rw [Int.neg_mul_eq_neg_mul, add_mul_emod_self]
Remainder Invariance under Subtraction of Multiple: $(a - b \cdot c) \% c = a \% c$
Informal description
For any integers $a$, $b$, and $c$, the remainder of $(a - b \cdot c)$ modulo $c$ is equal to the remainder of $a$ modulo $c$, i.e., $(a - b \cdot c) \% c = a \% c$.
Int.add_neg_mul_emod_self_left theorem
{a b c : Int} : (a + -(b * c)) % b = a % b
Full source
@[simp] theorem add_neg_mul_emod_self_left {a b c : Int} : (a + -(b * c)) % b = a % b := by
  rw [Int.neg_mul_eq_mul_neg, add_mul_emod_self_left]
Remainder Invariance under Left Subtraction of Multiple: $(a - b \cdot c) \% b = a \% b$
Informal description
For any integers $a$, $b$, and $c$, the remainder of $(a - b \cdot c)$ modulo $b$ is equal to the remainder of $a$ modulo $b$, i.e., $(a - b \cdot c) \% b = a \% b$.
Int.add_emod_self theorem
{a b : Int} : (a + b) % b = a % b
Full source
@[simp] theorem add_emod_self {a b : Int} : (a + b) % b = a % b := by
  have := add_mul_emod_self_left a b 1; rwa [Int.mul_one] at this
Remainder Invariance under Addition: $(a + b) \% b = a \% b$
Informal description
For any integers $a$ and $b$, the remainder of $(a + b)$ modulo $b$ is equal to the remainder of $a$ modulo $b$, i.e., $(a + b) \% b = a \% b$.
Int.add_emod_self_left theorem
{a b : Int} : (a + b) % a = b % a
Full source
@[simp] theorem add_emod_self_left {a b : Int} : (a + b) % a = b % a := by
  rw [Int.add_comm, Int.add_emod_self]
Remainder Invariance under Left Addition: $(a + b) \bmod a = b \bmod a$
Informal description
For any integers $a$ and $b$, the remainder of $(a + b)$ modulo $a$ is equal to the remainder of $b$ modulo $a$, i.e., $(a + b) \bmod a = b \bmod a$.
Int.neg_emod_eq_sub_emod theorem
{a b : Int} : -a % b = (b - a) % b
Full source
theorem neg_emod_eq_sub_emod {a b : Int} : -a % b = (b - a) % b := by
  rw [← add_emod_self_left]; rfl
Remainder of Negation Equals Remainder of Divisor Minus Number: $-a \bmod b = (b - a) \bmod b$
Informal description
For any integers $a$ and $b$, the remainder of $-a$ modulo $b$ is equal to the remainder of $(b - a)$ modulo $b$, i.e., $-a \bmod b = (b - a) \bmod b$.
Int.emod_eq_add_self_emod theorem
{a b : Int} : a % b = (a + b) % b
Full source
theorem emod_eq_add_self_emod {a b : Int} : a % b = (a + b) % b :=
  Int.add_emod_self.symm
Remainder Invariance under Addition of Divisor: $a \bmod b = (a + b) \bmod b$
Informal description
For any integers $a$ and $b$, the remainder of $a$ modulo $b$ is equal to the remainder of $(a + b)$ modulo $b$, i.e., $a \bmod b = (a + b) \bmod b$.
Int.emod_neg theorem
(a b : Int) : a % -b = a % b
Full source
@[simp] theorem emod_neg (a b : Int) : a % -b = a % b := by
  rw [emod_def, emod_def, Int.ediv_neg, Int.neg_mul_neg]
Remainder Invariance Under Divisor Negation: $a \bmod (-b) = a \bmod b$
Informal description
For any integers $a$ and $b$, the remainder when $a$ is divided by $-b$ equals the remainder when $a$ is divided by $b$, i.e., $a \bmod (-b) = a \bmod b$.
Int.neg_emod_self theorem
(a : Int) : -a % a = 0
Full source
@[simp] theorem neg_emod_self (a : Int) : -a % a = 0 := by
  rw [neg_emod_eq_sub_emod, Int.sub_self, zero_emod]
Remainder of Negation by Itself is Zero: $-a \bmod a = 0$
Informal description
For any integer $a$, the remainder when $-a$ is divided by $a$ is $0$, i.e., $-a \bmod a = 0$.
Int.emod_sub_emod theorem
(m n k : Int) : (m % n - k) % n = (m - k) % n
Full source
@[simp] theorem emod_sub_emod (m n k : Int) : (m % n - k) % n = (m - k) % n :=
  Int.emod_add_emod m n (-k)
Modular Subtraction Invariance: $(m \% n - k) \% n = (m - k) \% n$
Informal description
For any integers $m$, $n$, and $k$, the remainder of $(m \% n - k)$ modulo $n$ is equal to the remainder of $(m - k)$ modulo $n$, i.e., \[ (m \% n - k) \% n = (m - k) \% n. \]
Int.sub_emod_emod theorem
(m n k : Int) : (m - n % k) % k = (m - n) % k
Full source
@[simp] theorem sub_emod_emod (m n k : Int) : (m - n % k) % k = (m - n) % k := by
  apply (emod_add_cancel_right (n % k)).mp
  rw [Int.sub_add_cancel, Int.add_emod_emod, Int.sub_add_cancel]
Modular Subtraction Invariance: $(m - n \% k) \% k = (m - n) \% k$
Informal description
For any integers $m$, $n$, and $k$, the remainder of $(m - n \% k)$ modulo $k$ is equal to the remainder of $(m - n)$ modulo $k$, i.e., \[ (m - n \% k) \% k = (m - n) \% k. \]
Int.emod_eq_of_lt theorem
{a b : Int} (H1 : 0 ≤ a) (H2 : a < b) : a % b = a
Full source
theorem emod_eq_of_lt {a b : Int} (H1 : 0 ≤ a) (H2 : a < b) : a % b = a :=
  have b0 := Int.le_trans H1 (Int.le_of_lt H2)
  match a, b, eq_ofNat_of_zero_le H1, eq_ofNat_of_zero_le b0 with
  | _, _, ⟨_, rfl⟩, ⟨_, rfl⟩ => congrArg ofNat <| Nat.mod_eq_of_lt (Int.ofNat_lt.1 H2)
Euclidean Modulus Identity for Nonnegative Integers Under Strict Inequality: $a \bmod b = a$ when $0 \leq a < b$
Informal description
For any integers $a$ and $b$ such that $0 \leq a$ and $a < b$, the Euclidean modulus satisfies $a \bmod b = a$.
Int.emod_lt_of_neg theorem
(a : Int) {b : Int} (h : b < 0) : a % b < -b
Full source
theorem emod_lt_of_neg (a : Int) {b : Int} (h : b < 0) : a % b < -b := by
  match b, h with
  | .negSucc b', h =>
    simp only [negSucc_eq, emod_neg]
    apply emod_lt_of_pos
    omega
Remainder Bound for Negative Divisors: $a \bmod b < -b$ when $b < 0$
Informal description
For any integer $a$ and any negative integer $b < 0$, the remainder $a \% b$ satisfies $a \% b < -b$.
Int.emod_lt theorem
(a : Int) {b : Int} (h : b ≠ 0) : a % b < b.natAbs
Full source
theorem emod_lt (a : Int) {b : Int} (h : b ≠ 0) : a % b < b.natAbs :=
  match b, h with
  | (b : Nat), h => emod_lt_of_pos a (by omega)
  | negSucc b, h => emod_lt_of_neg a (by omega)
Euclidean Remainder Bound: $a \bmod b < |b|$ for $b \neq 0$
Informal description
For any integer $a$ and any nonzero integer $b$, the Euclidean remainder $a \bmod b$ satisfies $a \bmod b < |b|$, where $|b|$ denotes the absolute value of $b$.
Int.emod_self_add_one theorem
{x : Int} (h : 0 ≤ x) : x % (x + 1) = x
Full source
@[simp] theorem emod_self_add_one {x : Int} (h : 0 ≤ x) : x % (x + 1) = x :=
  emod_eq_of_lt h (Int.lt_succ x)
Modulus Identity: $x \bmod (x + 1) = x$ for $x \geq 0$
Informal description
For any nonnegative integer $x \geq 0$, the Euclidean modulus of $x$ by $x+1$ equals $x$, i.e., $x \bmod (x + 1) = x$.
Int.sign_emod theorem
(a : Int) {b} (h : b ≠ 0) : sign (a % b) = if b ∣ a then 0 else 1
Full source
theorem sign_emod (a : Int) {b} (h : b ≠ 0) : sign (a % b) = if b ∣ a then 0 else 1 := by
  split <;> rename_i w
  · simp [emod_eq_zero_of_dvd, w]
  · obtain rfl | w := emod_pos_of_not_dvd w
    · simp at h
    · simp [sign_eq_one_of_pos w]
Sign of Remainder Modulo Nonzero Integer: $\text{sign}(a \bmod b) = 0$ if $b \mid a$, else $1$
Informal description
For any integer $a$ and nonzero integer $b$, the sign of the remainder $a \bmod b$ is $0$ if $b$ divides $a$, and $1$ otherwise. That is, $$\text{sign}(a \bmod b) = \begin{cases} 0 & \text{if } b \mid a \\ 1 & \text{otherwise} \end{cases}$$
Int.one_emod theorem
{b : Int} : 1 % b = if b.natAbs = 1 then 0 else 1
Full source
theorem one_emod {b : Int} : 1 % b = if b.natAbs = 1 then 0 else 1 := by
  rw [emod_def, one_ediv]
  split <;> rename_i h
  · obtain rfl | rfl := natAbs_eq_iff.mp h <;> simp
  · simp
Modulus of One: $1 \bmod b = 0$ if $|b| = 1$, else $1$
Informal description
For any integer $b$, the Euclidean modulus of $1$ by $b$ is equal to $0$ if the absolute value of $b$ (as a natural number) is $1$, and $1$ otherwise. That is, $$1 \bmod b = \begin{cases} 0 & \text{if } |b| = 1 \\ 1 & \text{otherwise} \end{cases}$$
Int.mul_ediv_cancel_of_dvd theorem
{a b : Int} (H : b ∣ a) : b * (a / b) = a
Full source
theorem mul_ediv_cancel_of_dvd {a b : Int} (H : b ∣ a) : b * (a / b) = a :=
  mul_ediv_cancel_of_emod_eq_zero (emod_eq_zero_of_dvd H)
Integer Division Cancellation: $b \cdot (a / b) = a$ when $b \mid a$
Informal description
For any integers $a$ and $b$ such that $b$ divides $a$ (i.e., $b \mid a$), the product of $b$ and the integer division of $a$ by $b$ equals $a$, i.e., $b \cdot (a / b) = a$.
Int.ediv_mul_cancel_of_dvd theorem
{a b : Int} (H : b ∣ a) : a / b * b = a
Full source
theorem ediv_mul_cancel_of_dvd {a b : Int} (H : b ∣ a) : a / b * b = a :=
  ediv_mul_cancel_of_emod_eq_zero (emod_eq_zero_of_dvd H)
Integer Division Cancellation: $(a / b) \cdot b = a$ when $b \mid a$
Informal description
For any integers $a$ and $b$ such that $b$ divides $a$ (i.e., $b \mid a$), the integer division of $a$ by $b$ multiplied by $b$ equals $a$, i.e., $(a / b) \cdot b = a$.
Int.emod_two_eq theorem
(x : Int) : x % 2 = 0 ∨ x % 2 = 1
Full source
theorem emod_two_eq (x : Int) : x % 2 = 0 ∨ x % 2 = 1 := by
  have h₁ : 0 ≤ x % 2 := Int.emod_nonneg x (by decide)
  have h₂ : x % 2 < 2 := Int.emod_lt_of_pos x (by decide)
  match x % 2, h₁, h₂ with
  | 0, _, _ => simp
  | 1, _, _ => simp
Possible Remainders Modulo 2: $x \bmod 2 \in \{0,1\}$
Informal description
For any integer $x$, the remainder when $x$ is divided by 2 is either 0 or 1, i.e., $x \bmod 2 \in \{0, 1\}$.
Int.add_emod_eq_add_emod_left theorem
{m n k : Int} (i : Int) (H : m % n = k % n) : (i + m) % n = (i + k) % n
Full source
theorem add_emod_eq_add_emod_left {m n k : Int} (i : Int)
    (H : m % n = k % n) : (i + m) % n = (i + k) % n := by
  rw [Int.add_comm, add_emod_eq_add_emod_right _ H, Int.add_comm]
Modular Equivalence Preserved Under Left Addition: $(i + m) \bmod n = (i + k) \bmod n$ when $m \equiv k \pmod{n}$
Informal description
For any integers $m$, $n$, $k$, and $i$, if $m \bmod n = k \bmod n$, then $(i + m) \bmod n = (i + k) \bmod n$.
Int.emod_add_cancel_left theorem
{m n k i : Int} : (i + m) % n = (i + k) % n ↔ m % n = k % n
Full source
theorem emod_add_cancel_left {m n k i : Int} : (i + m) % n = (i + k) % n ↔ m % n = k % n := by
  rw [Int.add_comm, Int.add_comm i, emod_add_cancel_right]
Modular Equivalence Under Left Addition: $(i + m) \bmod n = (i + k) \bmod n \leftrightarrow m \bmod n = k \bmod n$
Informal description
For any integers $m$, $n$, $k$, and $i$, the equality $(i + m) \bmod n = (i + k) \bmod n$ holds if and only if $m \bmod n = k \bmod n$.
Int.emod_sub_cancel_right theorem
{m n k : Int} (i) : (m - i) % n = (k - i) % n ↔ m % n = k % n
Full source
theorem emod_sub_cancel_right {m n k : Int} (i) : (m - i) % n = (k - i) % n ↔ m % n = k % n :=
  emod_add_cancel_right _
Modular Equivalence Under Right Subtraction: $(m - i) \bmod n = (k - i) \bmod n \leftrightarrow m \bmod n = k \bmod n$
Informal description
For any integers $m$, $n$, $k$, and $i$, the equality $(m - i) \bmod n = (k - i) \bmod n$ holds if and only if $m \bmod n = k \bmod n$.
Int.emod_eq_emod_iff_emod_sub_eq_zero theorem
{m n k : Int} : m % n = k % n ↔ (m - k) % n = 0
Full source
theorem emod_eq_emod_iff_emod_sub_eq_zero {m n k : Int} : m % n = k % n ↔ (m - k) % n = 0 :=
  (emod_sub_cancel_right k).symm.trans <| by simp [Int.sub_self]
Modular Equivalence via Difference: $m \equiv k \bmod n \leftrightarrow (m - k) \equiv 0 \bmod n$
Informal description
For any integers $m$, $n$, and $k$, the remainders of $m$ and $k$ modulo $n$ are equal if and only if the remainder of their difference $(m - k)$ modulo $n$ is zero, i.e., $$m \bmod n = k \bmod n \leftrightarrow (m - k) \bmod n = 0.$$
Int.ediv_emod_unique theorem
{a b r q : Int} (h : 0 < b) : a / b = q ∧ a % b = r ↔ r + b * q = a ∧ 0 ≤ r ∧ r < b
Full source
protected theorem ediv_emod_unique {a b r q : Int} (h : 0 < b) :
    a / b = q ∧ a % b = ra / b = q ∧ a % b = r ↔ r + b * q = a ∧ 0 ≤ r ∧ r < b := by
  constructor
  · intro ⟨rfl, rfl⟩
    exact ⟨emod_add_ediv a b, emod_nonneg _ (Int.ne_of_gt h), emod_lt_of_pos _ h⟩
  · intro ⟨rfl, hz, hb⟩
    constructor
    · rw [Int.add_mul_ediv_left r q (Int.ne_of_gt h), ediv_eq_zero_of_lt hz hb]
      simp [Int.zero_add]
    · rw [add_mul_emod_self_left, emod_eq_of_lt hz hb]
Euclidean Division-Remainder Characterization for Positive Divisors
Informal description
For any integers $a$, $b$, $r$, and $q$ with $b > 0$, the following are equivalent: 1. The Euclidean division of $a$ by $b$ yields quotient $q$ and remainder $r$, i.e., $a / b = q$ and $a \% b = r$. 2. The decomposition $r + b \cdot q = a$ holds, with $0 \leq r < b$.
Int.ediv_emod_unique' theorem
{a b r q : Int} (h : b < 0) : a / b = q ∧ a % b = r ↔ r + b * q = a ∧ 0 ≤ r ∧ r < -b
Full source
protected theorem ediv_emod_unique' {a b r q : Int} (h : b < 0) :
    a / b = q ∧ a % b = ra / b = q ∧ a % b = r ↔ r + b * q = a ∧ 0 ≤ r ∧ r < -b := by
  have := Int.ediv_emod_unique (a := a) (b := -b) (r := r) (q := -q) (by omega)
  simpa [Int.neg_inj, Int.neg_mul, Int.mul_neg]
Euclidean Division-Remainder Characterization for Negative Divisors
Informal description
For any integers $a$, $b$, $r$, and $q$ with $b < 0$, the following are equivalent: 1. The Euclidean division of $a$ by $b$ yields quotient $q$ and remainder $r$, i.e., $a / b = q$ and $a \% b = r$. 2. The decomposition $r + b \cdot q = a$ holds, with $0 \leq r < -b$.
Int.mul_emod_mul_of_pos theorem
{a : Int} (b c : Int) (H : 0 < a) : (a * b) % (a * c) = a * (b % c)
Full source
@[simp] theorem mul_emod_mul_of_pos
    {a : Int} (b c : Int) (H : 0 < a) : (a * b) % (a * c) = a * (b % c) := by
  rw [emod_def, emod_def, mul_ediv_mul_of_pos _ _ H, Int.mul_sub, Int.mul_assoc]
Modulo Scaling Identity: $(a \cdot b) \bmod (a \cdot c) = a \cdot (b \bmod c)$ for $a > 0$
Informal description
For any positive integer $a$ and integers $b, c$, the modulo operation satisfies $(a \cdot b) \bmod (a \cdot c) = a \cdot (b \bmod c)$.
Int.lt_ediv_add_one_mul_self theorem
(a : Int) {b : Int} (H : 0 < b) : a < (a / b + 1) * b
Full source
theorem lt_ediv_add_one_mul_self (a : Int) {b : Int} (H : 0 < b) : a < (a / b + 1) * b := by
  rw [Int.add_mul, Int.one_mul, Int.mul_comm]
  exact Int.lt_add_of_sub_left_lt <| Int.emod_def .. ▸ emod_lt_of_pos _ H
Integer Division Lower Bound: $a < \left(\left\lfloor \frac{a}{b} \right\rfloor + 1\right) \cdot b$ for $b > 0$
Informal description
For any integer $a$ and positive integer $b > 0$, the inequality $a < \left(\left\lfloor \frac{a}{b} \right\rfloor + 1\right) \cdot b$ holds.
Int.neg_ediv theorem
{a b : Int} : (-a) / b = -(a / b) - if b ∣ a then 0 else b.sign
Full source
theorem neg_ediv {a b : Int} : (-a) / b = -(a / b) - if b ∣ a then 0 else b.sign := by
  if hb : b = 0 then
    simp [hb]
  else
    conv => lhs; rw [← ediv_add_emod a b]
    rw [Int.neg_add, ← Int.mul_neg, mul_add_ediv_left _ _ hb, Int.add_comm]
    split <;> rename_i h
    · rw [emod_eq_zero_of_dvd h]
      simp
    · if hb : 0 < b then
        rw [Int.sign_eq_one_of_pos hb, ediv_eq_neg_one_of_neg_of_le]
        · omega
        · have : 0 < a % b := (emod_pos_of_not_dvd h).resolve_left (by omega)
          omega
        · have := emod_lt_of_pos a hb
          omega
      else
        replace hb : b < 0 := by omega
        rw [Int.sign_eq_neg_one_of_neg hb, Int.ediv_eq_one_of_neg_of_le]
        · omega
        · have : 0 < a % b := (emod_pos_of_not_dvd h).resolve_left (by omega)
          omega
        · have := emod_lt_of_neg a hb
          omega
Euclidean Division of Negated Integer: $(-a)/b = -(a/b) - \text{sign}(b)$ unless $b \mid a$
Informal description
For any integers $a$ and $b$, the Euclidean division of $-a$ by $b$ satisfies: \[ (-a) / b = -(a / b) - \begin{cases} 0 & \text{if } b \mid a \\ \text{sign}(b) & \text{otherwise} \end{cases} \] where $\text{sign}(b)$ is $1$ if $b > 0$, $-1$ if $b < 0$, and $0$ if $b = 0$.
Int.tdiv_cases theorem
(n m : Int) : n.tdiv m = if 0 ≤ n then if 0 ≤ m then n / m else -(n / (-m)) else if 0 ≤ m then -((-n) / m) else (-n) / (-m)
Full source
theorem tdiv_cases (n m : Int) : n.tdiv m =
    if 0 ≤ n then
      if 0 ≤ m then n / m else -(n / (-m))
    else
      if 0 ≤ m then -((-n) / m) else (-n) / (-m) := by
  split <;> rename_i hn
  · split <;> rename_i hm
    · rw [Int.tdiv_eq_ediv_of_nonneg hn]
    · rw [Int.tdiv_eq_ediv_of_nonneg hn]
      simp
  · split <;> rename_i hm
    · rw [Int.tdiv_eq_ediv, Int.neg_ediv]
      simp [hn, Int.neg_sub, Int.add_comm]
    · rw [Int.tdiv_eq_ediv, Int.neg_ediv, Int.ediv_neg]
      simp [hn, Int.sub_eq_add_neg, apply_ite Neg.neg]
Case Analysis for Truncating Division: $n \ \mathrm{tdiv} \ m$ in Terms of Euclidean Division
Informal description
For any integers $n$ and $m$, the truncating division $n \ \mathrm{tdiv} \ m$ satisfies: \[ n \ \mathrm{tdiv} \ m = \begin{cases} n / m & \text{if } 0 \leq n \text{ and } 0 \leq m, \\ -(n / (-m)) & \text{if } 0 \leq n \text{ and } m < 0, \\ -((-n) / m) & \text{if } n < 0 \text{ and } 0 \leq m, \\ (-n) / (-m) & \text{if } n < 0 \text{ and } m < 0. \end{cases} \]
Int.neg_emod theorem
{a b : Int} : (-a) % b = if b ∣ a then 0 else b.natAbs - (a % b)
Full source
theorem neg_emod {a b : Int} : (-a) % b = if b ∣ a then 0 else b.natAbs - (a % b) := by
  rw [emod_def, emod_def, neg_ediv, Int.mul_sub, Int.mul_neg]
  split <;> rename_i h
  · simp [mul_ediv_cancel_of_dvd h]
  · simp
    omega
Remainder of Negated Integer: $(-a) \bmod b = |b| - (a \bmod b)$ unless $b \mid a$
Informal description
For any integers $a$ and $b$, the remainder of $-a$ modulo $b$ satisfies: \[ (-a) \bmod b = \begin{cases} 0 & \text{if } b \text{ divides } a \\ |b| - (a \bmod b) & \text{otherwise} \end{cases} \] where $|b|$ denotes the absolute value of $b$ as a natural number.
Int.natAbs_ediv theorem
(a : Int) {b : Int} (hb : b ≠ 0) : natAbs (a / b) = natAbs a / natAbs b + if 0 ≤ a ∨ b ∣ a then 0 else 1
Full source
theorem natAbs_ediv (a : Int) {b : Int} (hb : b ≠ 0) : natAbs (a / b) = natAbs a / natAbs b + if 0 ≤ a ∨ b ∣ a then 0 else 1 := by
  induction b using wlog_sign
  case inv => simp
  case w b =>
    match a with
    | 0 => simp
    | (a + 1 : Nat) => norm_cast
    | negSucc a =>
      simp only [negSucc_eq]
      norm_cast
      rw [neg_ediv]
      norm_cast
      rw [natAbs_neg, natAbs_ofNat]
      have : ¬ 0 ≤ -((a + 1 : Nat) : Int) := by omega
      simp only [this]
      have : ↑b ∣ -((a + 1 : Nat) : Int)↑b ∣ -((a + 1 : Nat) : Int)  ↔ b ∣ a + 1 := by simp; norm_cast
      simp only [this, false_or]
      split <;> rename_i h
      · simp
      · rw [Nat.succ_div, if_neg h, sign_eq_one_of_pos (by omega), Int.sub_eq_add_neg, ← Int.neg_add, natAbs_neg]
        norm_cast
Absolute Value of Integer Division: $|a/b| = \lfloor |a|/|b| \rfloor + \mathbb{1}_{\{a < 0 \text{ and } b \nmid a\}}$
Informal description
For any integer $a$ and nonzero integer $b$, the absolute value of the Euclidean division $a / b$ (as a natural number) satisfies: \[ |a / b| = \left\lfloor \frac{|a|}{|b|} \right\rfloor + \begin{cases} 0 & \text{if } 0 \leq a \text{ or } b \mid a \\ 1 & \text{otherwise} \end{cases} \] where $| \cdot |$ denotes the absolute value function and $\lfloor \cdot \rfloor$ denotes the floor function.
Int.natAbs_emod_of_nonneg theorem
{a : Int} (h : 0 ≤ a) (b : Int) : natAbs (a % b) = natAbs a % natAbs b
Full source
theorem natAbs_emod_of_nonneg {a : Int} (h : 0 ≤ a) (b : Int) :
    natAbs (a % b) = natAbs a % natAbs b := by
  match a, b, h with
  | (a : Nat), (b : Nat), _ => norm_cast
  | (a : Nat), negSucc b, _ => simp [negSucc_eq]; norm_cast
Absolute Value of Remainder for Non-Negative Integers: $|a \bmod b| = |a| \bmod |b|$ when $0 \leq a$
Informal description
For any non-negative integer $a$ (i.e., $0 \leq a$) and any integer $b$, the absolute value of the remainder $a \bmod b$ (as a natural number) equals the remainder of the absolute values of $a$ and $b$ (as natural numbers), i.e., $|a \bmod b| = |a| \bmod |b|$.
Int.natAbs_emod theorem
(a : Int) {b : Int} (hb : b ≠ 0) : natAbs (a % b) = if 0 ≤ a ∨ b ∣ a then natAbs a % natAbs b else natAbs b - natAbs a % natAbs b
Full source
theorem natAbs_emod (a : Int) {b : Int} (hb : b ≠ 0):
    natAbs (a % b) = if 0 ≤ a ∨ b ∣ a then natAbs a % natAbs b else natAbs b - natAbs a % natAbs b := by
  match a with
  | (a : Nat) => simp [natAbs_emod_of_nonneg]
  | negSucc a =>
    simp
    split <;> rename_i h
    · simp [negSucc_eq]
      rw [emod_eq_zero_of_dvd, Nat.mod_eq_zero_of_dvd, natAbs_zero]
      · exact ofNat_dvd_right.mp h
      · exact dvd_natAbs.mp h
    simp [negSucc_eq]
    simp [neg_emod]
    rw [if_neg h]
    norm_cast
    have := natAbs_emod_of_nonneg (a := a + 1) (by omega) b
    norm_cast at this
    rw [natAbs_sub_of_nonneg_of_le, this]
    omega
    · exact emod_nonneg _ hb
    · have := emod_lt (a + 1 : Nat) hb
      omega
Absolute Value of Integer Remainder: $|a \bmod b| = |a| \bmod |b|$ or $|b| - (|a| \bmod |b|)$
Informal description
For any integer $a$ and nonzero integer $b$, the absolute value of the remainder $a \bmod b$ (as a natural number) satisfies: \[ |a \bmod b| = \begin{cases} |a| \bmod |b| & \text{if } 0 \leq a \text{ or } b \mid a \\ |b| - (|a| \bmod |b|) & \text{otherwise} \end{cases} \] where $| \cdot |$ denotes the absolute value function and $\bmod$ denotes the modulo operation on natural numbers.
Int.natAbs_ediv_le_natAbs theorem
(a b : Int) : natAbs (a / b) ≤ natAbs a
Full source
theorem natAbs_ediv_le_natAbs (a b : Int) : natAbs (a / b) ≤ natAbs a :=
  match b, Int.eq_nat_or_neg b with
  | _, ⟨n, .inl rfl⟩ => aux _ _
  | _, ⟨n, .inr rfl⟩ => by rw [Int.ediv_neg, natAbs_neg]; apply aux
where
  aux : ∀ (a : Int) (n : Nat), natAbs (a / n) ≤ natAbs a
  | ofNat _, _ => Nat.div_le_self ..
  | -[_+1], 0 => Nat.zero_le _
  | -[_+1], succ _ => Nat.succ_le_succ (Nat.div_le_self _ _)
Absolute Value Bound for Euclidean Division: $|a / b| \leq |a|$
Informal description
For any integers $a$ and $b$, the absolute value of the Euclidean division $a / b$ (as a natural number) is less than or equal to the absolute value of $a$ (as a natural number), i.e., $|a / b| \leq |a|$.
Int.natAbs_div_le_natAbs abbrev
Full source
@[deprecated natAbs_ediv_le_natAbs (since := "2025-03-05")]
abbrev natAbs_div_le_natAbs := natAbs_ediv_le_natAbs
Absolute Value Bound for Euclidean Division: $|a / b| \leq |a|$
Informal description
For any integers $a$ and $b$, the absolute value of the Euclidean division $a / b$ (as a natural number) is less than or equal to the absolute value of $a$ (as a natural number), i.e., $|a / b| \leq |a|$.
Int.ediv_le_self theorem
{a : Int} (b : Int) (Ha : 0 ≤ a) : a / b ≤ a
Full source
theorem ediv_le_self {a : Int} (b : Int) (Ha : 0 ≤ a) : a / b ≤ a := by
  have := Int.le_trans le_natAbs (ofNat_le.2 <| natAbs_ediv_le_natAbs a b)
  rwa [natAbs_of_nonneg Ha] at this
Euclidean Division Bound: $a / b \leq a$ for $a \geq 0$
Informal description
For any nonnegative integer $a$ (i.e., $0 \leq a$) and any integer $b$, the Euclidean division of $a$ by $b$ satisfies $a / b \leq a$.
Int.dvd_emod_sub_self theorem
{x : Int} {m : Nat} : (m : Int) ∣ x % m - x
Full source
theorem dvd_emod_sub_self {x : Int} {m : Nat} : (m : Int) ∣ x % m - x := by
  apply dvd_of_emod_eq_zero
  simp [sub_emod]
Divisibility of Remainder Difference: $m \mid (x \% m - x)$
Informal description
For any integer $x$ and natural number $m$, the integer $m$ divides the difference between the remainder of $x$ modulo $m$ and $x$ itself, i.e., $m \mid (x \% m - x)$.
Int.neg_mul_emod_left theorem
(a b : Int) : -(a * b) % b = 0
Full source
@[simp] theorem neg_mul_emod_left (a b : Int) : -(a * b) % b = 0 := by
  rw [← dvd_iff_emod_eq_zero, Int.dvd_neg]
  exact Int.dvd_mul_left a b
Modulus of Negated Product: $-(a \cdot b) \bmod b = 0$
Informal description
For any integers $a$ and $b$, the remainder of $-(a \cdot b)$ divided by $b$ is zero, i.e., $-(a \cdot b) \bmod b = 0$.
Int.neg_mul_emod_right theorem
(a b : Int) : -(a * b) % a = 0
Full source
@[simp] theorem neg_mul_emod_right (a b : Int) : -(a * b) % a = 0 := by
  rw [← dvd_iff_emod_eq_zero, Int.dvd_neg]
  exact Int.dvd_mul_right a b
Modulus of Negated Product by First Factor: $-(a \cdot b) \bmod a = 0$
Informal description
For any integers $a$ and $b$, the remainder of $-(a \cdot b)$ divided by $a$ is zero, i.e., $-(a \cdot b) \bmod a = 0$.
Int.neg_mul_ediv_cancel theorem
(a b : Int) (h : b ≠ 0) : -(a * b) / b = -a
Full source
@[deprecated mul_ediv_cancel (since := "2025-03-05")]
theorem neg_mul_ediv_cancel (a b : Int) (h : b ≠ 0) : -(a * b) / b = -a := by
  rw [neg_ediv_of_dvd (Int.dvd_mul_left a b), mul_ediv_cancel _ h]
Negation of Scaled Division: $-(a \cdot b)/b = -a$ for $b \neq 0$
Informal description
For any integers $a$ and $b$ with $b \neq 0$, the Euclidean division of $-(a \cdot b)$ by $b$ equals $-a$, i.e., $-(a \cdot b)/b = -a$.
Int.neg_mul_ediv_cancel_left theorem
(a b : Int) (h : a ≠ 0) : -(a * b) / a = -b
Full source
@[deprecated mul_ediv_cancel (since := "2025-03-05")]
theorem neg_mul_ediv_cancel_left (a b : Int) (h : a ≠ 0) : -(a * b) / a = -b := by
  rw [neg_ediv_of_dvd (Int.dvd_mul_right a b), mul_ediv_cancel_left _ h]
Left Cancellation of Negated Multiplication and Division in Integers: $-(a \cdot b)/a = -b$ for $a \neq 0$
Informal description
For any integers $a$ and $b$ with $a \neq 0$, the Euclidean division of $-(a \cdot b)$ by $a$ equals $-b$, i.e., $-(a \cdot b)/a = -b$.
Int.ediv_one theorem
: ∀ a : Int, a / 1 = a
Full source
@[simp] theorem ediv_one : ∀ a : Int, a / 1 = a
  | (_:Nat) => congrArg Nat.cast (Nat.div_one _)
  | -[_+1]  => congrArg negSucc (Nat.div_one _)
Euclidean Division by One Identity: $a / 1 = a$
Informal description
For any integer $a$, the Euclidean division of $a$ by $1$ equals $a$, i.e., $a / 1 = a$.
Int.emod_one theorem
(a : Int) : a % 1 = 0
Full source
@[simp] theorem emod_one (a : Int) : a % 1 = 0 := by
  simp [emod_def, Int.one_mul, Int.sub_self]
Remainder Modulo One is Zero: $a \bmod 1 = 0$
Informal description
For any integer $a$, the remainder when $a$ is divided by $1$ is $0$, i.e., $a \bmod 1 = 0$.
Int.emod_sub_cancel theorem
(x y : Int) : (x - y) % y = x % y
Full source
@[simp]
theorem emod_sub_cancel (x y : Int) : (x - y) % y = x % y := by
  by_cases h : y = 0
  · simp [h]
  · simp only [Int.emod_def, Int.sub_ediv_of_dvd, Int.dvd_refl, Int.ediv_self h, Int.mul_sub]
    simp [Int.mul_one, Int.sub_sub, Int.add_comm y]
Modular Subtraction Cancellation: $(x - y) \% y = x \% y$
Informal description
For any integers $x$ and $y$, the remainder of $(x - y)$ modulo $y$ equals the remainder of $x$ modulo $y$, i.e., $(x - y) \% y = x \% y$.
Int.add_neg_emod_self theorem
(a b : Int) : (a + -b) % b = a % b
Full source
@[simp] theorem add_neg_emod_self (a b : Int) : (a + -b) % b = a % b := by
  rw [Int.add_neg_eq_sub, emod_sub_cancel]
Modular Addition of Negative: $(a - b) \bmod b = a \bmod b$
Informal description
For any integers $a$ and $b$, the remainder of $(a + (-b))$ modulo $b$ equals the remainder of $a$ modulo $b$, i.e., $(a - b) \bmod b = a \bmod b$.
Int.neg_add_emod_self theorem
(a b : Int) : (-a + b) % a = b % a
Full source
@[simp] theorem neg_add_emod_self (a b : Int) : (-a + b) % a = b % a := by
  rw [Int.add_comm, add_neg_emod_self]
Modular Addition of Negative: $(-a + b) \bmod a = b \bmod a$
Informal description
For any integers $a$ and $b$, the remainder of $(-a + b)$ modulo $a$ equals the remainder of $b$ modulo $a$, i.e., $(-a + b) \bmod a = b \bmod a$.
Int.dvd_sub_of_emod_eq theorem
{a b c : Int} (h : a % b = c) : b ∣ a - c
Full source
/-- If `a % b = c` then `b` divides `a - c`. -/
theorem dvd_sub_of_emod_eq {a b c : Int} (h : a % b = c) : b ∣ a - c := by
  have hx : (a % b) % b = c % b := by
    rw [h]
  rw [Int.emod_emod, ← emod_sub_cancel_right c, Int.sub_self, zero_emod] at hx
  exact dvd_of_emod_eq_zero hx
Divisibility from Remainder: $a \% b = c \implies b \mid (a - c)$
Informal description
For any integers $a$, $b$, and $c$, if the remainder of $a$ divided by $b$ equals $c$ (i.e., $a \% b = c$), then $b$ divides the difference $a - c$ (i.e., $b \mid (a - c)$).
Int.ediv_eq_iff_eq_mul_right theorem
{a b c : Int} (H : b ≠ 0) (H' : b ∣ a) : a / b = c ↔ a = b * c
Full source
protected theorem ediv_eq_iff_eq_mul_right {a b c : Int}
    (H : b ≠ 0) (H' : b ∣ a) : a / b = c ↔ a = b * c :=
  ⟨Int.eq_mul_of_ediv_eq_right H', Int.ediv_eq_of_eq_mul_right H⟩
Integer Division Equals Factor if and only if Dividend is Product: $a / b = c \leftrightarrow a = b \cdot c$ for $b \neq 0$ and $b \mid a$
Informal description
For any integers $a, b, c$ with $b \neq 0$ and $b$ divides $a$, the integer division $a / b$ equals $c$ if and only if $a = b \cdot c$.
Int.ediv_eq_iff_eq_mul_left theorem
{a b c : Int} (H : b ≠ 0) (H' : b ∣ a) : a / b = c ↔ a = c * b
Full source
protected theorem ediv_eq_iff_eq_mul_left {a b c : Int}
    (H : b ≠ 0) (H' : b ∣ a) : a / b = c ↔ a = c * b := by
  rw [Int.mul_comm]; exact Int.ediv_eq_iff_eq_mul_right H H'
Integer Division Equals Factor if and only if Dividend is Product (Left Version): $a / b = c \leftrightarrow a = c \cdot b$ for $b \neq 0$ and $b \mid a$
Informal description
For any integers $a, b, c$ with $b \neq 0$ and $b$ divides $a$, the integer division $a / b$ equals $c$ if and only if $a = c \cdot b$.
Int.sub_ediv_of_dvd_sub theorem
{a b c : Int} (hcab : c ∣ a - b) : (a - b) / c = a / c - b / c
Full source
theorem sub_ediv_of_dvd_sub {a b c : Int}
    (hcab : c ∣ a - b) : (a - b) / c = a / c - b / c := by
  rw [← Int.add_sub_cancel ((a - b) / c), ← Int.add_ediv_of_dvd_left hcab, Int.sub_add_cancel]
Integer Division of Difference under Divisibility: $(a - b)/c = a/c - b/c$ when $c \mid (a - b)$
Informal description
For any integers $a$, $b$, and $c$ such that $c$ divides $a - b$, the integer division $(a - b)/c$ equals $a/c - b/c$.
Int.ediv_left_inj theorem
{a b d : Int} (hda : d ∣ a) (hdb : d ∣ b) : a / d = b / d ↔ a = b
Full source
@[simp] protected theorem ediv_left_inj {a b d : Int}
    (hda : d ∣ a) (hdb : d ∣ b) : a / d = b / d ↔ a = b := by
  refine ⟨fun h => ?_, congrArg (ediv · d)⟩
  rw [← Int.mul_ediv_cancel' hda, ← Int.mul_ediv_cancel' hdb, h]
Integer Division Equality under Divisibility: $a/d = b/d \leftrightarrow a = b$ when $d \mid a$ and $d \mid b$
Informal description
For any integers $a$, $b$, and $d$ such that $d$ divides both $a$ and $b$, the equality of integer divisions $a / d = b / d$ holds if and only if $a = b$.
Int.ediv_sign theorem
: ∀ a b, a / sign b = a * sign b
Full source
theorem ediv_sign : ∀ a b, a / sign b = a * sign b
  | _, succ _ => by simp [sign, Int.mul_one]
  | _, 0 => by simp [sign, Int.mul_zero]
  | _, -[_+1] => by simp [sign, Int.mul_neg, Int.mul_one]
Integer Division by Sign Equals Multiplication by Sign: $a / \text{sign}(b) = a \cdot \text{sign}(b)$
Informal description
For any integers $a$ and $b$, the integer division of $a$ by the sign of $b$ equals the product of $a$ with the sign of $b$, i.e., $a / \text{sign}(b) = a \cdot \text{sign}(b)$.
Int.sign_eq_ediv_natAbs theorem
(a : Int) : sign a = a / (natAbs a)
Full source
protected theorem sign_eq_ediv_natAbs (a : Int) : sign a = a / (natAbs a) :=
  if az : a = 0 then by simp [az] else
    (Int.ediv_eq_of_eq_mul_left (ofNat_ne_zero.2 <| natAbs_ne_zero.2 az)
      (sign_mul_natAbs _).symm).symm
Sign as Division by Absolute Value: $\text{sign}(a) = a / \text{natAbs}(a)$
Informal description
For any integer $a$, the sign of $a$ equals the integer division of $a$ by its natural absolute value, i.e., $\text{sign}(a) = a / \text{natAbs}(a)$.
Int.ediv_mul_le theorem
(a : Int) {b : Int} (H : b ≠ 0) : a / b * b ≤ a
Full source
protected theorem ediv_mul_le (a : Int) {b : Int} (H : b ≠ 0) : a / b * b ≤ a :=
  Int.le_of_sub_nonneg <| by rw [Int.mul_comm, ← emod_def]; apply emod_nonneg _ H
Integer Division Product Bound: $(a / b) \cdot b \leq a$ for $b \neq 0$
Informal description
For any integer $a$ and any nonzero integer $b$, the product of the integer division $a / b$ with $b$ is less than or equal to $a$, i.e., $(a / b) \cdot b \leq a$.
Int.le_of_mul_le_mul_left theorem
{a b c : Int} (w : a * b ≤ a * c) (h : 0 < a) : b ≤ c
Full source
theorem le_of_mul_le_mul_left {a b c : Int} (w : a * b ≤ a * c) (h : 0 < a) : b ≤ c := by
  have w := Int.sub_nonneg_of_le w
  rw [← Int.mul_sub] at w
  have w := Int.ediv_nonneg w (Int.le_of_lt h)
  rw [Int.mul_ediv_cancel_left _ (Int.ne_of_gt h)] at w
  exact Int.le_of_sub_nonneg w
Left Cancellation of Multiplication for Positive Integers: $a > 0 \land a \cdot b \leq a \cdot c \implies b \leq c$
Informal description
For any integers $a$, $b$, and $c$ such that $a > 0$, if $a \cdot b \leq a \cdot c$, then $b \leq c$.
Int.le_of_mul_le_mul_right theorem
{a b c : Int} (w : b * a ≤ c * a) (h : 0 < a) : b ≤ c
Full source
theorem le_of_mul_le_mul_right {a b c : Int} (w : b * a ≤ c * a) (h : 0 < a) : b ≤ c := by
  rw [Int.mul_comm b, Int.mul_comm c] at w
  exact le_of_mul_le_mul_left w h
Right Cancellation of Multiplication for Positive Integers: $a > 0 \land b \cdot a \leq c \cdot a \implies b \leq c$
Informal description
For any integers $a$, $b$, and $c$ such that $a > 0$, if $b \cdot a \leq c \cdot a$, then $b \leq c$.
Int.ediv_le_of_le_mul theorem
{a b c : Int} (H : 0 < c) (H' : a ≤ b * c) : a / c ≤ b
Full source
protected theorem ediv_le_of_le_mul {a b c : Int} (H : 0 < c) (H' : a ≤ b * c) : a / c ≤ b :=
  le_of_mul_le_mul_right (Int.le_trans (Int.ediv_mul_le _ (Int.ne_of_gt H)) H') H
Integer Division Bound: $a \leq b \cdot c \implies a / c \leq b$ for $c > 0$
Informal description
For any integers $a$, $b$, and $c$ such that $c > 0$, if $a \leq b \cdot c$, then the integer division $a / c$ satisfies $a / c \leq b$.
Int.ediv_nonpos_of_nonpos_of_neg theorem
{n s : Int} (h : n ≤ 0) (h2 : 0 < s) : n / s ≤ 0
Full source
protected theorem ediv_nonpos_of_nonpos_of_neg {n s : Int} (h : n ≤ 0) (h2 : 0 < s) : n / s ≤ 0 :=
  Int.ediv_le_of_le_mul h2 (by simp [h])
Nonpositive Integer Division by Positive Integer is Nonpositive
Informal description
For any integers $n$ and $s$ such that $n \leq 0$ and $s > 0$, the integer division $n / s$ satisfies $n / s \leq 0$.
Int.mul_lt_of_lt_ediv theorem
{a b c : Int} (H : 0 < c) (H3 : a < b / c) : a * c < b
Full source
protected theorem mul_lt_of_lt_ediv {a b c : Int} (H : 0 < c) (H3 : a < b / c) : a * c < b :=
  Int.lt_of_not_ge <| mt (Int.ediv_le_of_le_mul H) (Int.not_le_of_gt H3)
Integer Multiplication Bound: $a < b / c \implies a \cdot c < b$ for $c > 0$
Informal description
For any integers $a$, $b$, and $c$ with $c > 0$, if $a$ is strictly less than the integer division $b / c$, then $a \cdot c$ is strictly less than $b$.
Int.mul_le_of_le_ediv theorem
{a b c : Int} (H1 : 0 < c) (H2 : a ≤ b / c) : a * c ≤ b
Full source
protected theorem mul_le_of_le_ediv {a b c : Int} (H1 : 0 < c) (H2 : a ≤ b / c) : a * c ≤ b :=
  Int.le_trans (Int.mul_le_mul_of_nonneg_right H2 (Int.le_of_lt H1))
    (Int.ediv_mul_le _ (Int.ne_of_gt H1))
Integer Multiplication Bound: $a \leq b / c \implies a \cdot c \leq b$ for $c > 0$
Informal description
For any integers $a$, $b$, and $c$ with $c > 0$, if $a \leq b / c$, then $a \cdot c \leq b$.
Int.ediv_lt_of_lt_mul theorem
{a b c : Int} (H : 0 < c) (H' : a < b * c) : a / c < b
Full source
protected theorem ediv_lt_of_lt_mul {a b c : Int} (H : 0 < c) (H' : a < b * c) : a / c < b :=
  Int.lt_of_not_ge <| mt (Int.mul_le_of_le_ediv H) (Int.not_le_of_gt H')
Integer Division Bound: $a < b \cdot c \implies a / c < b$ for $c > 0$
Informal description
For any integers $a$, $b$, and $c$ with $c > 0$, if $a < b \cdot c$, then the integer division $a / c$ is strictly less than $b$.
Int.lt_of_mul_lt_mul_left theorem
{a b c : Int} (w : a * b < a * c) (h : 0 ≤ a) : b < c
Full source
theorem lt_of_mul_lt_mul_left {a b c : Int} (w : a * b < a * c) (h : 0 ≤ a) : b < c := by
  rcases Int.lt_trichotomy b c with lt | rfl | gt
  · exact lt
  · exact False.elim (Int.lt_irrefl _ w)
  · rcases Int.lt_trichotomy a 0 with a_lt | rfl | a_gt
    · exact False.elim (Int.lt_irrefl _ (Int.lt_of_lt_of_le a_lt h))
    · exact False.elim (Int.lt_irrefl b (by simp at w))
    · have := le_of_mul_le_mul_left (Int.le_of_lt w) a_gt
      exact False.elim (Int.lt_irrefl _ (Int.lt_of_lt_of_le gt this))
Left Multiplication Preserves Strict Inequality for Non-Negative Integers: $a \geq 0 \land a \cdot b < a \cdot c \implies b < c$
Informal description
For any integers $a$, $b$, and $c$ such that $a \geq 0$, if $a \cdot b < a \cdot c$, then $b < c$.
Int.lt_of_mul_lt_mul_right theorem
{a b c : Int} (w : b * a < c * a) (h : 0 ≤ a) : b < c
Full source
theorem lt_of_mul_lt_mul_right {a b c : Int} (w : b * a < c * a) (h : 0 ≤ a) : b < c := by
  rw [Int.mul_comm b, Int.mul_comm c] at w
  exact lt_of_mul_lt_mul_left w h
Right Multiplication Preserves Strict Inequality for Non-Negative Integers: $b \cdot a < c \cdot a \implies b < c$ when $a \geq 0$
Informal description
For any integers $a$, $b$, and $c$ such that $a \geq 0$, if $b \cdot a < c \cdot a$, then $b < c$.
Int.le_ediv_of_mul_le theorem
{a b c : Int} (H1 : 0 < c) (H2 : a * c ≤ b) : a ≤ b / c
Full source
protected theorem le_ediv_of_mul_le {a b c : Int} (H1 : 0 < c) (H2 : a * c ≤ b) : a ≤ b / c :=
  le_of_lt_add_one <|
    lt_of_mul_lt_mul_right (Int.lt_of_le_of_lt H2 (lt_ediv_add_one_mul_self _ H1)) (Int.le_of_lt H1)
Floor Division Lower Bound: $a \cdot c \leq b \implies a \leq \left\lfloor \frac{b}{c} \right\rfloor$ for $c > 0$
Informal description
For any integers $a$, $b$, and $c$ with $c > 0$, if $a \cdot c \leq b$, then $a \leq \left\lfloor \frac{b}{c} \right\rfloor$.
Int.le_ediv_iff_mul_le theorem
{a b c : Int} (H : 0 < c) : a ≤ b / c ↔ a * c ≤ b
Full source
protected theorem le_ediv_iff_mul_le {a b c : Int} (H : 0 < c) : a ≤ b / c ↔ a * c ≤ b :=
  ⟨Int.mul_le_of_le_ediv H, Int.le_ediv_of_mul_le H⟩
Floor Division Inequality: $a \leq \left\lfloor \frac{b}{c} \right\rfloor \leftrightarrow a \cdot c \leq b$ for $c > 0$
Informal description
For any integers $a$, $b$, and $c$ with $c > 0$, the inequality $a \leq \left\lfloor \frac{b}{c} \right\rfloor$ holds if and only if $a \cdot c \leq b$.
Int.lt_mul_of_ediv_lt theorem
{a b c : Int} (H1 : 0 < c) (H2 : a / c < b) : a < b * c
Full source
protected theorem lt_mul_of_ediv_lt {a b c : Int} (H1 : 0 < c) (H2 : a / c < b) : a < b * c :=
  Int.lt_of_not_ge <| mt (Int.le_ediv_of_mul_le H1) (Int.not_le_of_gt H2)
Floor Division Implies Strict Upper Bound: $\lfloor a / c \rfloor < b \implies a < b \cdot c$ for $c > 0$
Informal description
For any integers $a$, $b$, and $c$ with $c > 0$, if the floor division of $a$ by $c$ is less than $b$ (i.e., $\lfloor a / c \rfloor < b$), then $a$ is strictly less than $b \cdot c$ (i.e., $a < b \cdot c$).
Int.le_mul_of_ediv_le theorem
{a b c : Int} (H1 : 0 ≤ b) (H2 : b ∣ a) (H3 : a / b ≤ c) : a ≤ c * b
Full source
protected theorem le_mul_of_ediv_le {a b c : Int} (H1 : 0 ≤ b) (H2 : b ∣ a) (H3 : a / b ≤ c) :
    a ≤ c * b := by
  rw [← Int.ediv_mul_cancel H2]; exact Int.mul_le_mul_of_nonneg_right H3 H1
Integer Division Implies Multiplication Inequality: $b \geq 0 \land b \mid a \land a/b \leq c \implies a \leq c \cdot b$
Informal description
For any integers $a$, $b$, and $c$ such that $0 \leq b$, $b$ divides $a$, and $a / b \leq c$, it follows that $a \leq c \cdot b$.
Int.lt_ediv_of_mul_lt theorem
{a b c : Int} (H1 : 0 ≤ b) (H2 : b ∣ c) (H3 : a * b < c) : a < c / b
Full source
protected theorem lt_ediv_of_mul_lt {a b c : Int} (H1 : 0 ≤ b) (H2 : b ∣ c) (H3 : a * b < c) :
    a < c / b :=
  Int.lt_of_not_ge <| mt (Int.le_mul_of_ediv_le H1 H2) (Int.not_le_of_gt H3)
Strict Inequality Preservation under Integer Division: $0 \leq b \land b \mid c \land a \cdot b < c \implies a < c / b$
Informal description
For any integers $a$, $b$, and $c$ such that $0 \leq b$, $b$ divides $c$, and $a \cdot b < c$, it follows that $a < c / b$.
Int.lt_ediv_iff_mul_lt theorem
{a b : Int} {c : Int} (H : 0 < c) (H' : c ∣ b) : a < b / c ↔ a * c < b
Full source
protected theorem lt_ediv_iff_mul_lt {a b : Int} {c : Int} (H : 0 < c) (H' : c ∣ b) :
    a < b / c ↔ a * c < b :=
  ⟨Int.mul_lt_of_lt_ediv H, Int.lt_ediv_of_mul_lt (Int.le_of_lt H) H'⟩
Equivalence of Strict Inequality and Multiplication Under Integer Division: $a < b/c \leftrightarrow a \cdot c < b$ for $c > 0$ and $c \mid b$
Informal description
For any integers $a$, $b$, and $c$ with $c > 0$ and $c$ dividing $b$, the inequality $a < b / c$ holds if and only if $a \cdot c < b$.
Int.ediv_pos_of_pos_of_dvd theorem
{a b : Int} (H1 : 0 < a) (H2 : 0 ≤ b) (H3 : b ∣ a) : 0 < a / b
Full source
theorem ediv_pos_of_pos_of_dvd {a b : Int} (H1 : 0 < a) (H2 : 0 ≤ b) (H3 : b ∣ a) : 0 < a / b :=
  Int.lt_ediv_of_mul_lt H2 H3 (by rwa [Int.zero_mul])
Positivity of Integer Division: $a > 0 \land b \geq 0 \land b \mid a \implies a / b > 0$
Informal description
For any integers $a$ and $b$ such that $a > 0$, $b \geq 0$, and $b$ divides $a$, the integer division $a / b$ is strictly positive, i.e., $a / b > 0$.
Int.ediv_eq_ediv_of_mul_eq_mul theorem
{a b c d : Int} (H2 : d ∣ c) (H3 : b ≠ 0) (H4 : d ≠ 0) (H5 : a * d = b * c) : a / b = c / d
Full source
theorem ediv_eq_ediv_of_mul_eq_mul {a b c d : Int}
    (H2 : d ∣ c) (H3 : b ≠ 0) (H4 : d ≠ 0) (H5 : a * d = b * c) : a / b = c / d :=
  Int.ediv_eq_of_eq_mul_right H3 <| by
    rw [← Int.mul_ediv_assoc _ H2]; exact (Int.ediv_eq_of_eq_mul_left H4 H5.symm).symm
Equality of Integer Divisions under Cross-Multiplication: $a \cdot d = b \cdot c \Rightarrow a/b = c/d$ for $d \mid c$, $b, d \neq 0$
Informal description
For any integers $a, b, c, d$ such that $d$ divides $c$, $b \neq 0$, $d \neq 0$, and $a \cdot d = b \cdot c$, the integer divisions satisfy $a / b = c / d$.
Int.ediv_eq_iff_of_pos theorem
{k x y : Int} (h : 0 < k) : x / k = y ↔ y * k ≤ x ∧ x < y * k + k
Full source
theorem ediv_eq_iff_of_pos {k x y : Int} (h : 0 < k) : x / k = y ↔ y * k ≤ x ∧ x < y * k + k := by
  rw [Int.eq_iff_le_and_ge, and_comm, Int.le_ediv_iff_mul_le h, Int.ediv_le_iff_le_mul h]
Characterization of Integer Division Equality: $x / k = y \leftrightarrow yk \leq x < yk + k$ for $k > 0$
Informal description
For any integers $x$, $y$, and $k$ with $k > 0$, the equality $x / k = y$ holds if and only if $y \cdot k \leq x$ and $x < y \cdot k + k$.
Int.add_ediv_of_pos theorem
{a b c : Int} (h : 0 < c) : (a + b) / c = a / c + b / c + if c ≤ a % c + b % c then 1 else 0
Full source
theorem add_ediv_of_pos {a b c : Int} (h : 0 < c) :
    (a + b) / c = a / c + b / c + if c ≤ a % c + b % c then 1 else 0 := by
  have h' : c ≠ 0 := by omega
  conv => lhs; rw [← Int.ediv_add_emod a c]
  rw [Int.add_assoc, Int.mul_add_ediv_left _ _ h']
  conv => lhs; rw [← Int.ediv_add_emod b c]
  rw [Int.add_comm (a % c), Int.add_assoc, Int.mul_add_ediv_left _ _ h',
    ← Int.add_assoc, Int.add_comm (b % c)]
  congr
  rw [Int.ediv_eq_iff_of_pos h]
  have := emod_lt_of_pos a h
  have := emod_lt_of_pos b h
  constructor
  · have := emod_nonneg a h'
    have := emod_nonneg b h'
    split <;> · simp; omega
  · split <;> · simp; omega
Integer Division of Sum Formula for Positive Divisor: $\frac{a + b}{c} = \frac{a}{c} + \frac{b}{c} + \text{correction term}$
Informal description
For any integers $a$, $b$, and $c$ with $c > 0$, the integer division of the sum $a + b$ by $c$ satisfies: \[ \frac{a + b}{c} = \frac{a}{c} + \frac{b}{c} + \begin{cases} 1 & \text{if } c \leq (a \bmod c) + (b \bmod c), \\ 0 & \text{otherwise.} \end{cases} \]
Int.add_ediv theorem
{a b c : Int} (h : c ≠ 0) : (a + b) / c = a / c + b / c + if c.natAbs ≤ a % c + b % c then c.sign else 0
Full source
theorem add_ediv {a b c : Int} (h : c ≠ 0) :
    (a + b) / c = a / c + b / c + if c.natAbs ≤ a % c + b % c then c.sign else 0 := by
  induction c using wlog_sign
  case inv => simp; omega
  rename_i c
  norm_cast at h
  have : 0 < (c : Int) := by omega
  simp [sign_ofNat_of_nonzero h, add_ediv_of_pos this]
Integer Division of Sum Formula: $\frac{a + b}{c} = \frac{a}{c} + \frac{b}{c} + \text{correction term}$
Informal description
For any integers $a$, $b$, and $c$ with $c \neq 0$, the integer division of the sum $a + b$ by $c$ satisfies: \[ \frac{a + b}{c} = \frac{a}{c} + \frac{b}{c} + \begin{cases} \text{sign}(c) & \text{if } |c| \leq (a \bmod c) + (b \bmod c), \\ 0 & \text{otherwise.} \end{cases} \]
Int.ediv_le_ediv theorem
{a b c : Int} (H : 0 < c) (H' : a ≤ b) : a / c ≤ b / c
Full source
protected theorem ediv_le_ediv {a b c : Int} (H : 0 < c) (H' : a ≤ b) : a / c ≤ b / c :=
  Int.le_ediv_of_mul_le H (Int.le_trans (Int.ediv_mul_le _ (Int.ne_of_gt H)) H')
Monotonicity of Integer Division: $a \leq b \implies \left\lfloor \frac{a}{c} \right\rfloor \leq \left\lfloor \frac{b}{c} \right\rfloor$ for $c > 0$
Informal description
For any integers $a$, $b$, and $c$ with $c > 0$, if $a \leq b$, then the integer division $a / c$ is less than or equal to $b / c$.
Int.tdiv_neg theorem
: ∀ a b : Int, a.tdiv (-b) = -(a.tdiv b)
Full source
@[simp] protected theorem tdiv_neg : ∀ a b : Int, a.tdiv (-b) = -(a.tdiv b)
  | ofNat m, 0 => show ofNat (m / 0) = -↑(m / 0) by rw [Nat.div_zero]; rfl
  | ofNat _, -[_+1] | -[_+1], succ _ => (Int.neg_neg _).symm
  | ofNat _, succ _ | -[_+1], 0 => by simp [Int.tdiv, Int.neg_zero, ← Int.negSucc_eq]
  | -[_+1], -[_+1] => by simp only [tdiv, neg_negSucc]
Negation of Divisor in Truncating Division: $a \operatorname{tdiv} (-b) = -(a \operatorname{tdiv} b)$
Informal description
For any integers $a$ and $b$, the truncating division of $a$ by $-b$ equals the negation of the truncating division of $a$ by $b$, i.e., $a \operatorname{tdiv} (-b) = -(a \operatorname{tdiv} b)$.
Int.mul_tdiv_cancel theorem
(a : Int) {b : Int} (H : b ≠ 0) : (a * b).tdiv b = a
Full source
@[simp] theorem mul_tdiv_cancel (a : Int) {b : Int} (H : b ≠ 0) : (a * b).tdiv b = a := by
  rw [tdiv_eq_ediv_of_dvd (Int.dvd_mul_left a b), mul_ediv_cancel _ H]
Cancellation of Multiplication and Truncating Division in Integers: $(a \cdot b) \operatorname{tdiv} b = a$ for $b \neq 0$
Informal description
For any integers $a$ and $b$ with $b \neq 0$, the truncating division of $a \cdot b$ by $b$ equals $a$, i.e., $(a \cdot b) \operatorname{tdiv} b = a$.
Int.mul_tdiv_cancel_left theorem
(b : Int) (H : a ≠ 0) : (a * b).tdiv a = b
Full source
@[simp] theorem mul_tdiv_cancel_left (b : Int) (H : a ≠ 0) : (a * b).tdiv a = b :=
  Int.mul_comm .. ▸ Int.mul_tdiv_cancel _ H
Left Cancellation of Multiplication and Truncating Division in Integers: $(a \cdot b) \operatorname{tdiv} a = b$ for $a \neq 0$
Informal description
For any integers $a$ and $b$ with $a \neq 0$, the truncating division of $a \cdot b$ by $a$ equals $b$, i.e., $(a \cdot b) \operatorname{tdiv} a = b$.
Int.tdiv_nonneg theorem
{a b : Int} (Ha : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a.tdiv b
Full source
protected theorem tdiv_nonneg {a b : Int} (Ha : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a.tdiv b :=
  match a, b, eq_ofNat_of_zero_le Ha, eq_ofNat_of_zero_le Hb with
  | _, _, ⟨_, rfl⟩, ⟨_, rfl⟩ => ofNat_zero_le _
Nonnegativity of Truncating Division for Nonnegative Integers
Informal description
For any integers $a$ and $b$ such that $0 \leq a$ and $0 \leq b$, the truncating division $a \operatorname{tdiv} b$ is nonnegative, i.e., $0 \leq a \operatorname{tdiv} b$.
Int.tdiv_nonneg_of_nonpos_of_nonpos theorem
{a b : Int} (Ha : a ≤ 0) (Hb : b ≤ 0) : 0 ≤ a.tdiv b
Full source
theorem tdiv_nonneg_of_nonpos_of_nonpos {a b : Int} (Ha : a ≤ 0) (Hb : b ≤ 0) : 0 ≤ a.tdiv b := by
  rw [tdiv_eq_ediv]
  split <;> rename_i h
  · simpa using ediv_nonneg_of_nonpos_of_nonpos Ha Hb
  · simp at h
    by_cases h' : b = 0
    · subst h'
      simp
    · replace h' : b < 0 := by omega
      rw [sign_eq_neg_one_of_neg h']
      have : 0 < a / b := by
        by_cases h'' : a = 0
        · subst h''
          simp at h
        · replace h'' : a < 0 := by omega
          exact ediv_pos_of_neg_of_neg h'' h'
      omega
Nonnegativity of Truncating Division for Nonpositive Integers: $0 \leq a \operatorname{tdiv} b$ when $a, b \leq 0$
Informal description
For any integers $a$ and $b$ such that $a \leq 0$ and $b \leq 0$, the truncating division $a \operatorname{tdiv} b$ is nonnegative, i.e., $0 \leq a \operatorname{tdiv} b$.
Int.tdiv_nonpos_of_nonneg_of_nonpos theorem
{a b : Int} (Ha : 0 ≤ a) (Hb : b ≤ 0) : a.tdiv b ≤ 0
Full source
protected theorem tdiv_nonpos_of_nonneg_of_nonpos {a b : Int} (Ha : 0 ≤ a) (Hb : b ≤ 0) : a.tdiv b ≤ 0 :=
  Int.nonpos_of_neg_nonneg <| Int.tdiv_neg .. ▸ Int.tdiv_nonneg Ha (Int.neg_nonneg_of_nonpos Hb)
Nonpositivity of Truncating Division for Nonnegative Dividend and Nonpositive Divisor
Informal description
For any integers $a$ and $b$ such that $0 \leq a$ and $b \leq 0$, the truncating division $a \operatorname{tdiv} b$ is nonpositive, i.e., $a \operatorname{tdiv} b \leq 0$.
Int.tdiv_nonpos abbrev
Full source
@[deprecated Int.tdiv_nonpos_of_nonneg_of_nonpos (since := "2025-03-04")]
abbrev tdiv_nonpos := @Int.tdiv_nonpos_of_nonneg_of_nonpos
Nonpositivity of Truncating Division for Nonpositive Dividend and Positive Divisor
Informal description
For any integers $a$ and $b$ such that $a \leq 0$ and $0 < b$, the truncating division $a \operatorname{tdiv} b$ is nonpositive, i.e., $a \operatorname{tdiv} b \leq 0$.
Int.tdiv_eq_zero_of_lt theorem
{a b : Int} (H1 : 0 ≤ a) (H2 : a < b) : a.tdiv b = 0
Full source
theorem tdiv_eq_zero_of_lt {a b : Int} (H1 : 0 ≤ a) (H2 : a < b) : a.tdiv b = 0 :=
  match a, b, eq_ofNat_of_zero_le H1, eq_succ_of_zero_lt (Int.lt_of_le_of_lt H1 H2) with
  | _, _, ⟨_, rfl⟩, ⟨_, rfl⟩ => congrArg Nat.cast <| Nat.div_eq_of_lt <| ofNat_lt.1 H2
Truncating Division Yields Zero for Nonnegative Dividend Less Than Divisor: $0 \leq a < b \Rightarrow a \operatorname{tdiv} b = 0$
Informal description
For any integers $a$ and $b$ such that $0 \leq a$ and $a < b$, the truncating division of $a$ by $b$ equals zero, i.e., $a \operatorname{tdiv} b = 0$.
Int.mul_tdiv_mul_of_pos theorem
{a : Int} (b c : Int) (H : 0 < a) : (a * b).tdiv (a * c) = b.tdiv c
Full source
@[simp] theorem mul_tdiv_mul_of_pos {a : Int}
    (b c : Int) (H : 0 < a) : (a * b).tdiv (a * c) = b.tdiv c := by
  rw [tdiv_eq_ediv, mul_ediv_mul_of_pos _ _ H, tdiv_eq_ediv]
  simp only [sign_mul]
  by_cases h : 0 ≤ b
  · rw [if_pos, if_pos (.inl h)]
    left
    exact Int.mul_nonneg (Int.le_of_lt H) h
  · have H' : a ≠ 0 := by omega
    simp only [Int.mul_dvd_mul_iff_left H']
    by_cases h' : c ∣ b
    · simp [h']
    · rw [if_neg, if_neg]
      · simp [sign_eq_one_of_pos H]
      · simp [h']; omega
      · simp_all only [Int.not_le, ne_eq, or_false]
        exact Int.mul_neg_of_pos_of_neg H h
Cancellation of Positive Factor in Truncating Division: $(a \cdot b) \operatorname{tdiv} (a \cdot c) = b \operatorname{tdiv} c$ for $a > 0$
Informal description
For any positive integer $a$ and integers $b, c$, the truncating division of $a \cdot b$ by $a \cdot c$ equals the truncating division of $b$ by $c$, i.e., $(a \cdot b) \operatorname{tdiv} (a \cdot c) = b \operatorname{tdiv} c$.
Int.mul_tdiv_mul_of_pos_left theorem
(a : Int) {b : Int} (c : Int) (H : 0 < b) : (a * b).tdiv (c * b) = a.tdiv c
Full source
@[simp] theorem mul_tdiv_mul_of_pos_left
    (a : Int) {b : Int} (c : Int) (H : 0 < b) : (a * b).tdiv (c * b) = a.tdiv c := by
  rw [Int.mul_comm, Int.mul_comm c, mul_tdiv_mul_of_pos _ _ H]
Left Cancellation of Positive Factor in Truncating Division: $(a \cdot b) \operatorname{tdiv} (c \cdot b) = a \operatorname{tdiv} c$ for $b > 0$
Informal description
For any integers $a$, $b$, and $c$ with $b > 0$, the truncating division of $a \cdot b$ by $c \cdot b$ equals the truncating division of $a$ by $c$, i.e., $(a \cdot b) \operatorname{tdiv} (c \cdot b) = a \operatorname{tdiv} c$.
Int.tdiv_eq_of_eq_mul_right theorem
{a b c : Int} (H1 : b ≠ 0) (H2 : a = b * c) : a.tdiv b = c
Full source
protected theorem tdiv_eq_of_eq_mul_right {a b c : Int}
    (H1 : b ≠ 0) (H2 : a = b * c) : a.tdiv b = c := by rw [H2, Int.mul_tdiv_cancel_left _ H1]
Truncating Division of Product Equals Factor: $a = b \cdot c \implies a \operatorname{tdiv} b = c$ for $b \neq 0$
Informal description
For any integers $a$, $b$, and $c$ with $b \neq 0$, if $a = b \cdot c$, then the truncating division of $a$ by $b$ equals $c$, i.e., $a \operatorname{tdiv} b = c$.
Int.tdiv_eq_of_eq_mul_left theorem
{a b c : Int} (H1 : b ≠ 0) (H2 : a = c * b) : a.tdiv b = c
Full source
protected theorem tdiv_eq_of_eq_mul_left {a b c : Int}
    (H1 : b ≠ 0) (H2 : a = c * b) : a.tdiv b = c :=
  Int.tdiv_eq_of_eq_mul_right H1 (by rw [Int.mul_comm, H2])
Truncating Division of Product Equals Factor (Left Version): $a = c \cdot b \implies a \operatorname{tdiv} b = c$ for $b \neq 0$
Informal description
For any integers $a$, $b$, and $c$ with $b \neq 0$, if $a = c \cdot b$, then the truncating division of $a$ by $b$ equals $c$, i.e., $a \operatorname{tdiv} b = c$.
Int.tdiv_self theorem
{a : Int} (H : a ≠ 0) : a.tdiv a = 1
Full source
@[simp] protected theorem tdiv_self {a : Int} (H : a ≠ 0) : a.tdiv a = 1 := by
  have := Int.mul_tdiv_cancel 1 H; rwa [Int.one_mul] at this
Truncating Division Identity: $a \operatorname{tdiv} a = 1$ for $a \neq 0$
Informal description
For any nonzero integer $a$, the truncating division of $a$ by itself equals $1$, i.e., $a \operatorname{tdiv} a = 1$.
Int.neg_tdiv theorem
: ∀ a b : Int, (-a).tdiv b = -(a.tdiv b)
Full source
@[simp] protected theorem neg_tdiv : ∀ a b : Int, (-a).tdiv b = -(a.tdiv b)
  | 0, n => by simp [Int.neg_zero]
  | succ _, (n:Nat) => by simp [tdiv, ← Int.negSucc_eq]
  | -[_+1], 0 | -[_+1], -[_+1] => by
    simp only [tdiv, neg_negSucc, ← Int.natCast_succ, Int.neg_neg]
  | succ _, -[_+1] | -[_+1], succ _ => (Int.neg_neg _).symm
Negation and Truncating Division: $(-a) \ \mathrm{tdiv} \ b = -(a \ \mathrm{tdiv} \ b)$
Informal description
For any integers $a$ and $b$, the truncating division of $-a$ by $b$ equals the negation of the truncating division of $a$ by $b$, i.e., $(-a) \ \mathrm{tdiv} \ b = -(a \ \mathrm{tdiv} \ b)$.
Int.neg_tdiv_neg theorem
(a b : Int) : (-a).tdiv (-b) = a.tdiv b
Full source
protected theorem neg_tdiv_neg (a b : Int) : (-a).tdiv (-b) = a.tdiv b := by
  simp [Int.tdiv_neg, Int.neg_tdiv, Int.neg_neg]
Double Negation Identity for Truncating Division: $(-a) \operatorname{tdiv} (-b) = a \operatorname{tdiv} b$
Informal description
For any integers $a$ and $b$, the truncating division of $-a$ by $-b$ equals the truncating division of $a$ by $b$, i.e., $(-a) \operatorname{tdiv} (-b) = a \operatorname{tdiv} b$.
Int.sign_tdiv theorem
(a b : Int) : sign (a.tdiv b) = if natAbs a < natAbs b then 0 else sign a * sign b
Full source
theorem sign_tdiv (a b : Int) : sign (a.tdiv b) = if natAbs a < natAbs b then 0 else sign a * sign b := by
  induction b using wlog_sign
  case inv => simp; split <;> simp [Int.mul_neg]
  case w b =>
    induction a using wlog_sign
    case inv => simp; split <;> simp [Int.neg_mul]
    case w a =>
      rw [tdiv_eq_ediv_of_nonneg (by simp), sign_ediv]
      simp
Sign of Truncating Division: $\text{sign}(a \operatorname{tdiv} b) = \text{sign}(a) \cdot \text{sign}(b)$ unless $|a| < |b|$
Informal description
For any integers $a$ and $b$, the sign of the truncating division $a \operatorname{tdiv} b$ is given by: \[ \text{sign}(a \operatorname{tdiv} b) = \begin{cases} 0 & \text{if } |a| < |b| \\ \text{sign}(a) \cdot \text{sign}(b) & \text{otherwise} \end{cases} \]
Int.natAbs_tdiv theorem
(a b : Int) : natAbs (a.tdiv b) = (natAbs a).div (natAbs b)
Full source
@[simp] theorem natAbs_tdiv (a b : Int) : natAbs (a.tdiv b) = (natAbs a).div (natAbs b) :=
  match a, b, Int.eq_nat_or_neg a, Int.eq_nat_or_neg b with
  | _, _, ⟨_, .inl rfl⟩, ⟨_, .inl rfl⟩ => rfl
  | _, _, ⟨_, .inl rfl⟩, ⟨_, .inr rfl⟩ => by rw [Int.tdiv_neg, natAbs_neg, natAbs_neg]; rfl
  | _, _, ⟨_, .inr rfl⟩, ⟨_, .inl rfl⟩ => by rw [Int.neg_tdiv, natAbs_neg, natAbs_neg]; rfl
  | _, _, ⟨_, .inr rfl⟩, ⟨_, .inr rfl⟩ => by rw [Int.neg_tdiv_neg, natAbs_neg, natAbs_neg]; rfl
Absolute Value of Truncating Division Equals Floor Division of Absolute Values
Informal description
For any integers $a$ and $b$, the absolute value of the truncating division $a \operatorname{tdiv} b$ (as a natural number) equals the floor division of the absolute values of $a$ and $b$, i.e., $\text{natAbs}(a \operatorname{tdiv} b) = \text{natAbs}(a) / \text{natAbs}(b)$.
Int.ofNat_tmod theorem
(m n : Nat) : (↑(m % n) : Int) = tmod m n
Full source
theorem ofNat_tmod (m n : Nat) : (↑(m % n) : Int) = tmod m n := rfl
Natural Number Modulo Coerced to Integer Equals Truncating Modulo
Informal description
For any natural numbers $m$ and $n$, the canonical image of $m \% n$ (the remainder of $m$ divided by $n$) in the integers $\mathbb{Z}$ is equal to the truncating modulo operation $\text{tmod}$ applied to $m$ and $n$.
Int.tmod_nonneg theorem
: ∀ {a : Int} (b : Int), 0 ≤ a → 0 ≤ tmod a b
Full source
theorem tmod_nonneg : ∀ {a : Int} (b : Int), 0 ≤ a → 0 ≤ tmod a b
  | ofNat _, -[_+1], _ | ofNat _, ofNat _, _ => ofNat_nonneg _
Nonnegativity of Truncating Modulo for Nonnegative Dividend
Informal description
For any integer $a$ and any integer $b$, if $a$ is nonnegative (i.e., $0 \leq a$), then the truncating modulo operation $\text{tmod}$ applied to $a$ and $b$ yields a nonnegative result, i.e., $0 \leq \text{tmod } a b$.
Int.tmod_neg theorem
(a b : Int) : tmod a (-b) = tmod a b
Full source
@[simp] theorem tmod_neg (a b : Int) : tmod a (-b) = tmod a b := by
  rw [tmod_def, tmod_def, Int.tdiv_neg, Int.neg_mul_neg]
Truncating Modulus Invariance Under Divisor Negation: $\operatorname{tmod}(a, -b) = \operatorname{tmod}(a, b)$
Informal description
For any integers $a$ and $b$, the truncating modulo operation satisfies $\operatorname{tmod}(a, -b) = \operatorname{tmod}(a, b)$.
Int.neg_tmod theorem
(a b : Int) : tmod (-a) b = -tmod a b
Full source
@[simp] theorem neg_tmod (a b : Int) : tmod (-a) b = -tmod a b := by
  rw [tmod_def, Int.neg_tdiv, Int.mul_neg, tmod_def]
  omega
Negation and Truncating Modulo: $\operatorname{tmod}(-a, b) = -\operatorname{tmod}(a, b)$
Informal description
For any integers $a$ and $b$, the truncating modulo operation satisfies $\operatorname{tmod}(-a, b) = -\operatorname{tmod}(a, b)$.
Int.tmod_lt_of_pos theorem
(a : Int) {b : Int} (H : 0 < b) : tmod a b < b
Full source
theorem tmod_lt_of_pos (a : Int) {b : Int} (H : 0 < b) : tmod a b < b :=
  match a, b, eq_succ_of_zero_lt H with
  | ofNat _, _, ⟨n, rfl⟩ => ofNat_lt.2 <| Nat.mod_lt _ n.succ_pos
  | -[_+1], _, ⟨n, rfl⟩ => Int.lt_of_le_of_lt
    (Int.neg_nonpos_of_nonneg <| Int.ofNat_nonneg _) (ofNat_pos.2 n.succ_pos)
Bounds on Truncating Modulo Operation: $\operatorname{tmod}(a, b) < b$ for $b > 0$
Informal description
For any integer $a$ and any positive integer $b > 0$, the truncating modulo operation satisfies $\operatorname{tmod}(a, b) < b$.
Int.lt_tmod_of_pos theorem
(a : Int) {b : Int} (H : 0 < b) : -b < tmod a b
Full source
theorem lt_tmod_of_pos (a : Int) {b : Int} (H : 0 < b) : -b < tmod a b :=
  match a, b, eq_succ_of_zero_lt H with
  | ofNat _, _, ⟨n, rfl⟩ => by rw [ofNat_eq_coe, ← Int.natCast_succ, ← ofNat_tmod]; omega
  | -[a+1], _, ⟨n, rfl⟩ => by
    rw [negSucc_eq, neg_tmod, ← Int.natCast_succ, ← Int.natCast_succ, ← ofNat_tmod]
    have : (a + 1) % (n + 1) < n + 1 := Nat.mod_lt _ (Nat.zero_lt_succ n)
    omega
Lower Bound on Truncating Modulo Operation: $-b < \operatorname{tmod}(a, b)$ for $b > 0$
Informal description
For any integer $a$ and any positive integer $b > 0$, the truncating modulo operation satisfies $-b < \operatorname{tmod}(a, b)$.
Int.mul_tmod_left theorem
(a b : Int) : (a * b).tmod b = 0
Full source
@[simp] theorem mul_tmod_left (a b : Int) : (a * b).tmod b = 0 :=
  if h : b = 0 then by simp [h, Int.mul_zero] else by
    rw [Int.tmod_def, Int.mul_tdiv_cancel _ h, Int.mul_comm, Int.sub_self]
Truncating Modulus of Product with Respect to Right Factor: $(a \cdot b) \operatorname{tmod} b = 0$
Informal description
For any integers $a$ and $b$, the truncating modulus of $a \cdot b$ with respect to $b$ is zero, i.e., $(a \cdot b) \operatorname{tmod} b = 0$.
Int.mul_tmod_right theorem
(a b : Int) : (a * b).tmod a = 0
Full source
@[simp] theorem mul_tmod_right (a b : Int) : (a * b).tmod a = 0 := by
  rw [Int.mul_comm, mul_tmod_left]
Truncating Modulus of Product with Respect to Left Factor: $(a \cdot b) \operatorname{tmod} a = 0$
Informal description
For any integers $a$ and $b$, the truncating modulus of $a \cdot b$ with respect to $a$ is zero, i.e., $(a \cdot b) \operatorname{tmod} a = 0$.
Int.mul_tmod theorem
(a b n : Int) : (a * b).tmod n = (a.tmod n * b.tmod n).tmod n
Full source
theorem mul_tmod (a b n : Int) : (a * b).tmod n = (a.tmod n * b.tmod n).tmod n := by
  induction a using wlog_sign
  case inv => simp [Int.neg_mul]
  induction b using wlog_sign
  case inv => simp [Int.mul_neg]
  induction n using wlog_sign
  case inv => simp
  simp only [← Int.natCast_mul, ← ofNat_tmod]
  rw [Nat.mul_mod]
Modular Multiplication Identity for Truncating Modulo: $(a \cdot b) \bmod n = ((a \bmod n) \cdot (b \bmod n)) \bmod n$
Informal description
For any integers $a$, $b$, and $n$, the truncating modulo operation satisfies: $$(a \cdot b) \bmod n = ((a \bmod n) \cdot (b \bmod n)) \bmod n$$ where $\bmod$ denotes the truncating modulo operation (with remainder having same sign as dividend).
Int.tmod_self theorem
{a : Int} : a.tmod a = 0
Full source
@[simp] theorem tmod_self {a : Int} : a.tmod a = 0 := by
  have := mul_tmod_left 1 a; rwa [Int.one_mul] at this
Self-Modulus Property: $a \operatorname{tmod} a = 0$
Informal description
For any integer $a$, the truncating modulus of $a$ with respect to itself is zero, i.e., $a \operatorname{tmod} a = 0$.
Int.tmod_tmod_of_dvd theorem
(n : Int) {m k : Int} (h : m ∣ k) : (n.tmod k).tmod m = n.tmod m
Full source
@[simp] theorem tmod_tmod_of_dvd (n : Int) {m k : Int}
    (h : m ∣ k) : (n.tmod k).tmod m = n.tmod m := by
  induction n using wlog_sign
  case inv => simp
  induction k using wlog_sign
  case inv => simp [Int.dvd_neg]
  induction m using wlog_sign
  case inv => simp
  simp only [← Int.natCast_mul, ← ofNat_tmod]
  norm_cast at h
  rw [Nat.mod_mod_of_dvd _ h]
Nested Truncating Modulus Identity under Divisibility: $(n \bmod k) \bmod m = n \bmod m$ when $m \mid k$
Informal description
For any integer $n$ and integers $m, k$ such that $m$ divides $k$, the nested truncating modulus satisfies $(n \operatorname{tmod} k) \operatorname{tmod} m = n \operatorname{tmod} m$.
Int.tmod_tmod theorem
(a b : Int) : (a.tmod b).tmod b = a.tmod b
Full source
@[simp] theorem tmod_tmod (a b : Int) : (a.tmod b).tmod b = a.tmod b :=
  tmod_tmod_of_dvd a (Int.dvd_refl b)
Idempotence of Truncating Modulus: $(a \bmod b) \bmod b = a \bmod b$
Informal description
For any integers $a$ and $b$, the nested truncating modulus operation satisfies $(a \operatorname{tmod} b) \operatorname{tmod} b = a \operatorname{tmod} b$.
Int.tmod_eq_zero_of_dvd theorem
: ∀ {a b : Int}, a ∣ b → tmod b a = 0
Full source
theorem tmod_eq_zero_of_dvd : ∀ {a b : Int}, a ∣ btmod b a = 0
  | _, _, ⟨_, rfl⟩ => mul_tmod_right ..
Divisibility Implies Zero Truncating Modulus: $a \mid b \Rightarrow b \operatorname{tmod} a = 0$
Informal description
For any integers $a$ and $b$, if $a$ divides $b$, then the truncating modulus of $b$ with respect to $a$ is zero, i.e., $b \operatorname{tmod} a = 0$.
Int.tmod_eq_of_lt theorem
{a b : Int} (H1 : 0 ≤ a) (H2 : a < b) : tmod a b = a
Full source
theorem tmod_eq_of_lt {a b : Int} (H1 : 0 ≤ a) (H2 : a < b) : tmod a b = a := by
  rw [tmod_eq_emod_of_nonneg H1, emod_eq_of_lt H1 H2]
Truncating Modulus Identity for Nonnegative Integers Under Strict Inequality: $\operatorname{tmod}(a, b) = a$ when $0 \leq a < b$
Informal description
For any integers $a$ and $b$ such that $0 \leq a$ and $a < b$, the truncating modulus satisfies $\operatorname{tmod}(a, b) = a$.
Int.sign_tmod theorem
(a b : Int) : sign (tmod a b) = if b ∣ a then 0 else sign a
Full source
theorem sign_tmod (a b : Int) : sign (tmod a b) = if b ∣ a then 0 else sign a := by
  if hb : b = 0 then
    subst hb
    split <;> simp_all
  else
    induction a using wlog_sign
    case inv a => split <;> simp_all
    rename_i a
    match a with
    | 0 => simp
    | (a + 1) =>
      simp
      split
      · simp [tmod_eq_zero_of_dvd, *]
      · norm_cast
        rw [tmod_eq_emod_of_nonneg (by omega)]
        rw [sign_emod _ hb]
        simp_all
Sign of Truncating Modulus: $\operatorname{sign}(\operatorname{tmod}(a, b)) = 0$ if $b \mid a$, else $\operatorname{sign}(a)$
Informal description
For any integers $a$ and $b$, the sign of the truncating modulus $\operatorname{tmod}(a, b)$ is given by: \[ \operatorname{sign}(\operatorname{tmod}(a, b)) = \begin{cases} 0 & \text{if } b \text{ divides } a \\ \operatorname{sign}(a) & \text{otherwise} \end{cases} \]
Int.natAbs_tmod theorem
(a b : Int) : natAbs (tmod a b) = natAbs a % natAbs b
Full source
@[simp] theorem natAbs_tmod (a b : Int) : natAbs (tmod a b) = natAbs a % natAbs b := by
  induction a using wlog_sign
  case inv => simp
  induction b using wlog_sign
  case inv => simp
  norm_cast
Absolute Value of Truncating Modulus Equals Modulus of Absolute Values: $\operatorname{natAbs}(\operatorname{tmod}(a, b)) = \operatorname{natAbs}(a) \% \operatorname{natAbs}(b)$
Informal description
For any integers $a$ and $b$, the natural number absolute value of the truncating modulus $\operatorname{tmod}(a, b)$ equals the modulus of the natural number absolute values of $a$ and $b$, i.e., \[ \operatorname{natAbs}(\operatorname{tmod}(a, b)) = \operatorname{natAbs}(a) \% \operatorname{natAbs}(b). \]
Int.mul_tdiv_cancel_of_tmod_eq_zero theorem
{a b : Int} (H : a.tmod b = 0) : b * (a.tdiv b) = a
Full source
theorem mul_tdiv_cancel_of_tmod_eq_zero {a b : Int} (H : a.tmod b = 0) : b * (a.tdiv b) = a := by
  have := tmod_add_tdiv a b; rwa [H, Int.zero_add] at this
Multiplication-Cancellation Property of Truncating Division When Remainder is Zero: $b \cdot \operatorname{tdiv}(a, b) = a$
Informal description
For any integers $a$ and $b$, if the truncating modulus $\operatorname{tmod}(a, b) = 0$, then $b$ multiplied by the truncating division $\operatorname{tdiv}(a, b)$ equals $a$, i.e., \[ b \cdot \operatorname{tdiv}(a, b) = a. \]
Int.tdiv_mul_cancel_of_tmod_eq_zero theorem
{a b : Int} (H : a.tmod b = 0) : a.tdiv b * b = a
Full source
theorem tdiv_mul_cancel_of_tmod_eq_zero {a b : Int} (H : a.tmod b = 0) : a.tdiv b * b = a := by
  rw [Int.mul_comm, mul_tdiv_cancel_of_tmod_eq_zero H]
Multiplication-Cancellation Property of Truncating Division When Remainder is Zero: $\operatorname{tdiv}(a, b) \cdot b = a$
Informal description
For any integers $a$ and $b$, if the truncating modulus $\operatorname{tmod}(a, b) = 0$, then the truncating division $\operatorname{tdiv}(a, b)$ multiplied by $b$ equals $a$, i.e., \[ \operatorname{tdiv}(a, b) \cdot b = a. \]
Int.dvd_of_tmod_eq_zero theorem
{a b : Int} (H : tmod b a = 0) : a ∣ b
Full source
theorem dvd_of_tmod_eq_zero {a b : Int} (H : tmod b a = 0) : a ∣ b :=
  ⟨b.tdiv a, (mul_tdiv_cancel_of_tmod_eq_zero H).symm⟩
Divisibility Criterion via Truncating Modulus: $\operatorname{tmod}(b, a) = 0 \implies a \mid b$
Informal description
For any integers $a$ and $b$, if the truncating modulus operation satisfies $\operatorname{tmod}(b, a) = 0$, then $a$ divides $b$, i.e., $a \mid b$.
Int.dvd_iff_tmod_eq_zero theorem
{a b : Int} : a ∣ b ↔ tmod b a = 0
Full source
theorem dvd_iff_tmod_eq_zero {a b : Int} : a ∣ ba ∣ b ↔ tmod b a = 0 :=
  ⟨tmod_eq_zero_of_dvd, dvd_of_tmod_eq_zero⟩
Divisibility Criterion via Truncating Modulus: $a \mid b \leftrightarrow \operatorname{tmod}(b, a) = 0$
Informal description
For any integers $a$ and $b$, $a$ divides $b$ if and only if the truncating modulus of $b$ with respect to $a$ is zero, i.e., $a \mid b \leftrightarrow \operatorname{tmod}(b, a) = 0$.
Int.tdiv_mul_cancel theorem
{a b : Int} (H : b ∣ a) : a.tdiv b * b = a
Full source
protected theorem tdiv_mul_cancel {a b : Int} (H : b ∣ a) : a.tdiv b * b = a :=
  tdiv_mul_cancel_of_tmod_eq_zero (tmod_eq_zero_of_dvd H)
Truncating Division Cancellation: $\operatorname{tdiv}(a, b) \cdot b = a$ when $b \mid a$
Informal description
For any integers $a$ and $b$ such that $b$ divides $a$, the product of the truncating division of $a$ by $b$ and $b$ equals $a$, i.e., \[ \operatorname{tdiv}(a, b) \cdot b = a. \]
Int.mul_tdiv_cancel' theorem
{a b : Int} (H : a ∣ b) : a * b.tdiv a = b
Full source
protected theorem mul_tdiv_cancel' {a b : Int} (H : a ∣ b) : a * b.tdiv a = b := by
  rw [Int.mul_comm, Int.tdiv_mul_cancel H]
Truncating Division Cancellation: $a \cdot \operatorname{tdiv}(b, a) = b$ when $a \mid b$
Informal description
For any integers $a$ and $b$ such that $a$ divides $b$, the product of $a$ and the truncating division of $b$ by $a$ equals $b$, i.e., \[ a \cdot \operatorname{tdiv}(b, a) = b. \]
Int.neg_tmod_self theorem
(a : Int) : (-a).tmod a = 0
Full source
@[simp] theorem neg_tmod_self (a : Int) : (-a).tmod a = 0 := by
  rw [← dvd_iff_tmod_eq_zero, Int.dvd_neg]
  exact Int.dvd_refl a
Truncating Modulus of Negated Integer: $\operatorname{tmod}(-a, a) = 0$
Informal description
For any integer $a$, the truncating modulus of $-a$ with respect to $a$ is zero, i.e., $\operatorname{tmod}(-a, a) = 0$.
Int.lt_tdiv_add_one_mul_self theorem
(a : Int) {b : Int} (H : 0 < b) : a < (a.tdiv b + 1) * b
Full source
theorem lt_tdiv_add_one_mul_self (a : Int) {b : Int} (H : 0 < b) : a < (a.tdiv b + 1) * b := by
  rw [Int.add_mul, Int.one_mul, Int.mul_comm]
  exact Int.lt_add_of_sub_left_lt <| Int.tmod_def .. ▸ tmod_lt_of_pos _ H
Upper bound for integer division: $a < (q + 1)b$ where $q = \lfloor a/b \rfloor$ and $b > 0$
Informal description
For any integer $a$ and any positive integer $b > 0$, the following inequality holds: \[ a < \left(\left\lfloor \frac{a}{b} \right\rfloor + 1\right) \cdot b \] where $\lfloor \cdot \rfloor$ denotes the truncating division (rounding towards zero).
Int.mul_tdiv_assoc theorem
(a : Int) : ∀ {b c : Int}, c ∣ b → (a * b).tdiv c = a * (b.tdiv c)
Full source
protected theorem mul_tdiv_assoc (a : Int) : ∀ {b c : Int}, c ∣ b → (a * b).tdiv c = a * (b.tdiv c)
  | _, c, ⟨d, rfl⟩ =>
    if cz : c = 0 then by simp [cz, Int.mul_zero] else by
      rw [Int.mul_left_comm, Int.mul_tdiv_cancel_left _ cz, Int.mul_tdiv_cancel_left _ cz]
Associativity of Multiplication and Truncating Division: $(a \cdot b) \operatorname{tdiv} c = a \cdot (b \operatorname{tdiv} c)$ when $c \mid b$
Informal description
For any integer $a$ and for any integers $b, c$ such that $c$ divides $b$, the truncating division of $a \cdot b$ by $c$ equals $a$ multiplied by the truncating division of $b$ by $c$, i.e., $(a \cdot b) \operatorname{tdiv} c = a \cdot (b \operatorname{tdiv} c)$.
Int.mul_tdiv_assoc' theorem
(b : Int) {a c : Int} (h : c ∣ a) : (a * b).tdiv c = a.tdiv c * b
Full source
protected theorem mul_tdiv_assoc' (b : Int) {a c : Int} (h : c ∣ a) :
    (a * b).tdiv c = a.tdiv c * b := by
  rw [Int.mul_comm, Int.mul_tdiv_assoc _ h, Int.mul_comm]
Associativity of Multiplication and Truncating Division (variant): $(a \cdot b) \operatorname{tdiv} c = (a \operatorname{tdiv} c) \cdot b$ when $c \mid a$
Informal description
For any integer $b$ and for any integers $a, c$ such that $c$ divides $a$, the truncating division of $a \cdot b$ by $c$ equals the truncating division of $a$ by $c$ multiplied by $b$, i.e., $(a \cdot b) \operatorname{tdiv} c = (a \operatorname{tdiv} c) \cdot b$.
Int.neg_tdiv_of_dvd theorem
: ∀ {a b : Int}, b ∣ a → (-a).tdiv b = -(a.tdiv b)
Full source
theorem neg_tdiv_of_dvd : ∀ {a b : Int}, b ∣ a → (-a).tdiv b = -(a.tdiv b)
  | _, b, ⟨c, rfl⟩ => by
    by_cases bz : b = 0
    · simp [bz]
    · rw [Int.neg_mul_eq_mul_neg, Int.mul_tdiv_cancel_left _ bz, Int.mul_tdiv_cancel_left _ bz]
Negation Preserves Truncating Division Under Divisibility: $(-a) \operatorname{tdiv} b = -(a \operatorname{tdiv} b)$ when $b \mid a$
Informal description
For any integers $a$ and $b$ such that $b$ divides $a$, the truncating division of $-a$ by $b$ equals the negation of the truncating division of $a$ by $b$, i.e., $(-a) \operatorname{tdiv} b = -(a \operatorname{tdiv} b)$.
Int.tdiv_dvd_tdiv theorem
: ∀ {a b c : Int}, a ∣ b → b ∣ c → b.tdiv a ∣ c.tdiv a
Full source
theorem tdiv_dvd_tdiv : ∀ {a b c : Int}, a ∣ bb ∣ cb.tdiv a ∣ c.tdiv a
  | a, _, _, ⟨b, rfl⟩, ⟨c, rfl⟩ => by
    by_cases az : a = 0
    · simp [az]
    · rw [Int.mul_tdiv_cancel_left _ az, Int.mul_assoc, Int.mul_tdiv_cancel_left _ az]
      apply Int.dvd_mul_right
Divisibility Preservation under Truncating Division: $a \mid b \land b \mid c \Rightarrow (b \operatorname{tdiv} a) \mid (c \operatorname{tdiv} a)$
Informal description
For any integers $a$, $b$, and $c$, if $a$ divides $b$ and $b$ divides $c$, then the truncating division of $b$ by $a$ divides the truncating division of $c$ by $a$.
Int.mul_tdiv_cancel_of_dvd theorem
{a b : Int} (H : b ∣ a) : b * (a.tdiv b) = a
Full source
theorem mul_tdiv_cancel_of_dvd {a b : Int} (H : b ∣ a) : b * (a.tdiv b) = a :=
  mul_tdiv_cancel_of_tmod_eq_zero (tmod_eq_zero_of_dvd H)
Multiplication-Cancellation Property of Truncating Division Under Divisibility: $b \cdot (a \operatorname{tdiv} b) = a$ when $b \mid a$
Informal description
For any integers $a$ and $b$ such that $b$ divides $a$, the product of $b$ and the truncating division of $a$ by $b$ equals $a$, i.e., \[ b \cdot (a \operatorname{tdiv} b) = a. \]
Int.tdiv_mul_cancel_of_dvd theorem
{a b : Int} (H : b ∣ a) : a.tdiv b * b = a
Full source
theorem tdiv_mul_cancel_of_dvd {a b : Int} (H : b ∣ a) : a.tdiv b * b = a :=
  tdiv_mul_cancel_of_tmod_eq_zero (tmod_eq_zero_of_dvd H)
Truncating Division Cancellation under Divisibility: $\operatorname{tdiv}(a, b) \cdot b = a$ when $b \mid a$
Informal description
For any integers $a$ and $b$ such that $b$ divides $a$, the truncating division of $a$ by $b$ multiplied by $b$ equals $a$, i.e., \[ \operatorname{tdiv}(a, b) \cdot b = a. \]
Int.tmod_two_eq theorem
(x : Int) : x.tmod 2 = -1 ∨ x.tmod 2 = 0 ∨ x.tmod 2 = 1
Full source
theorem tmod_two_eq (x : Int) : x.tmod 2 = -1 ∨ x.tmod 2 = 0 ∨ x.tmod 2 = 1 := by
  have h₁ : -2 < x.tmod 2 := Int.lt_tmod_of_pos x (by decide)
  have h₂ : x.tmod 2 < 2 := Int.tmod_lt_of_pos x (by decide)
  match x.tmod 2, h₁, h₂ with
  | -1, _, _ => simp
  | 0, _, _ => simp
  | 1, _, _ => simp
Possible Remainders in Truncating Modulo 2: $\{-1, 0, 1\}$
Informal description
For any integer $x$, the truncating modulo operation with divisor 2 satisfies $\operatorname{tmod}(x, 2) \in \{-1, 0, 1\}$.
Int.tdiv_tmod_unique theorem
{a b r q : Int} (ha : 0 ≤ a) (hb : b ≠ 0) : a.tdiv b = q ∧ a.tmod b = r ↔ r + b * q = a ∧ 0 ≤ r ∧ r < natAbs b
Full source
protected theorem tdiv_tmod_unique {a b r q : Int} (ha : 0 ≤ a) (hb : b ≠ 0) :
    a.tdiv b = q ∧ a.tmod b = ra.tdiv b = q ∧ a.tmod b = r ↔ r + b * q = a ∧ 0 ≤ r ∧ r < natAbs b := by
  rw [tdiv_eq_ediv_of_nonneg ha, tmod_eq_emod_of_nonneg ha]
  by_cases hb' : 0 < b
  · rw [Int.ediv_emod_unique hb']
    omega
  · replace hb' : 0 < -b := by omega
    have := Int.ediv_emod_unique (a := a) (q := -q) (r := r) hb'
    simp at this
    simp [this, Int.neg_mul, Int.mul_neg]
    omega
Characterization of Truncating Division and Remainder for Non-Negative Integers: $a \geq 0 \land b \neq 0 \Rightarrow (a \ \mathrm{tdiv} \ b = q \land a \ \mathrm{tmod} \ b = r) \leftrightarrow r + b q = a \land 0 \leq r < |b|$
Informal description
For any integers $a, b, r, q$ with $a \geq 0$ and $b \neq 0$, the following are equivalent: 1. The truncating division of $a$ by $b$ equals $q$ and the truncating modulus equals $r$ (i.e., $a \ \mathrm{tdiv} \ b = q$ and $a \ \mathrm{tmod} \ b = r$). 2. The decomposition $r + b \cdot q = a$ holds, with $0 \leq r < |b|$.
Int.tdiv_tmod_unique' theorem
{a b r q : Int} (ha : a ≤ 0) (hb : b ≠ 0) : a.tdiv b = q ∧ a.tmod b = r ↔ r + b * q = a ∧ -natAbs b < r ∧ r ≤ 0
Full source
protected theorem tdiv_tmod_unique' {a b r q : Int} (ha : a ≤ 0) (hb : b ≠ 0) :
    a.tdiv b = q ∧ a.tmod b = ra.tdiv b = q ∧ a.tmod b = r ↔ r + b * q = a ∧ -natAbs b < r ∧ r ≤ 0 := by
  have := Int.tdiv_tmod_unique (a := -a) (q := -q) (r := -r) (by omega) hb
  simp at this
  simp [this, Int.mul_neg]
  omega
Characterization of Truncating Division and Remainder for Non-Positive Integers: $a \leq 0 \land b \neq 0 \Rightarrow (a \ \mathrm{tdiv} \ b = q \land a \ \mathrm{tmod} \ b = r) \leftrightarrow r + b q = a \land -|b| < r \leq 0$
Informal description
For any integers $a, b, r, q$ with $a \leq 0$ and $b \neq 0$, the following are equivalent: 1. The truncating division of $a$ by $b$ equals $q$ and the truncating modulus equals $r$ (i.e., $a \ \mathrm{tdiv} \ b = q$ and $a \ \mathrm{tmod} \ b = r$). 2. The decomposition $r + b \cdot q = a$ holds, with $-|b| < r \leq 0$.
Int.mul_tmod_mul_of_pos theorem
{a : Int} (b c : Int) (H : 0 < a) : (a * b).tmod (a * c) = a * (b.tmod c)
Full source
@[simp] theorem mul_tmod_mul_of_pos
    {a : Int} (b c : Int) (H : 0 < a) : (a * b).tmod (a * c) = a * (b.tmod c) := by
  rw [tmod_def, tmod_def, mul_tdiv_mul_of_pos _ _ H, Int.mul_sub, Int.mul_assoc]
Scalar Multiplication Preserves Truncating Modulus: $(a \cdot b) \operatorname{tmod} (a \cdot c) = a \cdot (b \operatorname{tmod} c)$ for $a > 0$
Informal description
For any positive integer $a$ and integers $b, c$, the truncating modulus of $a \cdot b$ by $a \cdot c$ equals $a$ multiplied by the truncating modulus of $b$ by $c$, i.e., \[ (a \cdot b) \operatorname{tmod} (a \cdot c) = a \cdot (b \operatorname{tmod} c). \]
Int.natAbs_tdiv_le_natAbs theorem
(a b : Int) : natAbs (a.tdiv b) ≤ natAbs a
Full source
theorem natAbs_tdiv_le_natAbs (a b : Int) : natAbs (a.tdiv b) ≤ natAbs a := by
  induction a using wlog_sign
  case inv => simp
  induction b using wlog_sign
  case inv => simp
  simpa using Nat.div_le_self _ _
Absolute Value Bound for Truncating Division: $\text{natAbs}(a \operatorname{tdiv} b) \leq \text{natAbs}(a)$
Informal description
For any integers $a$ and $b$, the absolute value of the truncating division $a \operatorname{tdiv} b$ (as a natural number) is less than or equal to the absolute value of $a$, i.e., $\text{natAbs}(a \operatorname{tdiv} b) \leq \text{natAbs}(a)$.
Int.tdiv_le_self theorem
{a : Int} (b : Int) (Ha : 0 ≤ a) : a.tdiv b ≤ a
Full source
theorem tdiv_le_self {a : Int} (b : Int) (Ha : 0 ≤ a) : a.tdiv b ≤ a := by
  have := Int.le_trans le_natAbs (ofNat_le.2 <| natAbs_tdiv_le_natAbs a b)
  rwa [natAbs_of_nonneg Ha] at this
Non-negativity of Truncating Division: $a \operatorname{tdiv} b \leq a$ for $a \geq 0$
Informal description
For any non-negative integer $a$ and any integer $b$, the truncating division $a \operatorname{tdiv} b$ is less than or equal to $a$ itself, i.e., $a \operatorname{tdiv} b \leq a$.
Int.dvd_tmod_sub_self theorem
{x : Int} {m : Nat} : (m : Int) ∣ x.tmod m - x
Full source
theorem dvd_tmod_sub_self {x : Int} {m : Nat} : (m : Int) ∣ x.tmod m - x := by
  rw [tmod_eq_emod]
  have := dvd_emod_sub_self (x := x) (m := m)
  split
  · simpa
  · rw [Int.sub_sub, Int.add_comm, ← Int.sub_sub]
    apply Int.dvd_sub this
    simp
Divisibility of Truncating Modulus Difference: $m \mid (\operatorname{tmod}(x, m) - x)$
Informal description
For any integer $x$ and natural number $m$, the integer $m$ divides the difference between the truncating modulus of $x$ with respect to $m$ and $x$ itself, i.e., $m \mid (\operatorname{tmod}(x, m) - x)$.
Int.neg_mul_tmod_right theorem
(a b : Int) : (-(a * b)).tmod a = 0
Full source
@[simp] theorem neg_mul_tmod_right (a b : Int) : (-(a * b)).tmod a = 0 := by
  rw [← dvd_iff_tmod_eq_zero, Int.dvd_neg]
  exact Int.dvd_mul_right a b
Truncating Modulus of Negative Product: $\operatorname{tmod}(-(a \cdot b), a) = 0$
Informal description
For any integers $a$ and $b$, the truncating modulus of $-(a \cdot b)$ with respect to $a$ is zero, i.e., $\operatorname{tmod}(-(a \cdot b), a) = 0$.
Int.neg_mul_tmod_left theorem
(a b : Int) : (-(a * b)).tmod b = 0
Full source
@[simp] theorem neg_mul_tmod_left (a b : Int) : (-(a * b)).tmod b = 0 := by
  rw [← dvd_iff_tmod_eq_zero, Int.dvd_neg]
  exact Int.dvd_mul_left a b
Truncating Modulus of Negative Product: $\operatorname{tmod}(-(a \cdot b), b) = 0$
Informal description
For any integers $a$ and $b$, the truncating modulus of $-(a \cdot b)$ with respect to $b$ is zero, i.e., $\operatorname{tmod}(-(a \cdot b), b) = 0$.
Int.tdiv_one theorem
: ∀ a : Int, a.tdiv 1 = a
Full source
@[simp] protected theorem tdiv_one : ∀ a : Int, a.tdiv 1 = a
  | (n:Nat) => congrArg ofNat (Nat.div_one _)
  | -[n+1] => by simp [Int.tdiv, neg_ofNat_succ]; rfl
Truncating Division by One Identity: $a \operatorname{tdiv} 1 = a$
Informal description
For any integer $a$, the truncating division of $a$ by $1$ equals $a$, i.e., $a \operatorname{tdiv} 1 = a$.
Int.tmod_one theorem
(a : Int) : tmod a 1 = 0
Full source
@[simp] theorem tmod_one (a : Int) : tmod a 1 = 0 := by
  simp [tmod_def, Int.tdiv_one, Int.one_mul, Int.sub_self]
Truncating Modulus Identity: $a \operatorname{tmod} 1 = 0$
Informal description
For any integer $a$, the truncating modulus of $a$ with respect to $1$ is zero, i.e., $a \operatorname{tmod} 1 = 0$.
Int.tdiv_eq_iff_eq_mul_right theorem
{a b c : Int} (H : b ≠ 0) (H' : b ∣ a) : a.tdiv b = c ↔ a = b * c
Full source
protected theorem tdiv_eq_iff_eq_mul_right {a b c : Int}
    (H : b ≠ 0) (H' : b ∣ a) : a.tdiv b = c ↔ a = b * c :=
  ⟨Int.eq_mul_of_tdiv_eq_right H', Int.tdiv_eq_of_eq_mul_right H⟩
Truncating Division Equivalence: $a \operatorname{tdiv} b = c \leftrightarrow a = b \cdot c$ for $b \neq 0$ and $b \mid a$
Informal description
For any integers $a$, $b$, and $c$ with $b \neq 0$ and $b$ dividing $a$, the truncating division of $a$ by $b$ equals $c$ if and only if $a$ equals $b$ multiplied by $c$, i.e., $a \operatorname{tdiv} b = c \leftrightarrow a = b \cdot c$.
Int.tdiv_eq_iff_eq_mul_left theorem
{a b c : Int} (H : b ≠ 0) (H' : b ∣ a) : a.tdiv b = c ↔ a = c * b
Full source
protected theorem tdiv_eq_iff_eq_mul_left {a b c : Int}
    (H : b ≠ 0) (H' : b ∣ a) : a.tdiv b = c ↔ a = c * b := by
  rw [Int.mul_comm]; exact Int.tdiv_eq_iff_eq_mul_right H H'
Truncating Division Equivalence: $a \operatorname{tdiv} b = c \leftrightarrow a = c \cdot b$ for $b \neq 0$ and $b \mid a$
Informal description
For any integers $a$, $b$, and $c$ with $b \neq 0$ and $b$ dividing $a$, the truncating division of $a$ by $b$ equals $c$ if and only if $a$ equals $c$ multiplied by $b$, i.e., $a \operatorname{tdiv} b = c \leftrightarrow a = c \cdot b$.
Int.tdiv_left_inj theorem
{a b d : Int} (hda : d ∣ a) (hdb : d ∣ b) : a.tdiv d = b.tdiv d ↔ a = b
Full source
@[simp] protected theorem tdiv_left_inj {a b d : Int}
    (hda : d ∣ a) (hdb : d ∣ b) : a.tdiv d = b.tdiv d ↔ a = b := by
  refine ⟨fun h => ?_, congrArg (tdiv · d)⟩
  rw [← Int.mul_tdiv_cancel' hda, ← Int.mul_tdiv_cancel' hdb, h]
Injectivity of Truncating Division for Common Divisors: $\frac{a}{d} = \frac{b}{d} \leftrightarrow a = b$ when $d \mid a$ and $d \mid b$
Informal description
For any integers $a$, $b$, and $d$ such that $d$ divides both $a$ and $b$, the truncating division of $a$ by $d$ equals the truncating division of $b$ by $d$ if and only if $a$ equals $b$. In other words: \[ \frac{a}{d} = \frac{b}{d} \leftrightarrow a = b \] where $\frac{\cdot}{d}$ denotes truncating division by $d$.
Int.tdiv_sign theorem
: ∀ a b, a.tdiv (sign b) = a * sign b
Full source
theorem tdiv_sign : ∀ a b, a.tdiv (sign b) = a * sign b
  | _, succ _ => by simp [sign, Int.mul_one]
  | _, 0 => by simp [sign, Int.mul_zero]
  | _, -[_+1] => by simp [sign, Int.mul_neg, Int.mul_one]
Truncating Division by Sign Equals Multiplication by Sign: $a \operatorname{tdiv} (\operatorname{sign} b) = a \cdot \operatorname{sign} b$
Informal description
For any integers $a$ and $b$, the truncating division of $a$ by the sign of $b$ equals the product of $a$ and the sign of $b$, i.e., $a \operatorname{tdiv} (\operatorname{sign} b) = a \cdot \operatorname{sign} b$.
Int.sign_eq_tdiv_abs theorem
(a : Int) : sign a = a.tdiv (natAbs a)
Full source
protected theorem sign_eq_tdiv_abs (a : Int) : sign a = a.tdiv (natAbs a) :=
  if az : a = 0 then by simp [az] else
    (Int.tdiv_eq_of_eq_mul_left (ofNat_ne_zero.2 <| natAbs_ne_zero.2 az)
      (sign_mul_natAbs _).symm).symm
Sign as Truncating Division by Absolute Value: $\operatorname{sign}(a) = a \ \mathrm{tdiv} \ |a|$
Informal description
For any integer $a$, the sign of $a$ equals the truncating division of $a$ by its natural absolute value, i.e., \[ \operatorname{sign}(a) = a \ \mathrm{tdiv} \ |a|. \]
Int.mul_tdiv_self_le theorem
{x k : Int} (h : 0 ≤ x) : k * (x.tdiv k) ≤ x
Full source
theorem mul_tdiv_self_le {x k : Int} (h : 0 ≤ x) : k * (x.tdiv k) ≤ x := by
  by_cases w : k = 0
  · simp [w, h]
  · rw [tdiv_eq_ediv_of_nonneg h]
    apply mul_ediv_self_le w
Truncating Division Lower Bound: $k \cdot (x \ \mathrm{tdiv} \ k) \leq x$ for $x \geq 0$
Informal description
For any integers $x$ and $k$ with $x \geq 0$, the product of $k$ and the truncating division of $x$ by $k$ is less than or equal to $x$, i.e., \[ k \cdot (x \ \mathrm{tdiv} \ k) \leq x. \]
Int.lt_mul_tdiv_self_add theorem
{x k : Int} (h : 0 < k) : x < k * (x.tdiv k) + k
Full source
theorem lt_mul_tdiv_self_add {x k : Int} (h : 0 < k) : x < k * (x.tdiv k) + k := by
  rw [tdiv_eq_ediv, sign_eq_one_of_pos h]
  have := lt_mul_ediv_self_add (x := x) h
  split <;> simp [Int.mul_add] <;> omega
Lower bound for truncating division: $x < k \cdot \left\lfloor \frac{x}{k} \right\rfloor_T + k$ when $k > 0$
Informal description
For any integers $x$ and $k$ with $k > 0$, we have the inequality: \[ x < k \cdot \left\lfloor \frac{x}{k} \right\rfloor_T + k \] where $\left\lfloor \cdot \right\rfloor_T$ denotes truncating integer division (rounding towards zero).
Int.tdiv_mul_le theorem
(a : Int) {b : Int} (hb : b ≠ 0) : a.tdiv b * b ≤ a + if 0 ≤ a then 0 else (b.natAbs - 1)
Full source
protected theorem tdiv_mul_le (a : Int) {b : Int} (hb : b ≠ 0) : a.tdiv b * b ≤ a + if 0 ≤ a then 0 else (b.natAbs - 1) :=
  Int.le_of_sub_nonneg <| by
    rw [Int.mul_comm, Int.add_comm, Int.add_sub_assoc, ← tmod_def]
    split
    · simp_all [tmod_nonneg]
    · match b, hb with
      | .ofNat (b + 1), _ =>
        have := lt_tmod_of_pos a (Int.ofNat_pos.2 (b.succ_pos))
        simp_all
        omega
      | .negSucc b, _ =>
        simp only [negSucc_eq, natAbs_neg, tmod_neg]
        have := lt_tmod_of_pos (b := b + 1) a (by omega)
        omega
Upper Bound for Truncating Division Product: $\lfloor a/b \rfloor \cdot b \leq a + \text{adjustment}$
Informal description
For any integers $a$ and $b$ with $b \neq 0$, the product of the truncating division $\lfloor a/b \rfloor$ and $b$ satisfies: \[ \lfloor a/b \rfloor \cdot b \leq a + \begin{cases} 0 & \text{if } 0 \leq a \\ |b| - 1 & \text{otherwise} \end{cases} \] where $\lfloor \cdot \rfloor$ denotes truncating division (rounding towards zero) and $|b|$ is the absolute value of $b$.
Int.tdiv_le_of_le_mul theorem
{a b c : Int} (Hc : 0 < c) (H' : a ≤ b * c) : a.tdiv c ≤ b + if 0 ≤ a then 0 else 1
Full source
protected theorem tdiv_le_of_le_mul {a b c : Int} (Hc : 0 < c) (H' : a ≤ b * c) : a.tdiv c ≤ b + if 0 ≤ a then 0 else 1 :=
  le_of_mul_le_mul_right (Int.le_trans (Int.tdiv_mul_le _ (by omega))
    (by
      split
      · simpa using H'
      · simp [Int.add_mul]
        omega)) Hc
Upper Bound for Truncating Division: $\lfloor a/c \rfloor \leq b + \text{adjustment}$ when $a \leq b \cdot c$ and $c > 0$
Informal description
For any integers $a$, $b$, and $c$ with $c > 0$, if $a \leq b \cdot c$, then the truncating division of $a$ by $c$ satisfies: \[ \left\lfloor \frac{a}{c} \right\rfloor \leq b + \begin{cases} 0 & \text{if } 0 \leq a \\ 1 & \text{otherwise} \end{cases} \] where $\left\lfloor \cdot \right\rfloor$ denotes truncating division (rounding towards zero).
Int.le_tdiv_of_mul_le theorem
{a b c : Int} (H1 : 0 < c) (H2 : a * c ≤ b) : a ≤ b.tdiv c
Full source
protected theorem le_tdiv_of_mul_le {a b c : Int} (H1 : 0 < c) (H2 : a * c ≤ b) : a ≤ b.tdiv c :=
  le_of_lt_add_one <|
    lt_of_mul_lt_mul_right (Int.lt_of_le_of_lt H2 (lt_tdiv_add_one_mul_self _ H1)) (Int.le_of_lt H1)
Lower bound for truncating division: $a \leq \lfloor b/c \rfloor$ when $a \cdot c \leq b$ and $c > 0$
Informal description
For any integers $a$, $b$, and $c$ with $c > 0$, if $a \cdot c \leq b$, then $a \leq \lfloor b / c \rfloor$, where $\lfloor \cdot \rfloor$ denotes truncating division (rounding towards zero).
Int.lt_mul_of_tdiv_lt theorem
{a b c : Int} (H1 : 0 < c) (H2 : a.tdiv c < b) : a < b * c
Full source
protected theorem lt_mul_of_tdiv_lt {a b c : Int} (H1 : 0 < c) (H2 : a.tdiv c < b) : a < b * c :=
  Int.lt_of_not_ge <| mt (Int.le_tdiv_of_mul_le H1) (Int.not_le_of_gt H2)
Upper Bound from Truncating Division: $\lfloor a/c \rfloor < b \implies a < b \cdot c$ for $c > 0$
Informal description
For any integers $a$, $b$, and $c$ with $c > 0$, if the truncating division of $a$ by $c$ is less than $b$ (i.e., $\lfloor a/c \rfloor < b$), then $a < b \cdot c$.
Int.le_mul_of_tdiv_le theorem
{a b c : Int} (H1 : 0 ≤ b) (H2 : b ∣ a) (H3 : a.tdiv b ≤ c) : a ≤ c * b
Full source
protected theorem le_mul_of_tdiv_le {a b c : Int} (H1 : 0 ≤ b) (H2 : b ∣ a) (H3 : a.tdiv b ≤ c) :
    a ≤ c * b := by
  rw [← Int.tdiv_mul_cancel H2]; exact Int.mul_le_mul_of_nonneg_right H3 H1
Upper Bound on Integer via Truncating Division: $b \geq 0 \land b \mid a \land \operatorname{tdiv}(a, b) \leq c \implies a \leq c \cdot b$
Informal description
For any integers $a$, $b$, and $c$ such that $b$ is non-negative ($0 \leq b$), $b$ divides $a$ ($b \mid a$), and the truncating division of $a$ by $b$ satisfies $\operatorname{tdiv}(a, b) \leq c$, it follows that $a \leq c \cdot b$.
Int.lt_tdiv_of_mul_lt theorem
{a b c : Int} (H1 : 0 ≤ b) (H2 : b ∣ c) (H3 : a * b < c) : a < c.tdiv b
Full source
protected theorem lt_tdiv_of_mul_lt {a b c : Int} (H1 : 0 ≤ b) (H2 : b ∣ c) (H3 : a * b < c) :
    a < c.tdiv b :=
  Int.lt_of_not_ge <| mt (Int.le_mul_of_tdiv_le H1 H2) (Int.not_le_of_gt H3)
Lower Bound from Truncating Division: $a \cdot b < c \implies a < \lfloor c/b \rfloor$ for $b \geq 0$ and $b \mid c$
Informal description
For any integers $a$, $b$, and $c$ such that $b$ is non-negative ($0 \leq b$), $b$ divides $c$ ($b \mid c$), and $a \cdot b < c$, it follows that $a$ is strictly less than the truncating division of $c$ by $b$, i.e., $a < \operatorname{tdiv}(c, b)$.
Int.tdiv_pos_of_pos_of_dvd theorem
{a b : Int} (H1 : 0 < a) (H2 : 0 ≤ b) (H3 : b ∣ a) : 0 < a.tdiv b
Full source
theorem tdiv_pos_of_pos_of_dvd {a b : Int} (H1 : 0 < a) (H2 : 0 ≤ b) (H3 : b ∣ a) : 0 < a.tdiv b :=
  Int.lt_tdiv_of_mul_lt H2 H3 (by rwa [Int.zero_mul])
Positivity of Truncating Division for Positive Dividend and Nonnegative Divisor
Informal description
For any integers $a$ and $b$ such that $a > 0$, $b \geq 0$, and $b$ divides $a$, the truncating division of $a$ by $b$ is positive, i.e., $\operatorname{tdiv}(a, b) > 0$.
Int.tdiv_eq_tdiv_of_mul_eq_mul theorem
{a b c d : Int} (H2 : d ∣ c) (H3 : b ≠ 0) (H4 : d ≠ 0) (H5 : a * d = b * c) : a.tdiv b = c.tdiv d
Full source
theorem tdiv_eq_tdiv_of_mul_eq_mul {a b c d : Int}
    (H2 : d ∣ c) (H3 : b ≠ 0) (H4 : d ≠ 0) (H5 : a * d = b * c) : a.tdiv b = c.tdiv d :=
  Int.tdiv_eq_of_eq_mul_right H3 <| by
    rw [← Int.mul_tdiv_assoc _ H2]; exact (Int.tdiv_eq_of_eq_mul_left H4 H5.symm).symm
Equality of Truncating Divisions Under Proportionality: $a \operatorname{tdiv} b = c \operatorname{tdiv} d$ when $a \cdot d = b \cdot c$ and $d \mid c$
Informal description
For any integers $a, b, c, d$ such that $d$ divides $c$, $b \neq 0$, $d \neq 0$, and $a \cdot d = b \cdot c$, the truncating division of $a$ by $b$ equals the truncating division of $c$ by $d$, i.e., $a \operatorname{tdiv} b = c \operatorname{tdiv} d$.
Int.le_mod_self_add_one_iff theorem
{a b : Int} (h : 0 < b) : b ≤ a % b + 1 ↔ b ∣ a + 1
Full source
theorem le_mod_self_add_one_iff {a b : Int} (h : 0 < b) : b ≤ a % b + 1 ↔ b ∣ a + 1 := by
  match b, h with
  | .ofNat 1, h => simp
  | .ofNat (b + 2), h =>
    simp only [ofNat_eq_coe, Int.natCast_add, cast_ofNat_Int] at *
    constructor
    · rw [dvd_iff_emod_eq_zero]
      intro w
      have := emod_lt_of_pos a h
      have : a % (b + 2) = b + 1 := by omega
      rw [add_emod, this, one_emod, if_neg (by omega)]
      have : (b + 1 + 1 : Int) = b + 2 := by omega
      rw [this, emod_self]
    · rintro ⟨d, w⟩
      replace w : a = (b + 2 : Int) * d - 1 := by omega
      subst w
      rw [emod_def, mul_sub_ediv_left _ _ (by omega), neg_one_ediv,
        sign_eq_one_of_pos (by omega), Int.mul_add]
      omega
Divisibility Criterion via Remainder Inequality: $b \leq a \bmod b + 1 \leftrightarrow b \mid a + 1$
Informal description
For any integers $a$ and $b$ with $b > 0$, the inequality $b \leq a \% b + 1$ holds if and only if $b$ divides $a + 1$, i.e., $$ b \leq a \bmod b + 1 \leftrightarrow b \mid (a + 1). $$
Int.add_one_tdiv_of_pos theorem
{a b : Int} (h : 0 < b) : (a + 1).tdiv b = a.tdiv b + if (0 < a + 1 ∧ b ∣ a + 1) ∨ (a < 0 ∧ b ∣ a) then 1 else 0
Full source
theorem add_one_tdiv_of_pos {a b : Int} (h : 0 < b) :
    (a + 1).tdiv b = a.tdiv b + if (0 < a + 1 ∧ b ∣ a + 1) ∨ (a < 0 ∧ b ∣ a) then 1 else 0 := by
  match b, h with
  | .ofNat 1, h => simp; omega
  | .ofNat (b + 2), h =>
    simp only [ofNat_eq_coe]
    rw [tdiv_eq_ediv, add_ediv (by omega), tdiv_eq_ediv]
    simp only [Int.natCast_add, cast_ofNat_Int]
    have : 1 / (b + 2 : Int) = 0 := by rw [one_ediv]; omega
    rw [this]
    have one_mod : 1 % (b + 2 : Int) = 1 := emod_eq_of_lt (by omega) (by omega)
    rw [one_mod]
    have : ↑(b + 2 : Int).natAbs = (b + 2 : Int) := by omega
    rw [this]
    have : (b + 2 : Int).sign = 1 := sign_eq_one_of_pos (by omega)
    rw [this]
    have : (b + 2) ≤ a % (b + 2 : Int) + 1 ↔ (b + 2 : Int) ∣ a + 1 := le_mod_self_add_one_iff (by omega)
    simp only [this]
    simp only [Int.add_zero]
    split <;> rename_i h
    · simp only [h, or_true, ↓reduceIte, and_true]
      omega
    · simp only [h, or_false, and_false]
      by_cases h₂ : 0 ≤ a
      · simp only [Int.add_zero, h₂, true_or, ↓reduceIte, false_or]
        have : 0 ≤ a + 1 := by omega
        have : 0 < a + 1 := by omega
        have : ¬ a < 0 := by omega
        simp [*]
      · simp only [Int.add_zero, h₂, false_or]
        split
        · have : a = -1 := by omega
          simp_all
        · have : a < 0 := by omega
          simp only [true_and, this]
          split <;> simp
Increment Truncating Division Formula for Positive Divisors: $(a + 1) \operatorname{tdiv} b = a \operatorname{tdiv} b + \text{adjustment}$
Informal description
For any integers $a$ and $b$ with $b > 0$, the truncating division of $a + 1$ by $b$ satisfies: $$ (a + 1) \operatorname{tdiv} b = a \operatorname{tdiv} b + \begin{cases} 1 & \text{if } (0 < a + 1 \text{ and } b \mid a + 1) \text{ or } (a < 0 \text{ and } b \mid a), \\ 0 & \text{otherwise.} \end{cases} $$
Int.add_one_tdiv theorem
{a b : Int} : (a + 1).tdiv b = a.tdiv b + if (0 < a + 1 ∧ b ∣ a + 1) ∨ (a < 0 ∧ b ∣ a) then b.sign else 0
Full source
theorem add_one_tdiv {a b : Int} :
    (a + 1).tdiv b = a.tdiv b + if (0 < a + 1 ∧ b ∣ a + 1) ∨ (a < 0 ∧ b ∣ a) then b.sign else 0 := by
  if hb : b = 0 then
    simp [hb]
  else
    induction b using wlog_sign
    case inv => simp; omega
    rename_i c
    norm_cast at hb
    have : 0 < (c : Int) := by omega
    simp [sign_ofNat_of_nonzero hb, add_one_tdiv_of_pos this]
Increment Truncating Division Formula: $(a + 1) \operatorname{tdiv} b = a \operatorname{tdiv} b + \text{adjustment}$
Informal description
For any integers $a$ and $b$, the truncating division of $a + 1$ by $b$ satisfies: $$(a + 1) \operatorname{tdiv} b = a \operatorname{tdiv} b + \begin{cases} \operatorname{sign}(b) & \text{if } (0 < a + 1 \text{ and } b \mid a + 1) \text{ or } (a < 0 \text{ and } b \mid a), \\ 0 & \text{otherwise.} \end{cases}$$
Int.tdiv_le_tdiv theorem
{a b c : Int} (H : 0 < c) (H' : a ≤ b) : a.tdiv c ≤ b.tdiv c
Full source
protected theorem tdiv_le_tdiv {a b c : Int} (H : 0 < c) (H' : a ≤ b) : a.tdiv c ≤ b.tdiv c := by
  obtain ⟨d, rfl⟩ := Int.exists_add_of_le H'
  clear H'
  induction d with
  | zero => simp
  | succ d ih =>
    simp only [Int.natCast_add, cast_ofNat_Int, ← Int.add_assoc]
    rw [add_one_tdiv_of_pos (by omega)]
    omega
Monotonicity of Truncating Division with Positive Divisor: $a \leq b \implies a \operatorname{tdiv} c \leq b \operatorname{tdiv} c$ for $c > 0$
Informal description
For any integers $a$, $b$, and $c$ with $c > 0$, if $a \leq b$, then the truncating division of $a$ by $c$ is less than or equal to the truncating division of $b$ by $c$, i.e., $a \operatorname{tdiv} c \leq b \operatorname{tdiv} c$.
Int.add_mul_fdiv_right theorem
(a b : Int) {c : Int} (H : c ≠ 0) : (a + b * c).fdiv c = a.fdiv c + b
Full source
theorem add_mul_fdiv_right (a b : Int) {c : Int} (H : c ≠ 0) : (a + b * c).fdiv c = a.fdiv c + b := by
  rw [fdiv_eq_ediv, add_mul_ediv_right _ _ H, fdiv_eq_ediv]
  simp only [Int.dvd_add_left (Int.dvd_mul_left _ _)]
  split <;> omega
Floor Division Identity: $\lfloor (a + b c)/c \rfloor = \lfloor a/c \rfloor + b$ for $c \neq 0$
Informal description
For any integers $a$, $b$, and $c$ with $c \neq 0$, the flooring division of $a + b \cdot c$ by $c$ equals the flooring division of $a$ by $c$ plus $b$, i.e., \[ \left\lfloor \frac{a + b \cdot c}{c} \right\rfloor = \left\lfloor \frac{a}{c} \right\rfloor + b. \]
Int.add_mul_fdiv_left theorem
(a : Int) {b : Int} (c : Int) (H : b ≠ 0) : (a + b * c).fdiv b = a.fdiv b + c
Full source
theorem add_mul_fdiv_left (a : Int) {b : Int}
    (c : Int) (H : b ≠ 0) : (a + b * c).fdiv b = a.fdiv b + c := by
  rw [Int.mul_comm, Int.add_mul_fdiv_right _ _ H]
Floor Division Identity: $\lfloor (a + b c)/b \rfloor = \lfloor a/b \rfloor + c$ for $b \neq 0$
Informal description
For any integers $a$, $c$, and a nonzero integer $b$, the flooring division of $a + b \cdot c$ by $b$ equals the flooring division of $a$ by $b$ plus $c$, i.e., \[ \left\lfloor \frac{a + b \cdot c}{b} \right\rfloor = \left\lfloor \frac{a}{b} \right\rfloor + c. \]
Int.mul_add_fdiv_right theorem
(a c : Int) {b : Int} (H : b ≠ 0) : (a * b + c).fdiv b = c.fdiv b + a
Full source
theorem mul_add_fdiv_right (a c : Int) {b : Int} (H : b ≠ 0) : (a * b + c).fdiv b = c.fdiv b + a := by
  rw [Int.add_comm, add_mul_fdiv_right _ _ H]
Floor Division Identity: $\lfloor (a b + c)/b \rfloor = \lfloor c/b \rfloor + a$ for $b \neq 0$
Informal description
For any integers $a$, $c$, and $b$ with $b \neq 0$, the flooring division of $a \cdot b + c$ by $b$ equals the flooring division of $c$ by $b$ plus $a$, i.e., \[ \left\lfloor \frac{a \cdot b + c}{b} \right\rfloor = \left\lfloor \frac{c}{b} \right\rfloor + a. \]
Int.mul_add_fdiv_left theorem
(b : Int) {a : Int} (c : Int) (H : a ≠ 0) : (a * b + c).fdiv a = c.fdiv a + b
Full source
theorem mul_add_fdiv_left (b : Int) {a : Int}
    (c : Int) (H : a ≠ 0) : (a * b + c).fdiv a = c.fdiv a + b := by
  rw [Int.add_comm, add_mul_fdiv_left _ _ H]
Floor Division Identity: $\lfloor (a b + c)/a \rfloor = \lfloor c/a \rfloor + b$ for $a \neq 0$
Informal description
For any integers $b$, $c$, and a nonzero integer $a$, the flooring division of $a \cdot b + c$ by $a$ equals the flooring division of $c$ by $a$ plus $b$, i.e., \[ \left\lfloor \frac{a \cdot b + c}{a} \right\rfloor = \left\lfloor \frac{c}{a} \right\rfloor + b. \]
Int.sub_mul_fdiv_right theorem
(a b : Int) {c : Int} (H : c ≠ 0) : (a - b * c).fdiv c = a.fdiv c - b
Full source
theorem sub_mul_fdiv_right (a b : Int) {c : Int} (H : c ≠ 0) : (a - b * c).fdiv c = a.fdiv c - b := by
  rw [Int.sub_eq_add_neg, ← Int.neg_mul, add_mul_fdiv_right _ _ H, Int.sub_eq_add_neg]
Floor Division Identity: $\lfloor (a - b c)/c \rfloor = \lfloor a/c \rfloor - b$ for $c \neq 0$
Informal description
For any integers $a$, $b$, and $c$ with $c \neq 0$, the flooring division of $a - b \cdot c$ by $c$ equals the flooring division of $a$ by $c$ minus $b$, i.e., \[ \left\lfloor \frac{a - b \cdot c}{c} \right\rfloor = \left\lfloor \frac{a}{c} \right\rfloor - b. \]
Int.sub_mul_fdiv_left theorem
(a : Int) {b : Int} (c : Int) (H : b ≠ 0) : (a - b * c).fdiv b = a.fdiv b - c
Full source
theorem sub_mul_fdiv_left (a : Int) {b : Int}
    (c : Int) (H : b ≠ 0) : (a - b * c).fdiv b = a.fdiv b - c := by
  rw [Int.sub_eq_add_neg, ← Int.mul_neg, add_mul_fdiv_left _ _ H, Int.sub_eq_add_neg]
Floor Division Identity: $\lfloor (a - b c)/b \rfloor = \lfloor a/b \rfloor - c$ for $b \neq 0$
Informal description
For any integers $a$, $c$, and a nonzero integer $b$, the flooring division of $a - b \cdot c$ by $b$ equals the flooring division of $a$ by $b$ minus $c$, i.e., \[ \left\lfloor \frac{a - b \cdot c}{b} \right\rfloor = \left\lfloor \frac{a}{b} \right\rfloor - c. \]
Int.mul_sub_fdiv_right theorem
(a c : Int) {b : Int} (H : b ≠ 0) : (a * b - c).fdiv b = a + (-c).fdiv b
Full source
theorem mul_sub_fdiv_right (a c : Int) {b : Int} (H : b ≠ 0) : (a * b - c).fdiv b = a + (-c).fdiv b := by
  rw [Int.sub_eq_add_neg, Int.add_comm, add_mul_fdiv_right _ _ H, Int.add_comm]
Floor Division Identity: $\lfloor (a b - c)/b \rfloor = a + \lfloor (-c)/b \rfloor$ for $b \neq 0$
Informal description
For any integers $a$, $c$, and nonzero integer $b$, the flooring division of $a \cdot b - c$ by $b$ equals $a$ plus the flooring division of $-c$ by $b$, i.e., \[ \left\lfloor \frac{a \cdot b - c}{b} \right\rfloor = a + \left\lfloor \frac{-c}{b} \right\rfloor. \]
Int.mul_sub_fdiv_left theorem
(b : Int) {a : Int} (c : Int) (H : a ≠ 0) : (a * b - c).fdiv a = b + (-c).fdiv a
Full source
theorem mul_sub_fdiv_left (b : Int) {a : Int}
    (c : Int) (H : a ≠ 0) : (a * b - c).fdiv a = b + (-c).fdiv a := by
  rw [Int.sub_eq_add_neg, Int.add_comm, add_mul_fdiv_left _ _ H, Int.add_comm]
Floor Division Identity: $\lfloor (a b - c)/a \rfloor = b + \lfloor (-c)/a \rfloor$ for $a \neq 0$
Informal description
For any integers $b$, $c$, and a nonzero integer $a$, the flooring division of $a \cdot b - c$ by $a$ equals $b$ plus the flooring division of $-c$ by $a$, i.e., \[ \left\lfloor \frac{a \cdot b - c}{a} \right\rfloor = b + \left\lfloor \frac{-c}{a} \right\rfloor. \]
Int.mul_fdiv_cancel theorem
(a : Int) {b : Int} (H : b ≠ 0) : fdiv (a * b) b = a
Full source
@[simp] theorem mul_fdiv_cancel (a : Int) {b : Int} (H : b ≠ 0) : fdiv (a * b) b = a :=
  if b0 : 0 ≤ b then by
    rw [fdiv_eq_ediv_of_nonneg _ b0, mul_ediv_cancel _ H]
  else
    match a, b, Int.not_le.1 b0 with
    | 0, _, _ => by simp [Int.zero_mul]
    | succ a, -[b+1], _ => congrArg ofNat <| Nat.mul_div_cancel (succ a) b.succ_pos
    | -[a+1], -[b+1], _ => congrArg negSucc <| Nat.div_eq_of_lt_le
      (Nat.le_of_lt_succ <| Nat.mul_lt_mul_of_pos_right a.lt_succ_self b.succ_pos)
      (Nat.lt_succ_self _)
Cancellation Property of Floor Division: $\lfloor (a \cdot b)/b \rfloor = a$ for $b \neq 0$
Informal description
For any integers $a$ and $b$ with $b \neq 0$, the flooring division of $a \times b$ by $b$ equals $a$, i.e., $\lfloor (a \cdot b) / b \rfloor = a$.
Int.mul_fdiv_cancel_left theorem
(b : Int) (H : a ≠ 0) : fdiv (a * b) a = b
Full source
@[simp] theorem mul_fdiv_cancel_left (b : Int) (H : a ≠ 0) : fdiv (a * b) a = b :=
  Int.mul_comm .. ▸ Int.mul_fdiv_cancel _ H
Left Cancellation Property of Floor Division: $\lfloor (a \cdot b)/a \rfloor = b$ for $a \neq 0$
Informal description
For any integers $a$ and $b$ with $a \neq 0$, the flooring division of $a \times b$ by $a$ equals $b$, i.e., $\lfloor (a \cdot b) / a \rfloor = b$.
Int.add_fdiv_of_dvd_right theorem
{a b c : Int} (H : c ∣ b) : (a + b).fdiv c = a.fdiv c + b.fdiv c
Full source
theorem add_fdiv_of_dvd_right {a b c : Int} (H : c ∣ b) : (a + b).fdiv c = a.fdiv c + b.fdiv c := by
  by_cases h : c = 0
  · simp [h]
  · obtain ⟨d, rfl⟩ := H
    rw [add_mul_fdiv_left _ _ h]
    simp [h]
Additivity of Floor Division under Divisibility: $\lfloor (a + b)/c \rfloor = \lfloor a/c \rfloor + \lfloor b/c \rfloor$ when $c \mid b$
Informal description
For any integers $a$, $b$, and $c$ such that $c$ divides $b$, the flooring division of $a + b$ by $c$ equals the sum of the flooring divisions of $a$ by $c$ and $b$ by $c$, i.e., \[ \left\lfloor \frac{a + b}{c} \right\rfloor = \left\lfloor \frac{a}{c} \right\rfloor + \left\lfloor \frac{b}{c} \right\rfloor. \]
Int.add_fdiv_of_dvd_left theorem
{a b c : Int} (H : c ∣ a) : (a + b).fdiv c = a.fdiv c + b.fdiv c
Full source
theorem add_fdiv_of_dvd_left {a b c : Int} (H : c ∣ a) : (a + b).fdiv c = a.fdiv c + b.fdiv c := by
  rw [Int.add_comm, Int.add_fdiv_of_dvd_right H, Int.add_comm]
Additivity of Floor Division under Left Divisibility: $\lfloor (a + b)/c \rfloor = \lfloor a/c \rfloor + \lfloor b/c \rfloor$ when $c \mid a$
Informal description
For any integers $a$, $b$, and $c$ such that $c$ divides $a$, the flooring division of $a + b$ by $c$ equals the sum of the flooring divisions of $a$ by $c$ and $b$ by $c$, i.e., \[ \left\lfloor \frac{a + b}{c} \right\rfloor = \left\lfloor \frac{a}{c} \right\rfloor + \left\lfloor \frac{b}{c} \right\rfloor. \]
Int.fdiv_nonneg theorem
{a b : Int} (Ha : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a.fdiv b
Full source
theorem fdiv_nonneg {a b : Int} (Ha : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a.fdiv b :=
  match a, b, eq_ofNat_of_zero_le Ha, eq_ofNat_of_zero_le Hb with
  | _, _, ⟨_, rfl⟩, ⟨_, rfl⟩ => ofNat_fdiv .. ▸ ofNat_zero_le _
Nonnegativity of Floor Division for Nonnegative Integers
Informal description
For any integers $a$ and $b$ such that $0 \leq a$ and $0 \leq b$, the flooring division $a \operatorname{fdiv} b$ satisfies $0 \leq a \operatorname{fdiv} b$.
Int.fdiv_nonneg_of_nonpos_of_nonpos theorem
{a b : Int} (Ha : a ≤ 0) (Hb : b ≤ 0) : 0 ≤ a.fdiv b
Full source
theorem fdiv_nonneg_of_nonpos_of_nonpos {a b : Int} (Ha : a ≤ 0) (Hb : b ≤ 0) : 0 ≤ a.fdiv b := by
  rw [fdiv_eq_ediv]
  by_cases ha : a = 0
  · simp [ha]
  · by_cases hb : b = 0
    · simp [hb]
    · have : 0 < a / b := ediv_pos_of_neg_of_neg (by omega) (by omega)
      split <;> omega
Nonnegativity of Floor Division for Nonpositive Integers: $0 \leq \lfloor a / b \rfloor$ when $a, b \leq 0$
Informal description
For any integers $a$ and $b$ such that $a \leq 0$ and $b \leq 0$, the flooring division $a \operatorname{fdiv} b$ satisfies $0 \leq a \operatorname{fdiv} b$.
Int.fdiv_nonpos_of_nonneg_of_nonpos theorem
: ∀ {a b : Int}, 0 ≤ a → b ≤ 0 → a.fdiv b ≤ 0
Full source
theorem fdiv_nonpos_of_nonneg_of_nonpos : ∀ {a b : Int}, 0 ≤ a → b ≤ 0 → a.fdiv b ≤ 0
  | 0, 0, _, _ | 0, -[_+1], _, _ | succ _, 0, _, _ | succ _, -[_+1], _, _ => by
    simp [fdiv, negSucc_le_zero]
Nonpositivity of Floor Division for Nonnegative Numerator and Nonpositive Denominator
Informal description
For any integers $a$ and $b$ such that $0 \leq a$ and $b \leq 0$, the flooring division $a \operatorname{fdiv} b$ satisfies $a \operatorname{fdiv} b \leq 0$.
Int.fdiv_nonpos abbrev
Full source
@[deprecated fdiv_nonpos_of_nonneg_of_nonpos (since := "2025-03-04")]
abbrev fdiv_nonpos := @fdiv_nonpos_of_nonneg_of_nonpos
Nonpositivity of Floor Division for Nonnegative Numerator and Nonpositive Denominator
Informal description
For any integers $a$ and $b$ such that $0 \leq a$ and $b \leq 0$, the flooring division $a \operatorname{fdiv} b$ satisfies $a \operatorname{fdiv} b \leq 0$.
Int.fdiv_neg_of_neg_of_pos theorem
: ∀ {a b : Int}, a < 0 → 0 < b → a.fdiv b < 0
Full source
theorem fdiv_neg_of_neg_of_pos : ∀ {a b : Int}, a < 0 → 0 < b → a.fdiv b < 0
  | -[_+1], succ _, _, _ => negSucc_lt_zero _
Negativity of Floor Division for Negative Numerator and Positive Denominator
Informal description
For any integers $a$ and $b$ such that $a < 0$ and $0 < b$, the flooring division $a \operatorname{fdiv} b$ satisfies $a \operatorname{fdiv} b < 0$.
Int.fdiv_eq_zero_of_lt theorem
{a b : Int} (H1 : 0 ≤ a) (H2 : a < b) : a.fdiv b = 0
Full source
theorem fdiv_eq_zero_of_lt {a b : Int} (H1 : 0 ≤ a) (H2 : a < b) : a.fdiv b = 0 := by
  rw [fdiv_eq_ediv, if_pos, Int.sub_zero]
  · apply ediv_eq_zero_of_lt (by omega) (by omega)
  · left; omega
Floor Division Yields Zero for Nonnegative Dividend Less Than Divisor: $\lfloor a / b \rfloor = 0$ when $0 \leq a < b$
Informal description
For any integers $a$ and $b$ such that $0 \leq a$ and $a < b$, the flooring division of $a$ by $b$ equals zero, i.e., $\lfloor a / b \rfloor = 0$.
Int.mul_fdiv_mul_of_pos theorem
{a : Int} (b c : Int) (H : 0 < a) : (a * b).fdiv (a * c) = b.fdiv c
Full source
@[simp] theorem mul_fdiv_mul_of_pos {a : Int}
    (b c : Int) (H : 0 < a) : (a * b).fdiv (a * c) = b.fdiv c := by
  rw [fdiv_eq_ediv, mul_ediv_mul_of_pos _ _ H, fdiv_eq_ediv]
  congr 2
  simp [Int.mul_dvd_mul_iff_left (Int.ne_of_gt H)]
  constructor
  · rintro (h | h)
    · exact .inl (Int.nonneg_of_mul_nonneg_right h H)
    · exact .inr h
  · rintro (h | h)
    · exact .inl (Int.mul_nonneg (by omega) h)
    · exact .inr h
Cancellation of Positive Factor in Floor Division: $\lfloor (a \cdot b)/(a \cdot c) \rfloor = \lfloor b/c \rfloor$ for $a > 0$
Informal description
For any integers $b$ and $c$, and any positive integer $a > 0$, the flooring division satisfies: \[ \lfloor (a \cdot b) / (a \cdot c) \rfloor = \lfloor b / c \rfloor \]
Int.mul_fdiv_mul_of_pos_left theorem
(a : Int) {b : Int} (c : Int) (H : 0 < b) : (a * b).fdiv (c * b) = a.fdiv c
Full source
@[simp] theorem mul_fdiv_mul_of_pos_left
    (a : Int) {b : Int} (c : Int) (H : 0 < b) : (a * b).fdiv (c * b) = a.fdiv c := by
  rw [Int.mul_comm a b, Int.mul_comm c b, Int.mul_fdiv_mul_of_pos _ _ H]
Cancellation of Positive Factor in Floor Division (Left Version): $\lfloor (a \cdot b)/(c \cdot b) \rfloor = \lfloor a/c \rfloor$ for $b > 0$
Informal description
For any integers $a$ and $c$, and any positive integer $b > 0$, the flooring division satisfies: \[ \lfloor (a \cdot b) / (c \cdot b) \rfloor = \lfloor a / c \rfloor \]
Int.fdiv_one theorem
: ∀ a : Int, a.fdiv 1 = a
Full source
@[simp] theorem fdiv_one : ∀ a : Int, a.fdiv 1 = a
  | 0 => rfl
  | succ _ => congrArg Nat.cast (Nat.div_one _)
  | -[_+1] => congrArg negSucc (Nat.div_one _)
Floor Division Identity: $\lfloor a / 1 \rfloor = a$
Informal description
For any integer $a$, the flooring division of $a$ by $1$ equals $a$, i.e., $\lfloor a / 1 \rfloor = a$.
Int.fdiv_eq_of_eq_mul_right theorem
{a b c : Int} (H1 : b ≠ 0) (H2 : a = b * c) : a.fdiv b = c
Full source
protected theorem fdiv_eq_of_eq_mul_right {a b c : Int}
    (H1 : b ≠ 0) (H2 : a = b * c) : a.fdiv b = c := by rw [H2, Int.mul_fdiv_cancel_left _ H1]
Floor Division of Product Equals Factor When Nonzero: $\lfloor a / b \rfloor = c$ for $a = b \cdot c$ and $b \neq 0$
Informal description
For any integers $a$, $b$, and $c$ with $b \neq 0$, if $a = b \cdot c$, then the flooring division of $a$ by $b$ equals $c$, i.e., $\lfloor a / b \rfloor = c$.
Int.fdiv_eq_of_eq_mul_left theorem
{a b c : Int} (H1 : b ≠ 0) (H2 : a = c * b) : a.fdiv b = c
Full source
protected theorem fdiv_eq_of_eq_mul_left {a b c : Int}
    (H1 : b ≠ 0) (H2 : a = c * b) : a.fdiv b = c :=
  Int.fdiv_eq_of_eq_mul_right H1 (by rw [Int.mul_comm, H2])
Floor Division of Product Equals Factor When Nonzero (Left Version): $\lfloor a / b \rfloor = c$ for $a = c \cdot b$ and $b \neq 0$
Informal description
For any integers $a$, $b$, and $c$ with $b \neq 0$, if $a = c \cdot b$, then the flooring division of $a$ by $b$ equals $c$, i.e., $\lfloor a / b \rfloor = c$.
Int.fdiv_self theorem
{a : Int} (H : a ≠ 0) : a.fdiv a = 1
Full source
@[simp] protected theorem fdiv_self {a : Int} (H : a ≠ 0) : a.fdiv a = 1 := by
  have := Int.mul_fdiv_cancel 1 H; rwa [Int.one_mul] at this
Floor Division Identity: $\lfloor a / a \rfloor = 1$ for $a \neq 0$
Informal description
For any nonzero integer $a$, the flooring division of $a$ by itself equals $1$, i.e., $\lfloor a / a \rfloor = 1$.
Int.neg_fdiv theorem
{a b : Int} : (-a).fdiv b = -(a.fdiv b) - if b = 0 ∨ b ∣ a then 0 else 1
Full source
theorem neg_fdiv {a b : Int} : (-a).fdiv b = -(a.fdiv b) - if b = 0 ∨ b ∣ a then 0 else 1 := by
  rw [fdiv_eq_ediv, fdiv_eq_ediv, neg_ediv]
  simp
  by_cases h : b ∣ a
  · simp [h]
  · simp [h]
    by_cases h' : 0 ≤ b
    · by_cases h'' : b = 0
      · simp [h'']
      · simp only [h', ↓reduceIte, Int.sub_zero, h'']
        replace h' : 0 < b := by omega
        rw [sign_eq_one_of_pos (by omega)]
    · simp only [h', ↓reduceIte]
      rw [sign_eq_neg_one_of_neg (by omega), if_neg (by omega)]
      omega
Floor Division of Negated Numerator: $\lfloor (-a)/b \rfloor = -\lfloor a/b \rfloor - \mathbb{1}_{b \neq 0 \land b \nmid a}$
Informal description
For any integers $a$ and $b$, the flooring division of $-a$ by $b$ satisfies: \[ \lfloor (-a)/b \rfloor = -\lfloor a/b \rfloor - \begin{cases} 0 & \text{if } b = 0 \text{ or } b \mid a \\ 1 & \text{otherwise} \end{cases} \]
Int.neg_fdiv_neg theorem
(a b : Int) : (-a).fdiv (-b) = a.fdiv b
Full source
@[simp] protected theorem neg_fdiv_neg (a b : Int) : (-a).fdiv (-b) = a.fdiv b := by
  match a, b with
  | 0, 0 => rfl
  | 0, ofNat b => simp
  | 0, -[b+1] => simp
  | ofNat (a + 1), 0 => simp
  | ofNat (a + 1), ofNat (b + 1) =>
    unfold fdiv
    simp only [ofNat_eq_coe, Int.natCast_add, cast_ofNat_Int, Nat.succ_eq_add_one]
    rw [← negSucc_eq, ← negSucc_eq]
  | ofNat (a + 1), -[b+1] =>
    unfold fdiv
    simp only [ofNat_eq_coe, Int.natCast_add, cast_ofNat_Int, Nat.succ_eq_add_one]
    rw [← negSucc_eq, neg_negSucc]
  | -[a+1], 0 => simp
  | -[a+1], ofNat (b + 1) =>
    unfold fdiv
    simp only [ofNat_eq_coe, Int.natCast_add, cast_ofNat_Int, Nat.succ_eq_add_one]
    rw [neg_negSucc, ← negSucc_eq]
  | -[a+1], -[b+1] =>
    unfold fdiv
    simp only [ofNat_eq_coe, ofNat_ediv, Nat.succ_eq_add_one, Int.natCast_add, cast_ofNat_Int]
    rw [neg_negSucc, neg_negSucc]
    simp
Floor Division of Negated Integers: $\lfloor (-a)/(-b) \rfloor = \lfloor a/b \rfloor$
Informal description
For any integers $a$ and $b$, the flooring division of $-a$ by $-b$ equals the flooring division of $a$ by $b$, i.e., $\lfloor (-a)/(-b) \rfloor = \lfloor a/b \rfloor$.
Int.fdiv_neg theorem
{a b : Int} (h : b ≠ 0) : a.fdiv (-b) = if b ∣ a then -(a.fdiv b) else -(a.fdiv b) - 1
Full source
theorem fdiv_neg {a b : Int} (h : b ≠ 0) : a.fdiv (-b) = if b ∣ a then -(a.fdiv b) else -(a.fdiv b) - 1 := by
  rw [← Int.neg_fdiv_neg, Int.neg_neg, neg_fdiv]
  simp only [h, false_or]
  split <;> omega
Floor Division by Negative Denominator: $\lfloor a/(-b) \rfloor$ in Terms of $\lfloor a/b \rfloor$
Informal description
For any integers $a$ and $b$ with $b \neq 0$, the flooring division of $a$ by $-b$ satisfies: \[ \lfloor a/(-b) \rfloor = \begin{cases} -\lfloor a/b \rfloor & \text{if } b \mid a \\ -\lfloor a/b \rfloor - 1 & \text{otherwise} \end{cases} \]
Int.ofNat_fmod theorem
(m n : Nat) : ↑(m % n) = fmod m n
Full source
theorem ofNat_fmod (m n : Nat) : ↑(m % n) = fmod m n := by
  cases m <;> simp [fmod, Nat.succ_eq_add_one]
Preservation of Modulo Operation in Natural-to-Integer Cast: $(m \% n : \mathbb{N}) = \operatorname{fmod}(m, n)$
Informal description
For any natural numbers $m$ and $n$, the canonical map from natural numbers to integers preserves the modulo operation, i.e., $(m \% n : \mathbb{N}) = \operatorname{fmod}(m, n)$ where $\operatorname{fmod}$ is the flooring modulus operation on integers.
Int.fmod_nonneg theorem
{a b : Int} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a.fmod b
Full source
theorem fmod_nonneg {a b : Int} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a.fmod b :=
  fmod_eq_tmod_of_nonneg ha hb ▸ tmod_nonneg _ ha
Nonnegativity of Flooring Modulus for Nonnegative Integers
Informal description
For any integers $a$ and $b$ such that $a \geq 0$ and $b \geq 0$, the flooring modulus $a \operatorname{fmod} b$ is nonnegative, i.e., $0 \leq a \operatorname{fmod} b$.
Int.fmod_nonneg_of_pos theorem
(a : Int) {b : Int} (hb : 0 < b) : 0 ≤ a.fmod b
Full source
theorem fmod_nonneg_of_pos (a : Int) {b : Int} (hb : 0 < b) : 0 ≤ a.fmod b :=
  fmod_eq_emod_of_nonneg _ (Int.le_of_lt hb) ▸ emod_nonneg _ (Int.ne_of_lt hb).symm
Non-negativity of Flooring Modulus for Positive Divisors: $0 \leq a \operatorname{fmod} b$ when $b > 0$
Informal description
For any integer $a$ and any positive integer $b$ (i.e., $b > 0$), the flooring modulus $a \operatorname{fmod} b$ is non-negative, i.e., $0 \leq a \operatorname{fmod} b$.
Int.fmod_nonneg' abbrev
Full source
@[deprecated fmod_nonneg_of_pos (since := "2025-03-04")]
abbrev fmod_nonneg' := @fmod_nonneg_of_pos
Non-negativity of Flooring Modulus for Positive Divisors: $0 \leq a \operatorname{fmod} b$ when $b > 0$
Informal description
For any integer $a$ and any positive integer $b$ (i.e., $b > 0$), the flooring modulus $a \operatorname{fmod} b$ is non-negative, i.e., $0 \leq a \operatorname{fmod} b$.
Int.fmod_lt_of_pos theorem
(a : Int) {b : Int} (H : 0 < b) : a.fmod b < b
Full source
theorem fmod_lt_of_pos (a : Int) {b : Int} (H : 0 < b) : a.fmod b < b :=
  fmod_eq_emod_of_nonneg _ (Int.le_of_lt H) ▸ emod_lt_of_pos a H
Flooring Modulus Bounds: $0 \leq \operatorname{fmod}(a,b) < b$ for $b > 0$
Informal description
For any integer $a$ and any positive integer $b > 0$, the flooring modulus of $a$ modulo $b$ satisfies $0 \leq \operatorname{fmod}(a, b) < b$.
Int.add_mul_fmod_self theorem
{a b c : Int} : (a + b * c).fmod c = a.fmod c
Full source
@[simp] theorem add_mul_fmod_self {a b c : Int} : (a + b * c).fmod c = a.fmod c := by
  rw [fmod_eq_emod, add_mul_emod_self, fmod_eq_emod]
  simp
Flooring Modulus Invariance under Addition of Multiple: $\operatorname{fmod}(a + b \cdot c, c) = \operatorname{fmod}(a, c)$
Informal description
For any integers $a$, $b$, and $c$, the flooring modulus of $a + b \cdot c$ with respect to $c$ equals the flooring modulus of $a$ with respect to $c$, i.e., $\operatorname{fmod}(a + b \cdot c, c) = \operatorname{fmod}(a, c)$.
Int.add_mul_fmod_self_left theorem
(a b c : Int) : (a + b * c).fmod b = a.fmod b
Full source
@[simp] theorem add_mul_fmod_self_left (a b c : Int) : (a + b * c).fmod b = a.fmod b := by
  rw [Int.mul_comm, Int.add_mul_fmod_self]
Flooring Modulus Invariance under Left Addition of Multiple: $\operatorname{fmod}(a + b \cdot c, b) = \operatorname{fmod}(a, b)$
Informal description
For any integers $a$, $b$, and $c$, the flooring modulus of $a + b \cdot c$ with respect to $b$ equals the flooring modulus of $a$ with respect to $b$, i.e., $\operatorname{fmod}(a + b \cdot c, b) = \operatorname{fmod}(a, b)$.
Int.fmod_add_fmod theorem
(m n k : Int) : (m.fmod n + k).fmod n = (m + k).fmod n
Full source
@[simp] theorem fmod_add_fmod (m n k : Int) : (m.fmod n + k).fmod n = (m + k).fmod n := by
  by_cases h : n = 0
  · simp [h]
  rw [fmod_def, fmod_def]
  conv => rhs; rw [fmod_def]
  have : m - n * m.fdiv n + k = m + k + n * (- m.fdiv n) := by simp [Int.mul_neg]; omega
  rw [this, add_fdiv_of_dvd_right (Int.dvd_mul_right ..), Int.mul_add, mul_fdiv_cancel_left _ h]
  omega
Flooring Modulus Addition Property: $\operatorname{fmod}(\operatorname{fmod}(m, n) + k, n) = \operatorname{fmod}(m + k, n)$
Informal description
For any integers $m$, $n$, and $k$, the flooring modulus of the sum of the flooring modulus of $m$ with respect to $n$ and $k$, with respect to $n$, equals the flooring modulus of the sum of $m$ and $k$ with respect to $n$. In other words: \[ \operatorname{fmod}(\operatorname{fmod}(m, n) + k, n) = \operatorname{fmod}(m + k, n). \]
Int.add_fmod_fmod theorem
(m n k : Int) : (m + n.fmod k).fmod k = (m + n).fmod k
Full source
@[simp] theorem add_fmod_fmod (m n k : Int) : (m + n.fmod k).fmod k = (m + n).fmod k := by
  rw [Int.add_comm, Int.fmod_add_fmod, Int.add_comm]
Flooring Modulus Addition Property: $\operatorname{fmod}(m + \operatorname{fmod}(n, k), k) = \operatorname{fmod}(m + n, k)$
Informal description
For any integers $m$, $n$, and $k$, the flooring modulus of the sum of $m$ and the flooring modulus of $n$ with respect to $k$, with respect to $k$, equals the flooring modulus of the sum of $m$ and $n$ with respect to $k$. In other words: \[ \operatorname{fmod}(m + \operatorname{fmod}(n, k), k) = \operatorname{fmod}(m + n, k). \]
Int.add_fmod theorem
(a b n : Int) : (a + b).fmod n = (a.fmod n + b.fmod n).fmod n
Full source
theorem add_fmod (a b n : Int) : (a + b).fmod n = (a.fmod n + b.fmod n).fmod n := by
  simp
Additivity of Flooring Modulus: $\operatorname{fmod}(a + b, n) = \operatorname{fmod}(\operatorname{fmod}(a, n) + \operatorname{fmod}(b, n), n)$
Informal description
For any integers $a$, $b$, and $n$, the flooring modulus of the sum $a + b$ with respect to $n$ is equal to the flooring modulus of the sum of the flooring moduli of $a$ and $b$ with respect to $n$, i.e., \[ \operatorname{fmod}(a + b, n) = \operatorname{fmod}(\operatorname{fmod}(a, n) + \operatorname{fmod}(b, n), n). \]
Int.add_fmod_eq_add_fmod_right theorem
{m n k : Int} (i : Int) (H : m.fmod n = k.fmod n) : (m + i).fmod n = (k + i).fmod n
Full source
theorem add_fmod_eq_add_fmod_right {m n k : Int} (i : Int)
    (H : m.fmod n = k.fmod n) : (m + i).fmod n = (k + i).fmod n := by
  rw [add_fmod]
  conv => rhs; rw [add_fmod]
  rw [H]
Invariance of Flooring Modulus under Addition: $\operatorname{fmod}(m + i, n) = \operatorname{fmod}(k + i, n)$ when $\operatorname{fmod}(m, n) = \operatorname{fmod}(k, n)$
Informal description
For any integers $m$, $n$, $k$, and $i$, if the flooring moduli of $m$ and $k$ with respect to $n$ are equal (i.e., $\operatorname{fmod}(m, n) = \operatorname{fmod}(k, n)$), then the flooring moduli of $m + i$ and $k + i$ with respect to $n$ are also equal, i.e., \[ \operatorname{fmod}(m + i, n) = \operatorname{fmod}(k + i, n). \]
Int.fmod_add_cancel_right theorem
{m n k : Int} (i) : (m + i).fmod n = (k + i).fmod n ↔ m.fmod n = k.fmod n
Full source
theorem fmod_add_cancel_right {m n k : Int} (i) : (m + i).fmod n = (k + i).fmod n ↔ m.fmod n = k.fmod n :=
  ⟨fun H => by
    have := add_fmod_eq_add_fmod_right (-i) H
    rwa [Int.add_neg_cancel_right, Int.add_neg_cancel_right] at this,
  add_fmod_eq_add_fmod_right _⟩
Equivalence of Flooring Moduli under Addition: $\operatorname{fmod}(m + i, n) = \operatorname{fmod}(k + i, n) \leftrightarrow \operatorname{fmod}(m, n) = \operatorname{fmod}(k, n)$
Informal description
For any integers $m$, $n$, $k$, and $i$, the flooring moduli of $m + i$ and $k + i$ with respect to $n$ are equal if and only if the flooring moduli of $m$ and $k$ with respect to $n$ are equal, i.e., \[ \operatorname{fmod}(m + i, n) = \operatorname{fmod}(k + i, n) \leftrightarrow \operatorname{fmod}(m, n) = \operatorname{fmod}(k, n). \]
Int.mul_fmod_left theorem
(a b : Int) : (a * b).fmod b = 0
Full source
@[simp] theorem mul_fmod_left (a b : Int) : (a * b).fmod b = 0 :=
  if h : b = 0 then by simp [h, Int.mul_zero] else by
    rw [Int.fmod_def, Int.mul_fdiv_cancel _ h, Int.mul_comm, Int.sub_self]
Flooring Modulus of Product with Respect to Right Factor: $(a \times b) \operatorname{fmod} b = 0$
Informal description
For any integers $a$ and $b$, the flooring modulus of the product $a \times b$ with respect to $b$ is zero, i.e., $(a \times b) \operatorname{fmod} b = 0$.
Int.mul_fmod_right theorem
(a b : Int) : (a * b).fmod a = 0
Full source
@[simp] theorem mul_fmod_right (a b : Int) : (a * b).fmod a = 0 := by
  rw [Int.mul_comm, mul_fmod_left]
Flooring Modulus of Product with Respect to Left Factor: $(a \times b) \operatorname{fmod} a = 0$
Informal description
For any integers $a$ and $b$, the flooring modulus of the product $a \times b$ with respect to $a$ is zero, i.e., $(a \times b) \operatorname{fmod} a = 0$.
Int.mul_fmod theorem
(a b n : Int) : (a * b).fmod n = (a.fmod n * b.fmod n).fmod n
Full source
theorem mul_fmod (a b n : Int) : (a * b).fmod n = (a.fmod n * b.fmod n).fmod n := by
  conv => lhs; rw [
    ← fmod_add_fdiv a n, ← fmod_add_fdiv' b n, Int.add_mul, Int.mul_add, Int.mul_add,
    Int.mul_assoc, Int.mul_assoc, ← Int.mul_add n _ _, add_mul_fmod_self_left,
    ← Int.mul_assoc, add_mul_fmod_self]
Modular Multiplicativity of Flooring Modulus: $\operatorname{fmod}(a \cdot b, n) = \operatorname{fmod}(\operatorname{fmod}(a, n) \cdot \operatorname{fmod}(b, n), n)$
Informal description
For any integers $a$, $b$, and $n$, the flooring modulus of the product $a \cdot b$ with respect to $n$ is equal to the flooring modulus of the product of the flooring moduli of $a$ and $b$ with respect to $n$, i.e., \[ \operatorname{fmod}(a \cdot b, n) = \operatorname{fmod}(\operatorname{fmod}(a, n) \cdot \operatorname{fmod}(b, n), n). \]
Int.fmod_self theorem
{a : Int} : a.fmod a = 0
Full source
@[simp] theorem fmod_self {a : Int} : a.fmod a = 0 := by
  have := mul_fmod_left 1 a; rwa [Int.one_mul] at this
Flooring Modulus of Integer with Itself: $a \operatorname{fmod} a = 0$
Informal description
For any integer $a$, the flooring modulus of $a$ with respect to itself is zero, i.e., $a \operatorname{fmod} a = 0$.
Int.fmod_fmod_of_dvd theorem
(n : Int) {m k : Int} (h : m ∣ k) : (n.fmod k).fmod m = n.fmod m
Full source
@[simp] theorem fmod_fmod_of_dvd (n : Int) {m k : Int}
    (h : m ∣ k) : (n.fmod k).fmod m = n.fmod m := by
  conv => rhs; rw [← fmod_add_fdiv n k]
  match k, h with
  | _, ⟨t, rfl⟩ => rw [Int.mul_assoc, add_mul_fmod_self_left]
Nested Flooring Modulus under Divisibility: $\operatorname{fmod}(\operatorname{fmod}(n, k), m) = \operatorname{fmod}(n, m)$ when $m \mid k$
Informal description
For any integers $n$, $m$, and $k$ such that $m$ divides $k$, the flooring modulus of the flooring modulus of $n$ with respect to $k$ with respect to $m$ equals the flooring modulus of $n$ with respect to $m$, i.e., \[ \operatorname{fmod}(\operatorname{fmod}(n, k), m) = \operatorname{fmod}(n, m). \]
Int.fmod_fmod theorem
(a b : Int) : (a.fmod b).fmod b = a.fmod b
Full source
@[simp] theorem fmod_fmod (a b : Int) : (a.fmod b).fmod b = a.fmod b :=
  fmod_fmod_of_dvd _ (Int.dvd_refl b)
Idempotence of Flooring Modulus: $\operatorname{fmod}(\operatorname{fmod}(a, b), b) = \operatorname{fmod}(a, b)$
Informal description
For any integers $a$ and $b$, the flooring modulus of $(a \operatorname{fmod} b)$ with respect to $b$ equals $a \operatorname{fmod} b$, i.e., \[ \operatorname{fmod}(\operatorname{fmod}(a, b), b) = \operatorname{fmod}(a, b). \]
Int.sub_fmod theorem
(a b n : Int) : (a - b).fmod n = (a.fmod n - b.fmod n).fmod n
Full source
theorem sub_fmod (a b n : Int) : (a - b).fmod n = (a.fmod n - b.fmod n).fmod n := by
  apply (fmod_add_cancel_right b).mp
  rw [Int.sub_add_cancel, ← Int.add_fmod_fmod, Int.sub_add_cancel, fmod_fmod]
Flooring Modulus of Difference: $\operatorname{fmod}(a - b, n) = \operatorname{fmod}(\operatorname{fmod}(a, n) - \operatorname{fmod}(b, n), n)$
Informal description
For any integers $a$, $b$, and $n$, the flooring modulus of the difference $a - b$ with respect to $n$ equals the flooring modulus of the difference of the flooring moduli of $a$ and $b$ with respect to $n$, i.e., \[ \operatorname{fmod}(a - b, n) = \operatorname{fmod}(\operatorname{fmod}(a, n) - \operatorname{fmod}(b, n), n). \]
Int.fmod_eq_zero_of_dvd theorem
: ∀ {a b : Int}, a ∣ b → b.fmod a = 0
Full source
theorem fmod_eq_zero_of_dvd : ∀ {a b : Int}, a ∣ b → b.fmod a = 0
  | _, _, ⟨_, rfl⟩ => mul_fmod_right ..
Flooring Modulus Zero for Divisible Integers: $a \mid b \Rightarrow b \operatorname{fmod} a = 0$
Informal description
For any integers $a$ and $b$, if $a$ divides $b$, then the flooring modulus of $b$ with respect to $a$ is zero, i.e., $b \operatorname{fmod} a = 0$.
Int.fmod_eq_of_lt theorem
{a b : Int} (H1 : 0 ≤ a) (H2 : a < b) : a.fmod b = a
Full source
theorem fmod_eq_of_lt {a b : Int} (H1 : 0 ≤ a) (H2 : a < b) : a.fmod b = a := by
  rw [fmod_eq_emod_of_nonneg _ (Int.le_trans H1 (Int.le_of_lt H2)), emod_eq_of_lt H1 H2]
Flooring Modulus Identity for Nonnegative Integers Under Strict Inequality: $\operatorname{fmod}(a,b) = a$ when $0 \leq a < b$
Informal description
For any integers $a$ and $b$ such that $0 \leq a$ and $a < b$, the flooring modulus satisfies $\operatorname{fmod}(a, b) = a$.
Int.neg_fmod_neg theorem
(a b : Int) : (-a).fmod (-b) = -a.fmod b
Full source
@[simp] protected theorem neg_fmod_neg (a b : Int) : (-a).fmod (-b) = -a.fmod b := by
  rw [fmod_def, Int.neg_fdiv_neg, fmod_def, Int.neg_mul]
  omega
Negation Identity for Flooring Modulus: $\operatorname{fmod}(-a, -b) = -\operatorname{fmod}(a, b)$
Informal description
For any integers $a$ and $b$, the flooring modulus of $-a$ with respect to $-b$ equals the negation of the flooring modulus of $a$ with respect to $b$, i.e., \[ \operatorname{fmod}(-a, -b) = -\operatorname{fmod}(a, b). \]
Int.mul_fdiv_cancel_of_fmod_eq_zero theorem
{a b : Int} (H : a.fmod b = 0) : b * (a.fdiv b) = a
Full source
theorem mul_fdiv_cancel_of_fmod_eq_zero {a b : Int} (H : a.fmod b = 0) : b * (a.fdiv b) = a := by
  have := fmod_add_fdiv a b; rwa [H, Int.zero_add] at this
Flooring Division Cancellation: $b \cdot \lfloor a / b \rfloor = a$ when $\operatorname{fmod}(a, b) = 0$
Informal description
For any integers $a$ and $b$ such that the flooring modulus $\operatorname{fmod}(a, b) = 0$, the product of $b$ and the flooring division $\operatorname{fdiv}(a, b)$ equals $a$, i.e., \[ b \cdot \lfloor a / b \rfloor = a. \]
Int.fdiv_mul_cancel_of_fmod_eq_zero theorem
{a b : Int} (H : a.fmod b = 0) : (a.fdiv b) * b = a
Full source
theorem fdiv_mul_cancel_of_fmod_eq_zero {a b : Int} (H : a.fmod b = 0) : (a.fdiv b) * b= a := by
  rw [Int.mul_comm, mul_fdiv_cancel_of_fmod_eq_zero H]
Flooring Division Cancellation: $\lfloor a / b \rfloor \cdot b = a$ when $\operatorname{fmod}(a, b) = 0$
Informal description
For any integers $a$ and $b$ such that the flooring modulus $\operatorname{fmod}(a, b) = 0$, the product of the flooring division $\operatorname{fdiv}(a, b)$ and $b$ equals $a$, i.e., \[ \lfloor a / b \rfloor \cdot b = a. \]
Int.dvd_of_fmod_eq_zero theorem
{a b : Int} (H : b.fmod a = 0) : a ∣ b
Full source
theorem dvd_of_fmod_eq_zero {a b : Int} (H : b.fmod a = 0) : a ∣ b :=
  ⟨b.fdiv a, (mul_fdiv_cancel_of_fmod_eq_zero H).symm⟩
Divisibility Criterion via Flooring Modulus: $\operatorname{fmod}(b, a) = 0 \implies a \mid b$
Informal description
For any integers $a$ and $b$, if the flooring modulus of $b$ with respect to $a$ is zero (i.e., $\operatorname{fmod}(b, a) = 0$), then $a$ divides $b$ (i.e., $a \mid b$).
Int.dvd_iff_fmod_eq_zero theorem
{a b : Int} : a ∣ b ↔ b.fmod a = 0
Full source
theorem dvd_iff_fmod_eq_zero {a b : Int} : a ∣ ba ∣ b ↔ b.fmod a = 0 :=
  ⟨fmod_eq_zero_of_dvd, dvd_of_fmod_eq_zero⟩
Divisibility Criterion via Flooring Modulus: $a \mid b \leftrightarrow b \operatorname{fmod} a = 0$
Informal description
For any integers $a$ and $b$, $a$ divides $b$ if and only if the flooring modulus of $b$ with respect to $a$ is zero, i.e., $a \mid b \leftrightarrow b \operatorname{fmod} a = 0$.
Int.fdiv_mul_cancel theorem
{a b : Int} (H : b ∣ a) : a.fdiv b * b = a
Full source
protected theorem fdiv_mul_cancel {a b : Int} (H : b ∣ a) : a.fdiv b * b = a :=
  fdiv_mul_cancel_of_fmod_eq_zero (fmod_eq_zero_of_dvd H)
Flooring Division Cancellation: $\lfloor a / b \rfloor \cdot b = a$ when $b \mid a$
Informal description
For any integers $a$ and $b$ such that $b$ divides $a$, the product of the flooring division $\lfloor a / b \rfloor$ and $b$ equals $a$, i.e., \[ \lfloor a / b \rfloor \cdot b = a. \]
Int.mul_fdiv_cancel' theorem
{a b : Int} (H : a ∣ b) : a * b.fdiv a = b
Full source
protected theorem mul_fdiv_cancel' {a b : Int} (H : a ∣ b) : a * b.fdiv a = b := by
  rw [Int.mul_comm, Int.fdiv_mul_cancel H]
Flooring Division Cancellation: $a \cdot \lfloor b / a \rfloor = b$ when $a \mid b$
Informal description
For any integers $a$ and $b$ such that $a$ divides $b$, the product of $a$ and the flooring division $\lfloor b / a \rfloor$ equals $b$, i.e., \[ a \cdot \lfloor b / a \rfloor = b. \]
Int.neg_fmod_self theorem
(a : Int) : (-a).fmod a = 0
Full source
@[simp] theorem neg_fmod_self (a : Int) : (-a).fmod a = 0 := by
  rw [← dvd_iff_fmod_eq_zero, Int.dvd_neg]
  exact Int.dvd_refl a
Flooring Modulus of Negated Integer: $(-a) \operatorname{fmod} a = 0$
Informal description
For any integer $a$, the flooring modulus of $-a$ with respect to $a$ is zero, i.e., $(-a) \operatorname{fmod} a = 0$.
Int.lt_fdiv_add_one_mul_self theorem
(a : Int) {b : Int} (H : 0 < b) : a < (a.fdiv b + 1) * b
Full source
theorem lt_fdiv_add_one_mul_self (a : Int) {b : Int} (H : 0 < b) : a < (a.fdiv b + 1) * b := by
  rw [Int.add_mul, Int.one_mul, Int.mul_comm]
  exact Int.lt_add_of_sub_left_lt <| Int.fmod_def .. ▸ fmod_lt_of_pos _ H
Upper Bound for Integer Division: $a < (\lfloor a/b \rfloor + 1) \cdot b$ for $b > 0$
Informal description
For any integer $a$ and any positive integer $b > 0$, the following inequality holds: $$a < \left(\left\lfloor \frac{a}{b} \right\rfloor + 1\right) \cdot b$$ where $\lfloor \cdot \rfloor$ denotes the floor function.
Int.fdiv_eq_iff_eq_mul_right theorem
{a b c : Int} (H : b ≠ 0) (H' : b ∣ a) : a.fdiv b = c ↔ a = b * c
Full source
protected theorem fdiv_eq_iff_eq_mul_right {a b c : Int}
    (H : b ≠ 0) (H' : b ∣ a) : a.fdiv b = c ↔ a = b * c :=
  ⟨Int.eq_mul_of_fdiv_eq_right H', Int.fdiv_eq_of_eq_mul_right H⟩
Floor Division Equivalence: $\lfloor a/b \rfloor = c \leftrightarrow a = b \cdot c$ for $b \neq 0$ and $b \mid a$
Informal description
For any integers $a$, $b$, and $c$ with $b \neq 0$ and $b$ dividing $a$, the flooring division $\lfloor a / b \rfloor$ equals $c$ if and only if $a = b \cdot c$.
Int.fdiv_eq_iff_eq_mul_left theorem
{a b c : Int} (H : b ≠ 0) (H' : b ∣ a) : a.fdiv b = c ↔ a = c * b
Full source
protected theorem fdiv_eq_iff_eq_mul_left {a b c : Int}
    (H : b ≠ 0) (H' : b ∣ a) : a.fdiv b = c ↔ a = c * b := by
  rw [Int.mul_comm]; exact Int.fdiv_eq_iff_eq_mul_right H H'
Floor Division Equivalence: $\lfloor a/b \rfloor = c \leftrightarrow a = c \cdot b$ for $b \neq 0$ and $b \mid a$
Informal description
For any integers $a$, $b$, and $c$ with $b \neq 0$ and $b$ dividing $a$, the flooring division $\lfloor a / b \rfloor$ equals $c$ if and only if $a = c \cdot b$.
Int.fdiv_left_inj theorem
{a b d : Int} (hda : d ∣ a) (hdb : d ∣ b) : a.fdiv d = b.fdiv d ↔ a = b
Full source
@[simp] protected theorem fdiv_left_inj {a b d : Int}
    (hda : d ∣ a) (hdb : d ∣ b) : a.fdiv d = b.fdiv d ↔ a = b := by
  refine ⟨fun h => ?_, congrArg (fdiv · d)⟩
  rw [← Int.mul_fdiv_cancel' hda, ← Int.mul_fdiv_cancel' hdb, h]
Injectivity of Floor Division under Divisibility: $\lfloor a/d \rfloor = \lfloor b/d \rfloor \iff a = b$ when $d \mid a$ and $d \mid b$
Informal description
For any integers $a$, $b$, and $d$ such that $d$ divides both $a$ and $b$, the flooring division of $a$ by $d$ equals the flooring division of $b$ by $d$ if and only if $a = b$. That is, \[ \lfloor a / d \rfloor = \lfloor b / d \rfloor \iff a = b. \]
Int.mul_fdiv_assoc theorem
(a : Int) : ∀ {b c : Int}, c ∣ b → (a * b).fdiv c = a * (b.fdiv c)
Full source
protected theorem mul_fdiv_assoc (a : Int) : ∀ {b c : Int}, c ∣ b → (a * b).fdiv c = a * (b.fdiv c)
  | _, c, ⟨d, rfl⟩ =>
    if cz : c = 0 then by simp [cz, Int.mul_zero] else by
      rw [Int.mul_left_comm, Int.mul_fdiv_cancel_left _ cz, Int.mul_fdiv_cancel_left _ cz]
Associativity of Multiplication with Floor Division: $\lfloor (a \cdot b)/c \rfloor = a \cdot \lfloor b/c \rfloor$ when $c \mid b$
Informal description
For any integer $a$ and integers $b, c$ with $c$ dividing $b$, the flooring division of $a \times b$ by $c$ equals $a$ multiplied by the flooring division of $b$ by $c$, i.e., $\lfloor (a \cdot b)/c \rfloor = a \cdot \lfloor b/c \rfloor$.
Int.mul_fdiv_assoc' theorem
(b : Int) {a c : Int} (h : c ∣ a) : (a * b).fdiv c = a.fdiv c * b
Full source
protected theorem mul_fdiv_assoc' (b : Int) {a c : Int} (h : c ∣ a) :
    (a * b).fdiv c = a.fdiv c * b := by
  rw [Int.mul_comm, Int.mul_fdiv_assoc _ h, Int.mul_comm]
Associativity of Floor Division with Multiplication: $\lfloor (a \cdot b)/c \rfloor = \lfloor a/c \rfloor \cdot b$ when $c \mid a$
Informal description
For any integer $b$ and integers $a, c$ with $c$ dividing $a$, the flooring division of $a \times b$ by $c$ equals the flooring division of $a$ by $c$ multiplied by $b$, i.e., $\lfloor (a \cdot b)/c \rfloor = \lfloor a/c \rfloor \cdot b$.
Int.neg_fdiv_of_dvd theorem
: ∀ {a b : Int}, b ∣ a → (-a).fdiv b = -(a.fdiv b)
Full source
theorem neg_fdiv_of_dvd : ∀ {a b : Int}, b ∣ a → (-a).fdiv b = -(a.fdiv b)
  | _, b, ⟨c, rfl⟩ => by
    by_cases bz : b = 0
    · simp [bz]
    · rw [Int.neg_mul_eq_mul_neg, Int.mul_fdiv_cancel_left _ bz, Int.mul_fdiv_cancel_left _ bz]
Negation and Floor Division: $\lfloor (-a)/b \rfloor = -\lfloor a/b \rfloor$ when $b \mid a$
Informal description
For any integers $a$ and $b$ such that $b$ divides $a$, the flooring division of $-a$ by $b$ equals the negation of the flooring division of $a$ by $b$, i.e., $\lfloor (-a)/b \rfloor = -\lfloor a/b \rfloor$.
Int.sub_fdiv_of_dvd theorem
(a : Int) {b c : Int} (hcb : c ∣ b) : (a - b).fdiv c = a.fdiv c - b.fdiv c
Full source
theorem sub_fdiv_of_dvd (a : Int) {b c : Int}
    (hcb : c ∣ b) : (a - b).fdiv c = a.fdiv c - b.fdiv c := by
  rw [Int.sub_eq_add_neg, Int.sub_eq_add_neg, Int.add_fdiv_of_dvd_right (Int.dvd_neg.2 hcb)]
  congr; exact Int.neg_fdiv_of_dvd hcb
Subtraction and Floor Division: $\lfloor (a - b)/c \rfloor = \lfloor a/c \rfloor - \lfloor b/c \rfloor$ when $c \mid b$
Informal description
For any integers $a$, $b$, and $c$ such that $c$ divides $b$, the flooring division of $a - b$ by $c$ equals the difference of the flooring divisions of $a$ by $c$ and $b$ by $c$, i.e., \[ \left\lfloor \frac{a - b}{c} \right\rfloor = \left\lfloor \frac{a}{c} \right\rfloor - \left\lfloor \frac{b}{c} \right\rfloor. \]
Int.fdiv_dvd_fdiv theorem
: ∀ {a b c : Int}, a ∣ b → b ∣ c → b.fdiv a ∣ c.fdiv a
Full source
theorem fdiv_dvd_fdiv : ∀ {a b c : Int}, a ∣ bb ∣ cb.fdiv a ∣ c.fdiv a
  | a, _, _, ⟨b, rfl⟩, ⟨c, rfl⟩ => by
    by_cases az : a = 0
    · simp [az]
    · rw [Int.mul_fdiv_cancel_left _ az, Int.mul_assoc, Int.mul_fdiv_cancel_left _ az]
      apply Int.dvd_mul_right
Divisibility of Floor Division Results: $\lfloor b/a \rfloor \mid \lfloor c/a \rfloor$ when $a \mid b$ and $b \mid c$
Informal description
For any integers $a$, $b$, and $c$, if $a$ divides $b$ and $b$ divides $c$, then the flooring division of $b$ by $a$ divides the flooring division of $c$ by $a$. In other words, if $a \mid b$ and $b \mid c$, then $\lfloor b / a \rfloor \mid \lfloor c / a \rfloor$.
Int.mul_fdiv_cancel_of_dvd theorem
{a b : Int} (H : b ∣ a) : b * (a.fdiv b) = a
Full source
theorem mul_fdiv_cancel_of_dvd {a b : Int} (H : b ∣ a) : b * (a.fdiv b) = a :=
  mul_fdiv_cancel_of_fmod_eq_zero (fmod_eq_zero_of_dvd H)
Floor Division Cancellation for Divisible Integers: $b \cdot \lfloor a/b \rfloor = a$ when $b \mid a$
Informal description
For any integers $a$ and $b$ such that $b$ divides $a$, the product of $b$ and the flooring division of $a$ by $b$ equals $a$, i.e., \[ b \cdot \left\lfloor \frac{a}{b} \right\rfloor = a. \]
Int.fdiv_mul_cancel_of_dvd theorem
{a b : Int} (H : b ∣ a) : a.fdiv b * b = a
Full source
theorem fdiv_mul_cancel_of_dvd {a b : Int} (H : b ∣ a) : a.fdiv b * b = a :=
  fdiv_mul_cancel_of_fmod_eq_zero (fmod_eq_zero_of_dvd H)
Floor Division Cancellation: $\lfloor a/b \rfloor \cdot b = a$ when $b \mid a$
Informal description
For any integers $a$ and $b$ such that $b$ divides $a$, the product of the flooring division $\lfloor a / b \rfloor$ and $b$ equals $a$, i.e., \[ \left\lfloor \frac{a}{b} \right\rfloor \cdot b = a. \]
Int.fmod_two_eq theorem
(x : Int) : x.fmod 2 = 0 ∨ x.fmod 2 = 1
Full source
theorem fmod_two_eq (x : Int) : x.fmod 2 = 0 ∨ x.fmod 2 = 1 := by
  have h₁ : 0 ≤ x.fmod 2 := Int.fmod_nonneg_of_pos _ (by decide)
  have h₂ : x.fmod 2 < 2 := Int.fmod_lt_of_pos x (by decide)
  match x.fmod 2, h₁, h₂ with
  | 0, _, _ => simp
  | 1, _, _ => simp
Possible Remainders Modulo 2: $x \operatorname{fmod} 2 \in \{0, 1\}$
Informal description
For any integer $x$, the flooring modulus of $x$ modulo 2 is either 0 or 1, i.e., $x \operatorname{fmod} 2 \in \{0, 1\}$.
Int.add_fmod_eq_add_fmod_left theorem
{m n k : Int} (i : Int) (H : m.fmod n = k.fmod n) : (i + m).fmod n = (i + k).fmod n
Full source
theorem add_fmod_eq_add_fmod_left {m n k : Int} (i : Int)
    (H : m.fmod n = k.fmod n) : (i + m).fmod n = (i + k).fmod n := by
  rw [Int.add_comm, add_fmod_eq_add_fmod_right _ H, Int.add_comm]
Left Addition Invariance of Flooring Modulus: $\operatorname{fmod}(i + m, n) = \operatorname{fmod}(i + k, n)$ when $\operatorname{fmod}(m, n) = \operatorname{fmod}(k, n)$
Informal description
For any integers $m$, $n$, $k$, and $i$, if the flooring moduli of $m$ and $k$ with respect to $n$ are equal (i.e., $\operatorname{fmod}(m, n) = \operatorname{fmod}(k, n)$), then the flooring moduli of $i + m$ and $i + k$ with respect to $n$ are also equal, i.e., \[ \operatorname{fmod}(i + m, n) = \operatorname{fmod}(i + k, n). \]
Int.fmod_add_cancel_left theorem
{m n k i : Int} : (i + m).fmod n = (i + k).fmod n ↔ m.fmod n = k.fmod n
Full source
theorem fmod_add_cancel_left {m n k i : Int} : (i + m).fmod n = (i + k).fmod n ↔ m.fmod n = k.fmod n := by
  rw [Int.add_comm, Int.add_comm i, fmod_add_cancel_right]
Left Addition Invariance of Flooring Modulus: $(i + m) \operatorname{fmod} n = (i + k) \operatorname{fmod} n \leftrightarrow m \operatorname{fmod} n = k \operatorname{fmod} n$
Informal description
For any integers $m$, $n$, $k$, and $i$, the flooring moduli of $i + m$ and $i + k$ with respect to $n$ are equal if and only if the flooring moduli of $m$ and $k$ with respect to $n$ are equal, i.e., \[ (i + m) \operatorname{fmod} n = (i + k) \operatorname{fmod} n \leftrightarrow m \operatorname{fmod} n = k \operatorname{fmod} n. \]
Int.fmod_sub_cancel_right theorem
{m n k : Int} (i) : (m - i).fmod n = (k - i).fmod n ↔ m.fmod n = k.fmod n
Full source
theorem fmod_sub_cancel_right {m n k : Int} (i) : (m - i).fmod n = (k - i).fmod n ↔ m.fmod n = k.fmod n :=
  fmod_add_cancel_right _
Equivalence of Flooring Moduli under Subtraction: $\operatorname{fmod}(m - i, n) = \operatorname{fmod}(k - i, n) \leftrightarrow \operatorname{fmod}(m, n) = \operatorname{fmod}(k, n)$
Informal description
For any integers $m$, $n$, $k$, and $i$, the flooring moduli of $m - i$ and $k - i$ with respect to $n$ are equal if and only if the flooring moduli of $m$ and $k$ with respect to $n$ are equal, i.e., \[ \operatorname{fmod}(m - i, n) = \operatorname{fmod}(k - i, n) \leftrightarrow \operatorname{fmod}(m, n) = \operatorname{fmod}(k, n). \]
Int.fmod_eq_fmod_iff_fmod_sub_eq_zero theorem
{m n k : Int} : m.fmod n = k.fmod n ↔ (m - k).fmod n = 0
Full source
theorem fmod_eq_fmod_iff_fmod_sub_eq_zero {m n k : Int} : m.fmod n = k.fmod n ↔ (m - k).fmod n = 0 :=
  (fmod_sub_cancel_right k).symm.trans <| by simp [Int.sub_self]
Equivalence of Flooring Moduli via Difference: $\operatorname{fmod}(m, n) = \operatorname{fmod}(k, n) \leftrightarrow \operatorname{fmod}(m - k, n) = 0$
Informal description
For any integers $m$, $n$, and $k$, the flooring moduli of $m$ and $k$ with respect to $n$ are equal if and only if the flooring modulus of their difference $m - k$ with respect to $n$ is zero, i.e., \[ \operatorname{fmod}(m, n) = \operatorname{fmod}(k, n) \leftrightarrow \operatorname{fmod}(m - k, n) = 0. \]
Int.fdiv_fmod_unique theorem
{a b r q : Int} (h : 0 < b) : a.fdiv b = q ∧ a.fmod b = r ↔ r + b * q = a ∧ 0 ≤ r ∧ r < b
Full source
protected theorem fdiv_fmod_unique {a b r q : Int} (h : 0 < b) :
    a.fdiv b = q ∧ a.fmod b = ra.fdiv b = q ∧ a.fmod b = r ↔ r + b * q = a ∧ 0 ≤ r ∧ r < b := by
  rw [fdiv_eq_ediv_of_nonneg, fmod_eq_emod_of_nonneg, Int.ediv_emod_unique]
  all_goals omega
Characterization of Floor Division and Remainder for Positive Divisors: $\lfloor a/b \rfloor = q \land a \operatorname{fmod} b = r \leftrightarrow r + bq = a \land 0 \leq r < b$
Informal description
For any integers $a$, $b$, $r$, and $q$ with $b > 0$, the following are equivalent: 1. The flooring division of $a$ by $b$ yields quotient $q$ and remainder $r$, i.e., $\lfloor a / b \rfloor = q$ and $a \operatorname{fmod} b = r$. 2. The decomposition $r + b \cdot q = a$ holds, with $0 \leq r < b$.
Int.fdiv_fmod_unique' theorem
{a b r q : Int} (h : b < 0) : a.fdiv b = q ∧ a.fmod b = r ↔ r + b * q = a ∧ b < r ∧ r ≤ 0
Full source
protected theorem fdiv_fmod_unique' {a b r q : Int} (h : b < 0) :
    a.fdiv b = q ∧ a.fmod b = ra.fdiv b = q ∧ a.fmod b = r ↔ r + b * q = a ∧ b < r ∧ r ≤ 0 := by
  have := Int.fdiv_fmod_unique (a := -a) (b := -b) (r := -r) (q := q) (by omega)
  simp at this
  simp [this, Int.neg_mul]
  omega
Characterization of Floor Division and Remainder for Negative Divisors: $\lfloor a/b \rfloor = q \land a \operatorname{fmod} b = r \leftrightarrow r + bq = a \land b < r \leq 0$
Informal description
For any integers $a$, $b$, $r$, and $q$ with $b < 0$, the following are equivalent: 1. The flooring division of $a$ by $b$ yields quotient $q$ and remainder $r$, i.e., $\lfloor a / b \rfloor = q$ and $a \operatorname{fmod} b = r$. 2. The decomposition $r + b \cdot q = a$ holds, with $b < r \leq 0$.
Int.mul_fmod_mul_of_pos theorem
{a : Int} (b c : Int) (H : 0 < a) : (a * b).fmod (a * c) = a * (b.fmod c)
Full source
@[simp] theorem mul_fmod_mul_of_pos
    {a : Int} (b c : Int) (H : 0 < a) : (a * b).fmod (a * c) = a * (b.fmod c) := by
  rw [fmod_def, fmod_def, mul_fdiv_mul_of_pos _ _ H, Int.mul_sub, Int.mul_assoc]
Scaling Property of Flooring Modulus: $(a \cdot b) \operatorname{fmod} (a \cdot c) = a \cdot (b \operatorname{fmod} c)$ for $a > 0$
Informal description
For any integers $b$ and $c$, and any positive integer $a > 0$, the flooring modulus satisfies: \[ (a \cdot b) \operatorname{fmod} (a \cdot c) = a \cdot (b \operatorname{fmod} c) \]
Int.natAbs_fdiv_le_natAbs theorem
(a b : Int) : natAbs (a.fdiv b) ≤ natAbs a
Full source
theorem natAbs_fdiv_le_natAbs (a b : Int) : natAbs (a.fdiv b) ≤ natAbs a := by
  rw [fdiv_eq_ediv]
  split
  · simp [natAbs_ediv_le_natAbs]
  · rename_i h
    simp at h
    match a, b, h with
    | 0, .negSucc b, h => simp at h
    | .ofNat (a + 1), .negSucc 0, h => simp at h
    | .ofNat (a + 1), .negSucc (b + 1), h =>
      rw [negSucc_eq, ofNat_eq_coe]
      norm_cast
      rw [Int.ediv_neg, Int.sub_eq_add_neg, ← Int.neg_add, natAbs_neg]
      norm_cast
      apply Nat.div_lt_self
      omega
      omega
    | .negSucc a, .negSucc b, h =>
      simp [negSucc_eq]
      norm_cast
      rw [Int.neg_ediv, if_neg (by simpa using h.2)]
      norm_cast
      rw [sign_eq_one_of_pos (by omega), Int.sub_eq_add_neg, ← Int.neg_add, natAbs_neg,
        Int.sub_add_cancel, natAbs_neg, natAbs_ofNat]
      apply Nat.div_le_self
Absolute Value Bound for Flooring Division: $|\lfloor a / b \rfloor| \leq |a|$
Informal description
For any integers $a$ and $b$, the absolute value of the flooring division $\lfloor a / b \rfloor$ is less than or equal to the absolute value of $a$, i.e., $|\lfloor a / b \rfloor| \leq |a|$.
Int.fdiv_le_self theorem
{a : Int} (b : Int) (Ha : 0 ≤ a) : a.fdiv b ≤ a
Full source
theorem fdiv_le_self {a : Int} (b : Int) (Ha : 0 ≤ a) : a.fdiv b ≤ a := by
  have := Int.le_trans le_natAbs (ofNat_le.2 <| natAbs_fdiv_le_natAbs a b)
  rwa [natAbs_of_nonneg Ha] at this
Flooring Division Bound: $\lfloor a / b \rfloor \leq a$ for $a \geq 0$
Informal description
For any nonnegative integer $a$ (i.e., $0 \leq a$) and any integer $b$, the flooring division of $a$ by $b$ satisfies $\lfloor a / b \rfloor \leq a$.
Int.dvd_fmod_sub_self theorem
{x : Int} {m : Nat} : (m : Int) ∣ x.fmod m - x
Full source
theorem dvd_fmod_sub_self {x : Int} {m : Nat} : (m : Int) ∣ x.fmod m - x := by
  rw [fmod_eq_emod]
  have := dvd_emod_sub_self (x := x) (m := m)
  split
  · simpa
  · have w : x % ↑m + ↑m - x = x % ↑m - x + ↑m := by omega
    rw [w]
    apply Int.dvd_add this (Int.dvd_refl ↑m)
Divisibility of Flooring Modulus Difference: $m \mid (\operatorname{fmod}(x, m) - x)$
Informal description
For any integer $x$ and natural number $m$, the integer $m$ divides the difference between the flooring modulus of $x$ modulo $m$ and $x$ itself, i.e., $m \mid (\operatorname{fmod}(x, m) - x)$.
Int.neg_mul_fmod_right theorem
(a b : Int) : (-(a * b)).fmod a = 0
Full source
@[simp] theorem neg_mul_fmod_right (a b : Int) : (-(a * b)).fmod a = 0 := by
  rw [← dvd_iff_fmod_eq_zero, Int.dvd_neg]
  exact Int.dvd_mul_right a b
Flooring Modulus of Negative Product: $(-(a \cdot b)) \operatorname{fmod} a = 0$
Informal description
For any integers $a$ and $b$, the flooring modulus of $-(a \cdot b)$ with respect to $a$ is equal to $0$, i.e., $(-(a \cdot b)) \operatorname{fmod} a = 0$.
Int.neg_mul_fmod_left theorem
(a b : Int) : (-(a * b)).fmod b = 0
Full source
@[simp] theorem neg_mul_fmod_left (a b : Int) : (-(a * b)).fmod b = 0 := by
  rw [← dvd_iff_fmod_eq_zero, Int.dvd_neg]
  exact Int.dvd_mul_left a b
Flooring Modulus of Negative Product: $(-(a \cdot b)) \operatorname{fmod} b = 0$
Informal description
For any integers $a$ and $b$, the flooring modulus of $-(a \times b)$ with respect to $b$ is zero, i.e., $(-(a \cdot b)) \operatorname{fmod} b = 0$.
Int.fmod_one theorem
(a : Int) : a.fmod 1 = 0
Full source
@[simp] theorem fmod_one (a : Int) : a.fmod 1 = 0 := by
  simp [fmod_def, Int.one_mul, Int.sub_self]
Flooring Modulus Identity: $a \operatorname{fmod} 1 = 0$
Informal description
For any integer $a$, the flooring modulus of $a$ with respect to $1$ is equal to $0$, i.e., $a \operatorname{fmod} 1 = 0$.
Int.fmod_sub_cancel theorem
(x y : Int) : (x - y).fmod y = x.fmod y
Full source
@[simp]
theorem fmod_sub_cancel (x y : Int) : (x - y).fmod y = x.fmod y := by
  by_cases h : y = 0
  · simp [h]
  · simp only [Int.fmod_def, Int.sub_fdiv_of_dvd, Int.dvd_refl, Int.fdiv_self h, Int.mul_sub]
    simp [Int.mul_one, Int.sub_sub, Int.add_comm y]
Flooring Modulus Cancellation: $(x - y) \operatorname{fmod} y = x \operatorname{fmod} y$
Informal description
For any integers $x$ and $y$, the flooring modulus of $x - y$ with respect to $y$ equals the flooring modulus of $x$ with respect to $y$, i.e., \[ (x - y) \operatorname{fmod} y = x \operatorname{fmod} y. \]
Int.add_neg_fmod_self theorem
(a b : Int) : (a + -b).fmod b = a.fmod b
Full source
@[simp] theorem add_neg_fmod_self (a b : Int) : (a + -b).fmod b = a.fmod b := by
  rw [Int.add_neg_eq_sub, fmod_sub_cancel]
Flooring Modulus Invariance Under Subtraction: $(a - b) \operatorname{fmod} b = a \operatorname{fmod} b$
Informal description
For any integers $a$ and $b$, the flooring modulus of $a - b$ with respect to $b$ equals the flooring modulus of $a$ with respect to $b$, i.e., \[ (a - b) \operatorname{fmod} b = a \operatorname{fmod} b. \]
Int.neg_add_fmod_self theorem
(a b : Int) : (-a + b).fmod a = b.fmod a
Full source
@[simp] theorem neg_add_fmod_self (a b : Int) : (-a + b).fmod a = b.fmod a := by
  rw [Int.add_comm, add_neg_fmod_self]
Flooring Modulus Invariance Under Negation and Addition: $(-a + b) \operatorname{fmod} a = b \operatorname{fmod} a$
Informal description
For any integers $a$ and $b$, the flooring modulus of $-a + b$ with respect to $a$ equals the flooring modulus of $b$ with respect to $a$, i.e., \[ (-a + b) \operatorname{fmod} a = b \operatorname{fmod} a. \]
Int.dvd_sub_of_fmod_eq theorem
{a b c : Int} (h : a.fmod b = c) : b ∣ a - c
Full source
/-- If `a.fmod b = c` then `b` divides `a - c`. -/
theorem dvd_sub_of_fmod_eq {a b c : Int} (h : a.fmod b = c) : b ∣ a - c := by
  have hx : (a.fmod b).fmod b = c.fmod b := by
    rw [h]
  rw [Int.fmod_fmod, ← fmod_sub_cancel_right c, Int.sub_self, zero_fmod] at hx
  exact dvd_of_fmod_eq_zero hx
Divisibility from Flooring Modulus: $\operatorname{fmod}(a, b) = c \implies b \mid (a - c)$
Informal description
For any integers $a$, $b$, and $c$, if the flooring modulus of $a$ with respect to $b$ equals $c$ (i.e., $\operatorname{fmod}(a, b) = c$), then $b$ divides the difference $a - c$ (i.e., $b \mid (a - c)$).
Int.fdiv_sign theorem
{a b : Int} : a.fdiv (sign b) = a * sign b
Full source
theorem fdiv_sign {a b : Int} : a.fdiv (sign b) = a * sign b := by
  rw [fdiv_eq_ediv]
  rcases sign_trichotomy b with h | h | h <;> simp [h]
Floor Division by Sign Equals Multiplication by Sign: $\lfloor a / \text{sign}(b) \rfloor = a \cdot \text{sign}(b)$
Informal description
For any integers $a$ and $b$, the flooring division of $a$ by the sign of $b$ equals $a$ multiplied by the sign of $b$, i.e., $\lfloor a / \text{sign}(b) \rfloor = a \cdot \text{sign}(b)$.
Int.sign_eq_fdiv_abs theorem
(a : Int) : sign a = a.fdiv (natAbs a)
Full source
protected theorem sign_eq_fdiv_abs (a : Int) : sign a = a.fdiv (natAbs a) :=
  if az : a = 0 then by simp [az] else
    (Int.fdiv_eq_of_eq_mul_left (ofNat_ne_zero.2 <| natAbs_ne_zero.2 az)
      (sign_mul_natAbs _).symm).symm
Sign as Floor Division by Absolute Value: $\text{sign}(a) = \lfloor a / |a| \rfloor$
Informal description
For any integer $a$, the sign of $a$ equals the flooring division of $a$ by its natural absolute value, i.e., \[ \text{sign}(a) = \left\lfloor \frac{a}{|a|} \right\rfloor. \]
Int.mul_fdiv_self_le theorem
{x k : Int} (h : 0 < k) : k * (x.fdiv k) ≤ x
Full source
theorem mul_fdiv_self_le {x k : Int} (h : 0 < k) : k * (x.fdiv k) ≤ x := by
  rw [fdiv_eq_ediv]
  have := mul_ediv_self_le (x := x) (k := k)
  split <;> simp <;> omega
Floor Division Lower Bound: $k \cdot \lfloor x / k \rfloor \leq x$ for $k > 0$
Informal description
For any integers $x$ and $k$ with $k > 0$, the product of $k$ and the flooring division of $x$ by $k$ is less than or equal to $x$, i.e., \[ k \cdot \lfloor x / k \rfloor \leq x. \]
Int.lt_mul_fdiv_self_add theorem
{x k : Int} (h : 0 < k) : x < k * (x.fdiv k) + k
Full source
theorem lt_mul_fdiv_self_add {x k : Int} (h : 0 < k) : x < k * (x.fdiv k) + k := by
  rw [fdiv_eq_ediv]
  have := lt_mul_ediv_self_add (x := x) h
  split <;> simp <;> omega
Lower bound for flooring division: $x < k \lfloor x/k \rfloor + k$ when $k > 0$
Informal description
For any integers $x$ and $k$ with $k > 0$, the following inequality holds: \[ x < k \cdot \left\lfloor \frac{x}{k} \right\rfloor + k. \]
Int.emod_bmod_congr theorem
(x : Int) (n : Nat) : Int.bmod (x % n) n = Int.bmod x n
Full source
@[simp]
theorem emod_bmod_congr (x : Int) (n : Nat) : Int.bmod (x%n) n = Int.bmod x n := by
  simp [bmod, Int.emod_emod]
Congruence of Balanced Modulus and Euclidean Remainder: $\text{bmod}(x \% n, n) = \text{bmod}(x, n)$
Informal description
For any integer $x$ and natural number $n$, the balanced modulus of the Euclidean remainder of $x$ modulo $n$ equals the balanced modulus of $x$ itself, i.e., \[ \text{bmod}(x \% n, n) = \text{bmod}(x, n). \]
Int.bdiv_add_bmod theorem
(x : Int) (m : Nat) : m * bdiv x m + bmod x m = x
Full source
theorem bdiv_add_bmod (x : Int) (m : Nat) : m * bdiv x m + bmod x m = x := by
  unfold bdiv bmod
  split
  · simp_all only [cast_ofNat_Int, Int.mul_zero, emod_zero, Int.zero_add, Int.sub_zero,
      ite_self]
  · dsimp only
    split
    · exact ediv_add_emod x m
    · rw [Int.mul_add, Int.mul_one, Int.add_assoc, Int.add_comm m, Int.sub_add_cancel]
      exact ediv_add_emod x m
Balanced Division-Modulus Decomposition: $m \cdot \text{bdiv}(x, m) + \text{bmod}(x, m) = x$
Informal description
For any integer $x$ and natural number $m$, the sum of $m$ multiplied by the balanced division of $x$ by $m$ and the balanced modulus of $x$ by $m$ equals $x$, i.e., \[ m \cdot \text{bdiv}(x, m) + \text{bmod}(x, m) = x. \]
Int.bmod_add_bdiv theorem
(x : Int) (m : Nat) : bmod x m + m * bdiv x m = x
Full source
theorem bmod_add_bdiv (x : Int) (m : Nat) : bmod x m + m * bdiv x m = x := by
  rw [Int.add_comm]; exact bdiv_add_bmod x m
Balanced Division-Modulus Decomposition (Additive Form)
Informal description
For any integer $x$ and natural number $m$, the sum of the balanced modulus $\text{bmod}(x, m)$ and $m$ times the balanced division $\text{bdiv}(x, m)$ equals $x$, i.e., \[ \text{bmod}(x, m) + m \cdot \text{bdiv}(x, m) = x. \]
Int.bdiv_add_bmod' theorem
(x : Int) (m : Nat) : bdiv x m * m + bmod x m = x
Full source
theorem bdiv_add_bmod' (x : Int) (m : Nat) : bdiv x m * m + bmod x m = x := by
  rw [Int.mul_comm]; exact bdiv_add_bmod x m
Balanced Division-Modulus Decomposition (Multiplicative Form)
Informal description
For any integer $x$ and natural number $m$, the sum of the balanced division of $x$ by $m$ multiplied by $m$ and the balanced modulus of $x$ by $m$ equals $x$, i.e., \[ \text{bdiv}(x, m) \cdot m + \text{bmod}(x, m) = x. \]
Int.bmod_add_bdiv' theorem
(x : Int) (m : Nat) : bmod x m + bdiv x m * m = x
Full source
theorem bmod_add_bdiv' (x : Int) (m : Nat) : bmod x m + bdiv x m * m = x := by
  rw [Int.add_comm]; exact bdiv_add_bmod' x m
Balanced Division-Modulus Decomposition (Additive Form with Reversed Multiplication)
Informal description
For any integer $x$ and natural number $m$, the sum of the balanced modulus $\text{bmod}(x, m)$ and the product of the balanced division $\text{bdiv}(x, m)$ with $m$ equals $x$, i.e., \[ \text{bmod}(x, m) + \text{bdiv}(x, m) \cdot m = x. \]
Int.bmod_eq_self_sub_mul_bdiv theorem
(x : Int) (m : Nat) : bmod x m = x - m * bdiv x m
Full source
theorem bmod_eq_self_sub_mul_bdiv (x : Int) (m : Nat) : bmod x m = x - m * bdiv x m := by
  rw [← Int.add_sub_cancel (bmod x m), bmod_add_bdiv]
Balanced Modulus as Difference: $\text{bmod}(x, m) = x - m \cdot \text{bdiv}(x, m)$
Informal description
For any integer $x$ and natural number $m$, the balanced modulus $\text{bmod}(x, m)$ equals $x$ minus $m$ times the balanced division $\text{bdiv}(x, m)$, i.e., \[ \text{bmod}(x, m) = x - m \cdot \text{bdiv}(x, m). \]
Int.bmod_eq_self_sub_bdiv_mul theorem
(x : Int) (m : Nat) : bmod x m = x - bdiv x m * m
Full source
theorem bmod_eq_self_sub_bdiv_mul (x : Int) (m : Nat) : bmod x m = x - bdiv x m * m := by
  rw [← Int.add_sub_cancel (bmod x m), bmod_add_bdiv']
Balanced Modulus Decomposition: $\text{bmod}(x, m) = x - \text{bdiv}(x, m) \cdot m$
Informal description
For any integer $x$ and natural number $m$, the balanced modulus $\text{bmod}(x, m)$ satisfies the equation: \[ \text{bmod}(x, m) = x - \text{bdiv}(x, m) \cdot m \] where $\text{bdiv}(x, m)$ is the balanced division of $x$ by $m$.
Int.bmod_pos theorem
(x : Int) (m : Nat) (p : x % m < (m + 1) / 2) : bmod x m = x % m
Full source
theorem bmod_pos (x : Int) (m : Nat) (p : x % m < (m + 1) / 2) : bmod x m = x % m := by
  simp [bmod_def, p]
Balanced Modulus Equals Remainder When Remainder is Small
Informal description
For any integer $x$ and natural number $m$, if the remainder of $x$ modulo $m$ (denoted $x \% m$) is less than $\frac{m + 1}{2}$, then the balanced modulus of $x$ with respect to $m$ (denoted $\text{bmod}(x, m)$) equals $x \% m$.
Int.bmod_neg theorem
(x : Int) (m : Nat) (p : x % m ≥ (m + 1) / 2) : bmod x m = (x % m) - m
Full source
theorem bmod_neg (x : Int) (m : Nat) (p : x % m ≥ (m + 1) / 2) : bmod x m = (x % m) - m := by
  simp [bmod_def, Int.not_lt.mpr p]
Balanced Modulus Adjustment for Large Remainders: $\text{bmod}(x, m) = (x \% m) - m$ when $x \% m \geq \frac{m + 1}{2}$
Informal description
For any integer $x$ and natural number $m$, if the remainder $x \% m$ satisfies $x \% m \geq \frac{m + 1}{2}$, then the balanced modulus $\text{bmod}(x, m)$ equals $(x \% m) - m$.
Int.bmod_one_is_zero theorem
(x : Int) : Int.bmod x 1 = 0
Full source
@[simp]
theorem bmod_one_is_zero (x : Int) : Int.bmod x 1 = 0 := by
  simp [Int.bmod]
Balanced Modulus Identity: $\text{bmod}(x, 1) = 0$
Informal description
For any integer $x$, the balanced modulus of $x$ with respect to $1$ is zero, i.e., $\text{bmod}(x, 1) = 0$.
Int.bmod_add_cancel theorem
(x : Int) (n : Nat) : Int.bmod (x + n) n = Int.bmod x n
Full source
@[simp]
theorem bmod_add_cancel (x : Int) (n : Nat) : Int.bmod (x + n) n = Int.bmod x n := by
  simp [bmod_def]
Balanced Modulus Invariance under Addition: $\text{bmod}(x + n, n) = \text{bmod}(x, n)$
Informal description
For any integer $x$ and natural number $n$, the balanced modulus of $x + n$ with respect to $n$ equals the balanced modulus of $x$ with respect to $n$, i.e., $\text{bmod}(x + n, n) = \text{bmod}(x, n)$.
Int.bmod_add_mul_cancel theorem
(x : Int) (n : Nat) (k : Int) : Int.bmod (x + n * k) n = Int.bmod x n
Full source
@[simp]
theorem bmod_add_mul_cancel (x : Int) (n : Nat) (k : Int) : Int.bmod (x + n * k) n = Int.bmod x n := by
  simp [bmod_def]
Balanced Modulus Invariance under Addition of Multiple: $\text{bmod}(x + n \cdot k, n) = \text{bmod}(x, n)$
Informal description
For any integer $x$, natural number $n$, and integer $k$, the balanced modulus of $x + n \cdot k$ with respect to $n$ equals the balanced modulus of $x$ with respect to $n$, i.e., $\text{bmod}(x + n \cdot k, n) = \text{bmod}(x, n)$.
Int.bmod_sub_cancel theorem
(x : Int) (n : Nat) : Int.bmod (x - n) n = Int.bmod x n
Full source
@[simp]
theorem bmod_sub_cancel (x : Int) (n : Nat) : Int.bmod (x - n) n = Int.bmod x n := by
  simp [bmod_def]
Balanced Modulus Invariance under Subtraction: $\text{bmod}(x - n, n) = \text{bmod}(x, n)$
Informal description
For any integer $x$ and natural number $n$, the balanced modulus of $x - n$ with respect to $n$ equals the balanced modulus of $x$ with respect to $n$, i.e., \[ \text{bmod}(x - n, n) = \text{bmod}(x, n). \]
Int.Int.bmod_sub_mul_cancel theorem
(x : Int) (n : Nat) (k : Int) : (x - n * k).bmod n = x.bmod n
Full source
@[simp] theorem Int.bmod_sub_mul_cancel (x : Int) (n : Nat) (k : Int) : (x - n * k).bmod n = x.bmod n := by
  rw [Int.sub_eq_add_neg, Int.neg_mul_eq_mul_neg, Int.bmod_add_mul_cancel]
Balanced Modulus Invariance under Subtraction of Multiple: $\text{bmod}(x - n \cdot k, n) = \text{bmod}(x, n)$
Informal description
For any integer $x$, natural number $n$, and integer $k$, the balanced modulus of $x - n \cdot k$ with respect to $n$ equals the balanced modulus of $x$ with respect to $n$, i.e., $\text{bmod}(x - n \cdot k, n) = \text{bmod}(x, n)$.
Int.emod_add_bmod_congr theorem
(x : Int) (n : Nat) : Int.bmod (x % n + y) n = Int.bmod (x + y) n
Full source
@[simp]
theorem emod_add_bmod_congr (x : Int) (n : Nat) : Int.bmod (x%n + y) n = Int.bmod (x + y) n := by
  simp [Int.emod_def, Int.sub_eq_add_neg]
  rw [←Int.mul_neg, Int.add_right_comm,  Int.bmod_add_mul_cancel]
Balanced Modulus Congruence under Addition: $\text{bmod}((x \bmod n) + y, n) = \text{bmod}(x + y, n)$
Informal description
For any integer $x$, natural number $n$, and integer $y$, the balanced modulus of $(x \bmod n) + y$ with respect to $n$ equals the balanced modulus of $x + y$ with respect to $n$, i.e., \[ \text{bmod}((x \bmod n) + y, n) = \text{bmod}(x + y, n). \]
Int.emod_sub_bmod_congr theorem
(x : Int) (n : Nat) : Int.bmod (x % n - y) n = Int.bmod (x - y) n
Full source
@[simp]
theorem emod_sub_bmod_congr (x : Int) (n : Nat) : Int.bmod (x%n - y) n = Int.bmod (x - y) n := by
  simp only [emod_def, Int.sub_eq_add_neg]
  rw [←Int.mul_neg, Int.add_right_comm,  Int.bmod_add_mul_cancel]
Balanced Modulus Congruence Under Subtraction: $\text{bmod}((x \bmod n) - y, n) = \text{bmod}(x - y, n)$
Informal description
For any integer $x$, natural number $n$, and integer $y$, the balanced modulus of $(x \% n) - y$ with respect to $n$ equals the balanced modulus of $x - y$ with respect to $n$, i.e., $$\text{bmod}((x \bmod n) - y, n) = \text{bmod}(x - y, n)$$ where: - $\bmod$ is the Euclidean modulus operation - $\text{bmod}$ is the balanced modulus function that returns a result in $[-n/2, n/2)$
Int.sub_emod_bmod_congr theorem
(x : Int) (n : Nat) : Int.bmod (x - y % n) n = Int.bmod (x - y) n
Full source
@[simp]
theorem sub_emod_bmod_congr (x : Int) (n : Nat) : Int.bmod (x - y%n) n = Int.bmod (x - y) n := by
  simp only [emod_def]
  rw [Int.sub_eq_add_neg, Int.neg_sub, Int.sub_eq_add_neg, ← Int.add_assoc, Int.add_right_comm,
    Int.bmod_add_mul_cancel, Int.sub_eq_add_neg]
Balanced Modulus Invariance under Subtraction of Remainder: $\text{bmod}(x - (y \bmod n), n) = \text{bmod}(x - y, n)$
Informal description
For any integer $x$ and natural number $n$, the balanced modulus of $x - (y \bmod n)$ with respect to $n$ equals the balanced modulus of $x - y$ with respect to $n$, i.e., \[ \text{bmod}(x - (y \bmod n), n) = \text{bmod}(x - y, n). \]
Int.emod_mul_bmod_congr theorem
(x : Int) (n : Nat) : Int.bmod (x % n * y) n = Int.bmod (x * y) n
Full source
@[simp]
theorem emod_mul_bmod_congr (x : Int) (n : Nat) : Int.bmod (x%n * y) n = Int.bmod (x * y) n := by
  simp [Int.emod_def, Int.sub_eq_add_neg]
  rw [←Int.mul_neg, Int.add_mul, Int.mul_assoc, Int.bmod_add_mul_cancel]
Balanced Modulus Invariance under Multiplication with Remainder: $\text{bmod}((x \% n) \cdot y, n) = \text{bmod}(x \cdot y, n)$
Informal description
For any integer $x$, natural number $n$, and integer $y$, the balanced modulus of $(x \% n) \cdot y$ with respect to $n$ equals the balanced modulus of $x \cdot y$ with respect to $n$, i.e., $\text{bmod}((x \% n) \cdot y, n) = \text{bmod}(x \cdot y, n)$.
Int.bmod_add_bmod_congr theorem
: Int.bmod (Int.bmod x n + y) n = Int.bmod (x + y) n
Full source
@[simp]
theorem bmod_add_bmod_congr : Int.bmod (Int.bmod x n + y) n = Int.bmod (x + y) n := by
  have := (@bmod_add_mul_cancel (Int.bmod x n + y) n (bdiv x n)).symm
  rwa [Int.add_right_comm, bmod_add_bdiv] at this
Congruence of Balanced Modulus under Addition: $\text{bmod}(\text{bmod}(x, n) + y, n) = \text{bmod}(x + y, n)$
Informal description
For any integers $x$, $y$ and natural number $n$, the balanced modulus of the sum of the balanced modulus of $x$ with respect to $n$ and $y$ with respect to $n$ equals the balanced modulus of $x + y$ with respect to $n$, i.e., \[ \text{bmod}(\text{bmod}(x, n) + y, n) = \text{bmod}(x + y, n). \]
Int.bmod_sub_bmod_congr theorem
: Int.bmod (Int.bmod x n - y) n = Int.bmod (x - y) n
Full source
@[simp]
theorem bmod_sub_bmod_congr : Int.bmod (Int.bmod x n - y) n = Int.bmod (x - y) n :=
  @bmod_add_bmod_congr x n (-y)
Congruence of Balanced Modulus under Subtraction: $\text{bmod}(\text{bmod}(x, n) - y, n) = \text{bmod}(x - y, n)$
Informal description
For any integers $x$, $y$ and natural number $n$, the balanced modulus of the difference between the balanced modulus of $x$ with respect to $n$ and $y$ with respect to $n$ equals the balanced modulus of $x - y$ with respect to $n$, i.e., \[ \text{bmod}(\text{bmod}(x, n) - y, n) = \text{bmod}(x - y, n). \]
Int.add_bmod_eq_add_bmod_right theorem
(i : Int) (H : bmod x n = bmod y n) : bmod (x + i) n = bmod (y + i) n
Full source
theorem add_bmod_eq_add_bmod_right (i : Int)
    (H : bmod x n = bmod y n) : bmod (x + i) n = bmod (y + i) n := by
  rw [← bmod_add_bmod_congr, ← @bmod_add_bmod_congr y, H]
Right Addition Preserves Balanced Modulus Congruence: $\text{bmod}(x, n) = \text{bmod}(y, n) \implies \text{bmod}(x + i, n) = \text{bmod}(y + i, n)$
Informal description
For any integers $x$, $y$, $i$ and natural number $n$, if the balanced modulus of $x$ with respect to $n$ equals the balanced modulus of $y$ with respect to $n$, then the balanced modulus of $x + i$ with respect to $n$ equals the balanced modulus of $y + i$ with respect to $n$. In other words, if $\text{bmod}(x, n) = \text{bmod}(y, n)$, then $\text{bmod}(x + i, n) = \text{bmod}(y + i, n)$.
Int.bmod_add_cancel_right theorem
(i : Int) : bmod (x + i) n = bmod (y + i) n ↔ bmod x n = bmod y n
Full source
theorem bmod_add_cancel_right (i : Int) : bmodbmod (x + i) n = bmod (y + i) n ↔ bmod x n = bmod y n :=
  ⟨fun H => by
    have := add_bmod_eq_add_bmod_right (-i) H
    rwa [Int.add_neg_cancel_right, Int.add_neg_cancel_right] at this,
  fun H => by rw [← bmod_add_bmod_congr, H, bmod_add_bmod_congr]⟩
Balanced Modulus Cancellation under Right Addition: $\text{bmod}(x + i, n) = \text{bmod}(y + i, n) \leftrightarrow \text{bmod}(x, n) = \text{bmod}(y, n)$
Informal description
For any integers $x$, $y$, $i$ and natural number $n$, the balanced modulus of $x + i$ with respect to $n$ equals the balanced modulus of $y + i$ with respect to $n$ if and only if the balanced modulus of $x$ with respect to $n$ equals the balanced modulus of $y$ with respect to $n$. In other words, \[ \text{bmod}(x + i, n) = \text{bmod}(y + i, n) \leftrightarrow \text{bmod}(x, n) = \text{bmod}(y, n). \]
Int.add_bmod_bmod theorem
: Int.bmod (x + Int.bmod y n) n = Int.bmod (x + y) n
Full source
@[simp] theorem add_bmod_bmod : Int.bmod (x + Int.bmod y n) n = Int.bmod (x + y) n := by
  rw [Int.add_comm x, Int.bmod_add_bmod_congr, Int.add_comm y]
Balanced Modulus Addition Identity: $\text{bmod}(x + \text{bmod}(y, n), n) = \text{bmod}(x + y, n)$
Informal description
For any integers $x$, $y$ and natural number $n$, the balanced modulus of the sum of $x$ and the balanced modulus of $y$ with respect to $n$ equals the balanced modulus of $x + y$ with respect to $n$, i.e., \[ \text{bmod}(x + \text{bmod}(y, n), n) = \text{bmod}(x + y, n). \]
Int.sub_bmod_bmod theorem
: Int.bmod (x - Int.bmod y n) n = Int.bmod (x - y) n
Full source
@[simp] theorem sub_bmod_bmod : Int.bmod (x - Int.bmod y n) n = Int.bmod (x - y) n := by
  apply (bmod_add_cancel_right (bmod y n)).mp
  rw [Int.sub_add_cancel, add_bmod_bmod, Int.sub_add_cancel]
Balanced Modulus Subtraction Identity: $\text{bmod}(x - \text{bmod}(y, n), n) = \text{bmod}(x - y, n)$
Informal description
For any integers $x$, $y$ and natural number $n$, the balanced modulus of the difference of $x$ and the balanced modulus of $y$ with respect to $n$ equals the balanced modulus of $x - y$ with respect to $n$, i.e., \[ \text{bmod}(x - \text{bmod}(y, n), n) = \text{bmod}(x - y, n). \]
Int.bmod_mul_bmod theorem
: Int.bmod (Int.bmod x n * y) n = Int.bmod (x * y) n
Full source
@[simp]
theorem bmod_mul_bmod : Int.bmod (Int.bmod x n * y) n = Int.bmod (x * y) n := by
  rw [bmod_def x n]
  split
  next p =>
    simp
  next p =>
    rw [Int.sub_mul, Int.sub_eq_add_neg, ← Int.mul_neg, bmod_add_mul_cancel, emod_mul_bmod_congr]
Balanced Modulus Invariance under Multiplication with Balanced Modulus: $\text{bmod}(\text{bmod}(x, n) \cdot y, n) = \text{bmod}(x \cdot y, n)$
Informal description
For any integers $x$, $y$ and natural number $n$, the balanced modulus of the product of the balanced modulus of $x$ with respect to $n$ and $y$ with respect to $n$ equals the balanced modulus of the product $x \cdot y$ with respect to $n$, i.e., \[ \text{bmod}(\text{bmod}(x, n) \cdot y, n) = \text{bmod}(x \cdot y, n). \]
Int.mul_bmod_bmod theorem
: Int.bmod (x * Int.bmod y n) n = Int.bmod (x * y) n
Full source
@[simp] theorem mul_bmod_bmod : Int.bmod (x * Int.bmod y n) n = Int.bmod (x * y) n := by
  rw [Int.mul_comm x, bmod_mul_bmod, Int.mul_comm x]
Balanced Modulus Invariance under Multiplication: $\text{bmod}(x \cdot \text{bmod}(y, n), n) = \text{bmod}(x \cdot y, n)$
Informal description
For any integers $x$, $y$ and natural number $n$, the balanced modulus of the product of $x$ and the balanced modulus of $y$ with respect to $n$ equals the balanced modulus of the product $x \cdot y$ with respect to $n$, i.e., \[ \text{bmod}(x \cdot \text{bmod}(y, n), n) = \text{bmod}(x \cdot y, n). \]
Int.add_bmod theorem
(a b : Int) (n : Nat) : (a + b).bmod n = (a.bmod n + b.bmod n).bmod n
Full source
theorem add_bmod (a b : Int) (n : Nat) : (a + b).bmod n = (a.bmod n + b.bmod n).bmod n := by
  simp
Balanced Modulus Addition Property: $\text{bmod}(a + b, n) = \text{bmod}(\text{bmod}(a, n) + \text{bmod}(b, n), n)$
Informal description
For any integers $a$, $b$ and natural number $n$, the balanced modulus of the sum $a + b$ with respect to $n$ equals the balanced modulus of the sum of the balanced moduli of $a$ and $b$ with respect to $n$, i.e., \[ \text{bmod}(a + b, n) = \text{bmod}(\text{bmod}(a, n) + \text{bmod}(b, n), n). \]
Int.emod_bmod theorem
{x : Int} {m : Nat} : bmod (x % m) m = bmod x m
Full source
theorem emod_bmod {x : Int} {m : Nat} : bmod (x % m) m = bmod x m := by
  simp [bmod]
Balanced Modulus of Euclidean Remainder: $\text{bmod}(x \% m, m) = \text{bmod}(x, m)$
Informal description
For any integer $x$ and natural number $m$, the balanced modulus of the Euclidean modulus of $x$ by $m$ equals the balanced modulus of $x$ by $m$, i.e., \[ \text{bmod}(x \% m, m) = \text{bmod}(x, m). \]
Int.bmod_bmod theorem
: bmod (bmod x m) m = bmod x m
Full source
@[simp] theorem bmod_bmod : bmod (bmod x m) m = bmod x m := by
  rw [bmod, bmod_emod]
  rfl
Idempotence of Balanced Modulus: $\text{bmod}(\text{bmod}(x, m), m) = \text{bmod}(x, m)$
Informal description
For any integer $x$ and natural number $m$, the balanced modulus function satisfies the idempotence property: \[ \text{bmod}(\text{bmod}(x, m), m) = \text{bmod}(x, m). \]
Int.bmod_zero theorem
: Int.bmod 0 m = 0
Full source
@[simp] theorem bmod_zero : Int.bmod 0 m = 0 := by
  dsimp [bmod]
  simp only [Int.zero_sub, ite_eq_left_iff, Int.neg_eq_zero]
  intro h
  rw [@Int.not_lt] at h
  match m with
  | 0 => rfl
  | (m+1) =>
    exfalso
    rw [Int.natCast_add, ofNat_one, Int.add_assoc, add_ediv_of_dvd_right] at h
    change _ + 2 / 2 ≤ 0 at h
    rw [Int.ediv_self, ← ofNat_two, ← ofNat_ediv, add_one_le_iff, ← @Int.not_le] at h
    exact h (ofNat_nonneg _)
    all_goals decide
Balanced Modulus of Zero: $\text{bmod}(0, m) = 0$
Informal description
For any natural number $m$, the balanced modulus function evaluated at $x = 0$ yields zero, i.e., $\text{bmod}(0, m) = 0$.
Int.dvd_bmod_sub_self theorem
{x : Int} {m : Nat} : (m : Int) ∣ bmod x m - x
Full source
theorem dvd_bmod_sub_self {x : Int} {m : Nat} : (m : Int) ∣ bmod x m - x := by
  dsimp [bmod]
  split
  · exact dvd_emod_sub_self
  · rw [Int.sub_sub, Int.add_comm, ← Int.sub_sub]
    exact Int.dvd_sub dvd_emod_sub_self (Int.dvd_refl _)
Divisibility of Balanced Modulus Difference: $m \mid (\text{bmod}(x, m) - x)$
Informal description
For any integer $x$ and natural number $m$, the difference between the balanced modulus $\text{bmod}(x, m)$ and $x$ is divisible by $m$, i.e., $m \mid (\text{bmod}(x, m) - x)$.
Int.le_bmod theorem
{x : Int} {m : Nat} (h : 0 < m) : -(m / 2) ≤ Int.bmod x m
Full source
theorem le_bmod {x : Int} {m : Nat} (h : 0 < m) : - (m/2) ≤ Int.bmod x m := by
  dsimp [bmod]
  have v : (m : Int) % 2 = 0 ∨ (m : Int) % 2 = 1 := emod_two_eq _
  split <;> rename_i w
  · refine Int.le_trans ?_ (Int.emod_nonneg _ ?_)
    · exact Int.neg_nonpos_of_nonneg (Int.ediv_nonneg (Int.ofNat_nonneg _) (by decide))
    · exact Int.ne_of_gt (ofNat_pos.mpr h)
  · simp [Int.not_lt] at w
    refine Int.le_trans ?_ (Int.sub_le_sub_right w _)
    rw [← ediv_add_emod m 2]
    generalize (m : Int) / 2 = q
    generalize h : (m : Int) % 2 = r at *
    rcases v with rfl | rfl
    · rw [Int.add_zero, Int.mul_ediv_cancel_left, Int.add_ediv_of_dvd_left,
        Int.mul_ediv_cancel_left, show (1 / 2 : Int) = 0 by decide, Int.add_zero,
        Int.neg_eq_neg_one_mul]
      conv => rhs; congr; rw [← Int.one_mul q]
      rw [← Int.sub_mul, show (1 - 2 : Int) = -1 by decide]
      apply Int.le_refl
      all_goals try decide
      all_goals apply Int.dvd_mul_right
    · rw [Int.add_ediv_of_dvd_left, Int.mul_ediv_cancel_left,
        show (1 / 2 : Int) = 0 by decide, Int.add_assoc, Int.add_ediv_of_dvd_left,
        Int.mul_ediv_cancel_left, show ((1 + 1) / 2 : Int) = 1 by decide, ← Int.sub_sub,
        Int.sub_eq_add_neg, Int.sub_eq_add_neg, Int.add_right_comm, Int.add_assoc q,
        show (1 + -1 : Int) = 0 by decide, Int.add_zero, ← Int.neg_mul]
      rw [Int.neg_eq_neg_one_mul]
      conv => rhs; congr; rw [← Int.one_mul q]
      rw [← Int.add_mul, show (1 + -2 : Int) = -1 by decide]
      apply Int.le_refl
      all_goals try decide
      all_goals try apply Int.dvd_mul_right
Lower Bound for Balanced Modulus: $-\lfloor m/2 \rfloor \leq \text{bmod}(x, m)$ for $m > 0$
Informal description
For any integer $x$ and positive natural number $m$, the balanced modulus $\text{bmod}(x, m)$ satisfies the inequality $-\lfloor m/2 \rfloor \leq \text{bmod}(x, m)$.
Int.bmod_lt theorem
{x : Int} {m : Nat} (h : 0 < m) : bmod x m < (m + 1) / 2
Full source
theorem bmod_lt {x : Int} {m : Nat} (h : 0 < m) : bmod x m < (m + 1) / 2 := by
  dsimp [bmod]
  split
  · assumption
  · apply Int.lt_of_lt_of_le
    · show _ < 0
      have : x % m < m := emod_lt_of_pos x (ofNat_pos.mpr h)
      exact Int.sub_neg_of_lt this
    · exact Int.le.intro_sub _ rfl
Upper Bound for Balanced Modulus: $\text{bmod}(x, m) < \frac{m + 1}{2}$ when $m > 0$
Informal description
For any integer $x$ and positive natural number $m$, the balanced modulus $\text{bmod}(x, m)$ satisfies $\text{bmod}(x, m) < \frac{m + 1}{2}$.
Int.bmod_eq_emod_of_lt theorem
{x : Int} {m : Nat} (hx : x % m < (m + 1) / 2) : bmod x m = x % m
Full source
theorem bmod_eq_emod_of_lt {x : Int} {m : Nat} (hx : x % m < (m + 1) / 2) : bmod x m = x % m := by
  simp [bmod, hx]
Balanced Modulus Equals Euclidean Remainder When Small
Informal description
For any integer $x$ and natural number $m$, if the Euclidean remainder $x \% m$ is less than $\frac{m+1}{2}$, then the balanced modulus $\text{bmod}(x, m)$ equals $x \% m$.
Int.bmod_eq_neg theorem
{n : Nat} {m : Int} (hm : 0 ≤ m) (hn : n = 2 * m) : m.bmod n = -m
Full source
theorem bmod_eq_neg {n : Nat} {m : Int} (hm : 0 ≤ m) (hn : n = 2 * m) : m.bmod n = -m := by
  by_cases h : m = 0
  · subst h; simp
  · rw [Int.bmod_def, hn, if_neg]
    · rw [Int.emod_eq_of_lt hm] <;> omega
    · simp only [Int.not_lt]
      rw [Int.emod_eq_of_lt hm] <;> omega
Balanced Modulus Identity for Even Modulus: $\text{bmod}(m, 2m) = -m$ when $m \geq 0$
Informal description
For any natural number $n$ and integer $m$ such that $0 \leq m$ and $n = 2m$, the balanced modulus of $m$ modulo $n$ equals $-m$, i.e., $\text{bmod}(m, n) = -m$.
Int.bmod_le theorem
{x : Int} {m : Nat} (h : 0 < m) : bmod x m ≤ (m - 1) / 2
Full source
theorem bmod_le {x : Int} {m : Nat} (h : 0 < m) : bmod x m ≤ (m - 1) / 2 := by
  refine lt_add_one_iff.mp ?_
  calc
    bmod x m < (m + 1) / 2  := bmod_lt h
    _ = ((m + 1 - 2) + 2)/2 := by simp
    _ = (m - 1) / 2 + 1     := by
      rw [add_ediv_of_dvd_right]
      · simp +decide only [Int.ediv_self]
        congr 2
        rw [Int.add_sub_assoc, ← Int.sub_neg]
        congr
      · trivial
Upper Bound for Balanced Modulus: $\text{bmod}(x, m) \leq \frac{m - 1}{2}$ when $m > 0$
Informal description
For any integer $x$ and positive natural number $m$, the balanced modulus $\text{bmod}(x, m)$ satisfies $\text{bmod}(x, m) \leq \frac{m - 1}{2}$.
Int.bmod_natAbs_plus_one theorem
(x : Int) (w : 1 < x.natAbs) : bmod x (x.natAbs + 1) = -x.sign
Full source
theorem bmod_natAbs_plus_one (x : Int) (w : 1 < x.natAbs) : bmod x (x.natAbs + 1) = - x.sign := by
  have t₁ : ∀ (x : Nat), x % (x + 2) = x :=
    fun x => Nat.mod_eq_of_lt (Nat.lt_succ_of_lt (Nat.lt.base x))
  have t₂ : ∀ (x : Int), 0 ≤ x → x % (x + 2) = x := fun x h => by
    match x, h with
    | Int.ofNat x, _ => erw [← Int.ofNat_two, ← ofNat_add, ← ofNat_emod, t₁]; rfl
  cases x with
  | ofNat x =>
    simp only [bmod, ofNat_eq_coe, natAbs_ofNat, Int.natCast_add, ofNat_one,
      emod_self_add_one (ofNat_nonneg x)]
    match x with
    | 0 => rw [if_pos] <;> simp +decide
    | (x+1) =>
      rw [if_neg]
      · simp [← Int.sub_sub]
      · refine Int.not_lt.mpr ?_
        simp only [← Int.natCast_add, ← ofNat_one, ← ofNat_two, ← ofNat_ediv]
        match x with
        | 0 => apply Int.le_refl
        | (x+1) =>
          refine Int.ofNat_le.mpr ?_
          apply Nat.div_le_of_le_mul
          simp only [Nat.two_mul, Nat.add_assoc]
          apply Nat.add_le_add_left (Nat.add_le_add_left (Nat.add_le_add_left (Nat.le_add_left
            _ _) _) _)
  | negSucc x =>
    rw [bmod, natAbs_negSucc, Int.natCast_add, ofNat_one, sign_negSucc, Int.neg_neg,
      Nat.succ_eq_add_one, negSucc_emod]
    erw [t₂]
    · rw [Int.natCast_add, ofNat_one, Int.add_sub_cancel, Int.add_comm, Int.add_sub_cancel, if_pos]
      · match x, w with
        | (x+1), _ =>
          rw [Int.add_assoc, add_ediv_of_dvd_right, show (1 + 1 : Int) = 2 by decide, Int.ediv_self]
          apply Int.lt_add_one_of_le
          rw [Int.add_comm, ofNat_add, Int.add_assoc, add_ediv_of_dvd_right,
            show ((1 : Nat) + 1 : Int) = 2 by decide, Int.ediv_self]
          apply Int.le_add_of_nonneg_left
          exact Int.le.intro_sub _ rfl
          all_goals decide
    · exact ofNat_nonneg x
    · exact succ_ofNat_pos (x + 1)
Balanced Modulus Identity for Integers with Large Absolute Value: $x \bmod (|x| + 1) = -\text{sign}(x)$
Informal description
For any integer $x$ with absolute value $|x| > 1$, the balanced modulus of $x$ with respect to $|x| + 1$ equals the negative of the sign of $x$, i.e., $$x \bmod (|x| + 1) = -\text{sign}(x).$$
Int.bmod_neg_bmod theorem
: bmod (-(bmod x n)) n = bmod (-x) n
Full source
@[simp]
theorem bmod_neg_bmod : bmod (-(bmod x n)) n = bmod (-x) n := by
  apply (bmod_add_cancel_right x).mp
  rw [Int.add_left_neg, ← add_bmod_bmod, Int.add_left_neg]
Balanced Modulus Negation Identity: $\text{bmod}(-(\text{bmod}(x, n)), n) = \text{bmod}(-x, n)$
Informal description
For any integer $x$ and natural number $n$, the balanced modulus of the negation of the balanced modulus of $x$ with respect to $n$ equals the balanced modulus of $-x$ with respect to $n$, i.e., \[ \text{bmod}(-(\text{bmod}(x, n)), n) = \text{bmod}(-x, n). \]
Int.dvd_eq_true_of_mod_eq_zero theorem
{a b : Int} (h : b % a == 0) : (a ∣ b) = True
Full source
protected theorem dvd_eq_true_of_mod_eq_zero {a b : Int} (h : b % a == 0) : (a ∣ b) = True := by
  simp [Int.dvd_of_emod_eq_zero, eq_of_beq h]
Divisibility Criterion via Zero Remainder: $a \mid b$ when $b \bmod a = 0$
Informal description
For any integers $a$ and $b$, if the remainder of $b$ divided by $a$ is zero (i.e., $b \bmod a = 0$), then $a$ divides $b$ (i.e., $a \mid b$ holds true).
Int.dvd_eq_false_of_mod_ne_zero theorem
{a b : Int} (h : b % a != 0) : (a ∣ b) = False
Full source
protected theorem dvd_eq_false_of_mod_ne_zero {a b : Int} (h : b % a != 0) : (a ∣ b) = False := by
  simp [eq_of_beq] at h
  simp [Int.dvd_iff_emod_eq_zero, h]
Non-divisibility Criterion via Non-zero Remainder
Informal description
For any integers $a$ and $b$, if the remainder of $b$ divided by $a$ is not zero (i.e., $b \bmod a \neq 0$), then $a$ does not divide $b$ (i.e., $a \nmid b$ holds).