3. Parity and divisibility

The problems in this chapter concern some elementary properties of natural numbers and integers: parity (whether a number is even or odd), divisibility, and congruence modulo n.

There are no new logical symbols like or in this chapter, and no major additions (such as the use of intermediate steps or lemmas) to our reasoning toolkit. Thus this chapter functions as a breather between the hard work of Chapter 2 and Chapter 4, a chance to consolidate what you have learned.

3.1. Definitions; parity

3.1.1. Example

When a mathematical term is introduced for the first time, it is given a definition.

Definition

An integer a is odd, if there exists an integer k, such that a=2k+1.

Here’s how we make a definition in Lean.

def Odd (a : ) : Prop :=  k, a = 2 * k + 1

On paper, you need to memorize every definition; this is crucial to be able to work with them. In Lean, when you see a term whose definition you’d like to double-check, you can right-click (Windows) / two-finger-click (Mac) to bring up an option “Go to Definition”. This will send you to the place in the Lean code where the definition was introduced. (The keyboard shortcut Ctrl-- will bring you back from that definition to where you were working before.)

Problem

Show that 7 is odd.

Solution

7=23+1, so 7 is odd.

Here’s how this problem looks in Lean.

example : Odd (7:) := by
  sorry

The goal state at the start is:

⊢ Odd 7

You can check a definition within a proof using the dsimp (“definitional-simplify”) tactic. Here, you can type dsimp [Odd] within the proof to have the definition of “odd” unfolded for you:

example : Odd (7:) := by
  dsimp [Odd]

After doing this, the goal is displayed using the definition of “odd” rather than with the word “odd”:

⊢ ∃ (k : ℤ), 7 = 2 * k + 1

This is optional, though; the proof will still work if you delete the dsimp line.

Here is the full Lean translation of the solution.

example : Odd (7 : ) := by
  dsimp [Odd]
  use 3
  numbers

If you don’t quite follow the proof – either in its text version or its Lean version – then reread some of the examples in Section 2.5, such as Example 2.5.3.

3.1.2. Example

Here’s a similar example.

Problem

Show that 3 is odd.

example : Odd (-3 : ) := by
  sorry

You should show that 3=2k+1 for some integer k. But what is the k that works?

3.1.3. Example

Problem

Prove that if n is an odd integer, then 3n+2 is odd.

Solution

Let n be odd. Then there exists an integer k such that n=2k+1. Thus

3n+2=3(2k+1)+2=2(3k+2)+1,

so 3n+2 is odd.

In the Lean version of the problem, the goal state initially looks like this.

n : ℤ
hn : Odd n
⊢ Odd (3 * n + 2)

If you want to unfold the definition Odd in the goal, you can use dsimp [Odd], as discussed in Example 3.1.1. If you want to unfold the definition Odd everywhere (goals and hypotheses), you can use dsimp [Odd] at *. After that the goal state looks like this:

n : ℤ
hn : ∃ (k : ℤ), n = 2 * k + 1
⊢ ∃ (k : ℤ), 3 * n + 2 = 2 * k + 1

This is our first encounter with something which might be confusing. The variable inside an existential is a “throwaway” variable, which exists only for the duration of the sentence. So the same variable might occur within the goal state repeatedly, without meaning the same thing. Here the k in the hypothesis hn is different from the k in the goal; it’s just the default name when the definition “odd” is unfolded.

example {n : } (hn : Odd n) : Odd (3 * n + 2) := by
  dsimp [Odd] at *
  obtain k, hk := hn
  use 3 * k + 2
  calc
    3 * n + 2 = 3 * (2 * k + 1) + 2 := by rw [hk]
    _ = 2 * (3 * k + 2) + 1 := by ring

Again, you can check that the dsimp line is not actually needed in the solution.

3.1.4. Example

Problem

Let n be an integer. Prove that if n is odd, then 7n4 is odd.

example {n : } (hn : Odd n) : Odd (7 * n - 4) := by
  sorry

3.1.5. Example

Problem

Prove that if the integers x and y are odd, then x+y+1 is odd.

Solution

Since x and y are odd, there exists an integer a such that x=2a+1, and there exists an integer b such that y=2b+1. Then

x+y+1=(2a+1)+(2b+1)+1=2(a+b+1)+1,

so x+y+1 is odd.

example {x y : } (hx : Odd x) (hy : Odd y) : Odd (x + y + 1) := by
  obtain a, ha := hx
  obtain b, hb := hy
  use a + b + 1
  calc
    x + y + 1 = 2 * a + 1 + (2 * b + 1) + 1 := by rw [ha, hb]
    _ = 2 * (a + b + 1) + 1 := by ring

3.1.6. Example

Problem

Prove that if the integers x and y are odd, then xy+2y is odd.

Solution

Since x and y are odd, there exists an integer a such that x=2a+1, and there exists an integer b such that y=2b+1. Then

xy+2y=(2a+1)(2b+1)+2(2b+1)=2(2ab+3b+a+1)+1,

so xy+2y is odd.

example {x y : } (hx : Odd x) (hy : Odd y) : Odd (x * y + 2 * y) := by
  sorry

3.1.7. Example

You can probably guess the definition of “even”.

Definition

An integer a is even, if there exists an integer k, such that a=2k.

def Even (a : ) : Prop :=  k, a = 2 * k

Problem

Let m be an integer. Prove that if m is odd, then 3m5 is even.

Solution

Since m is odd, there exists an integer t such that m=2t+1. Thus

3m5=3(2t+1)5=2(3t1),

so 3m5 is even.

example {m : } (hm : Odd m) : Even (3 * m - 5) := by
  sorry

3.1.8. Example

Problem

Let n be an even integer. Prove that n2+2n5 is odd.

example {n : } (hn : Even n) : Odd (n ^ 2 + 2 * n - 5) := by
  sorry

3.1.9. Example

In fact every integer is either even or odd; we will discuss how to prove this later, in Example 4.2.9.

In Lean this fact can be invoked as the lemma Int.even_or_odd:

lemma Int.even_or_odd (n : ) : Even n  Odd n :=

Problem

Let n be an integer. Prove that n2+n+4 is even.

Solution

We consider two cases, depending on whether n is even or odd.

If n is even, then there exists an integer x such that n=2x. Then

n2+n+4=(2x)2+2x+4=2(2x2+x+2),

so n2+n+4 is even.

If n is odd, then there exists an integer x such that n=2x+1. Then

n2+n+4=(2x+1)2+(2x+1)+4=2(2x2+3x+3),

so n2+n+4 is again even.

example (n : ) : Even (n ^ 2 + n + 4) := by
  obtain hn | hn := Int.even_or_odd n
  · obtain x, hx := hn
    use 2 * x ^ 2 + x + 2
    calc
      n ^ 2 + n + 4 = (2 * x) ^ 2 + 2 * x + 4 := by rw [hx]
      _ = 2 * (2 * x ^ 2 + x + 2) := by ring
  · obtain x, hx := hn
    use 2 * x ^ 2 + 3 * x + 3
    calc
      n ^ 2 + n + 4 = (2 * x + 1) ^ 2 + (2 * x + 1) + 4 := by rw [hx]
      _ = 2 * (2 * x ^ 2 + 3 * x + 3) := by ring

3.1.10. Exercises

  1. Prove that -9 is odd.

    example : Odd (-9 : ) := by
      sorry
    
  2. Prove that 26 is even.

    example : Even (26 : ) := by
      sorry
    
  3. Let m be an odd integer and n be an even integer. Prove that n+m is odd.

    example {m n : } (hm : Odd m) (hn : Even n) : Odd (n + m) := by
      sorry
    
  4. Let p be an odd integer and let q be an even integer. Show that pq4 is odd.

    example {p q : } (hp : Odd p) (hq : Even q) : Odd (p - q - 4) := by
      sorry
    
  5. Let a be an even integer and let b be an odd integer. Show that 3a+b3 is even.

    example {a b : } (ha : Even a) (hb : Odd b) : Even (3 * a + b - 3) := by
      sorry
    
  6. Prove that if the integers r and s are odd, then 3r5s is even.

    example {r s : } (hr : Odd r) (hs : Odd s) : Even (3 * r - 5 * s) := by
      sorry
    
  7. Let x be an integer. Show that if x is odd then so is x3.

    example {x : } (hx : Odd x) : Odd (x ^ 3) := by
      sorry
    
  8. Let n be an odd integer. Show that n23n+2 is even.

    example {n : } (hn : Odd n) : Even (n ^ 2 - 3 * n + 2) := by
      sorry
    
  9. Let a be an integer and suppose that a is odd. Show that a2+2a4 is odd.

    example {a : } (ha : Odd a) : Odd (a ^ 2 + 2 * a - 4) := by
      sorry
    
  10. Let p be an odd integer. Show that p2+3p5 is odd.

    example {p : } (hp : Odd p) : Odd (p ^ 2 + 3 * p - 5) := by
      sorry
    
  11. Let x and y be odd integers. Show that xy is odd.

    example {x y : } (hx : Odd x) (hy : Odd y) : Odd (x * y) := by
      sorry
    
  12. Let n be an integer. Show that 3n2+3n1 is odd.

    example (n : ) : Odd (3 * n ^ 2 + 3 * n - 1) := by
      sorry
    
  13. Let n be an integer. Show that there exists an integer mn which is odd.

    example (n : ) :  m  n, Odd m := by
      sorry
    
  14. Let a, b and c be integers. Show that at least one of ab, a+c or bc is even. 1

    example (a b c : ) : Even (a - b)  Even (a + c)  Even (b - c) := by
      sorry
    

Footnotes

1

Exercise taken from Hammack, Book of Proof, Chapter 9.

3.2. Divisibility

3.2.1. Example

Another mathematical definition you may have seen before is the definition of divisibility.

Definition

A natural number b is divisible by another natural number a, if there exists a natural number c, such that b=ac.

For example,

Problem

Show that the natural number 88 is divisible by 11.

Solution

88=118.

Divisibility is a very important concept, and there are several different names for it. All the following mean the same thing:

  • b is divisible by a

  • b is a multiple of a

  • a is a divisor of b

  • a is a factor of b

  • a divides b

We will most frequently use the last of these, “a divides b,” since it’s the most compact. There is also a standard notation, ab, which we will also use frequently.

In Lean, the definition of divisibility is already in the library, together with many theorems about it. We will typically work with this definition in Lean using the notation, , which you can type in Lean as \| or \mid. (In general, hover over a symbol in VSCode with your mouse to find out how to type it.) Warning: this is different from the visually similar symbol | which appears on your keyboard and which we use in obtain statements.

As in Section 3.1 with dsimp [Odd], it is possible to unfold the definition of divisibility in the middle of a Lean proof to remind yourself of what it means in the current context. You can write either dsimp [Dvd.dvd] (this being the text name of the Lean notation for divisibility) or dsimp [(· ·)] – unfortunately dsimp [∣] without the dots and parentheses does not work. As in the Section 3.1 examples, this unfolding is optional – the proof still works without it.

example : (11 : )  88 := by
  dsimp [(·  ·)]
  use 8
  numbers

3.2.2. Example

Another feature of this definition is that, although we stated it above for natural numbers, we will also often want to consider divisibility of integers. Here is the corresponding definition for integers:

Definition

An integer b is divisible by another integer a, if there exists an integer c, such that b=ac.

We also use all the same variant terminology and the same notation ab for integer divisibility.

Problem

Show that the integer 6 is divisible by -2.

Solution

6=23.

example : (-2 : )  6 := by
  sorry

3.2.3. Example

Problem

Let a and b be integers and suppose that ab. Show that ab2+2b.

Solution

Since ab, there exists an integer k such that b=ak. Then

b2+2b=(ak)2+2(ak)=a(k(ak+2)).

So ab2+2b.

example {a b : } (hab : a  b) : a  b ^ 2 + 2 * b := by
  obtain k, hk := hab
  use k * (a * k + 2)
  calc
    b ^ 2 + 2 * b = (a * k) ^ 2 + 2 * (a * k) := by rw [hk]
    _ = a * (k * (a * k + 2)) := by ring

3.2.4. Example

Problem

Let a, b and c be natural numbers and suppose that ab and b2c. Show that a2c.

Solution

Since ab, there exists a natural number x such that b=ax. Since b2c, there exists a natural number y such that c=b2y. Then

c=b2y=(ax)2y=a2(x2y).

So a2c.

Translate this solution into Lean. As usual, if you wish, you can use the command dsimp [(· ·)] at * to unfold to its definition everywhere in the goal state, though this will have no effect on the correctness of the proof.

example {a b c : } (hab : a  b) (hbc : b ^ 2  c) : a ^ 2  c := by
  sorry

3.2.5. Example

Problem

Let x, y and z be natural numbers and suppose that xyz. Show that xz.

Solution

Since xyz, there exists a natural number t such that z=(xy)t. Then

z=(xy)t=x(yt).

So xz.

example {x y z : } (h : x * y  z) : x  z := by
  sorry

3.2.6. Example

You might wonder how to show that a number is not divisible by another number. A convenient test here is a theorem which we will prove later in the book, in Example 4.5.8: if an integer a is strictly between two consecutive multiples of an integer b, then it is not a multiple of b. More formally, if there exists an integer q such that bq<a<b(q+1), then a is not a multiple of b. Here is an example applying this test:

Problem

Show that 12 is not divisible by 5.

Solution

52<12<5(2+1).

In Lean, this test is available as the lemma Int.not_dvd_of_exists_lt_and_lt:

lemma Int.not_dvd_of_exists_lt_and_lt (a b : )
  (h :  q, b * q < a  a < b * (q + 1)) :
  ¬b  a :=

And here is the same solution written up in Lean.

example : ¬(5 : )  12 := by
  apply Int.not_dvd_of_exists_lt_and_lt
  use 2
  constructor
  · numbers -- show `5 * 2 < 12`
  · numbers -- show `12 < 5 * (2 + 1)`

3.2.7. Example

Problem

Let a and b be natural numbers, with b positive, and suppose that a divides b. Show that ab.

Solution

Since ab, there exists a natural number k such that b=ak.

We first note that

0<b=ak

so 0<k. Thus in fact 1k.

Now, we have

a=a1ak=b.
example {a b : } (hb : 0 < b) (hab : a  b) : a  b := by
  obtain k, hk := hab
  have H1 :=
    calc
      0 < b := hb
      _ = a * k := hk
  cancel a at H1
  have H : 1  k := H1
  calc
    a = a * 1 := by ring
    _  a * k := by rel [H]
    _ = b := by rw [hk]

This lemma is available in the main Lean library under the name Nat.le_of_dvd.

3.2.8. Example

Problem

Let a and b be natural numbers, with b positive, and suppose that a divides b. Show that a is positive.

Solution

Since ab, there exists a natural number k such that b=ak.

We have

0<b=ak,

so 0<a.

example {a b : } (hab : a  b) (hb : 0 < b) : 0 < a := by
  sorry

This lemma is also available in the main Lean library, under the name Nat.pos_of_dvd_of_pos.

3.2.9. Exercises

  1. Show that 0 is divisible by every integer t.

    example (t : ) : t  0 := by
      sorry
    
  2. Show that -10 is not divisible by 3.

    example : ¬(3 : )  -10 := by
      sorry
    
  3. Let x and y be integers and suppose that xy. Show that x3y4y2.

    example {x y : } (h : x  y) : x  3 * y - 4 * y ^ 2 := by
      sorry
    
  4. Let m and n be integers and suppose that mn. Show that m2n3+n.

    example {m n : } (h : m  n) : m  2 * n ^ 3 + n := by
      sorry
    
  5. Let a and b be integers and suppose that ab. Show that a2b3b2+3b.

    example {a b : } (hab : a  b) : a  2 * b ^ 3 - b ^ 2 + 3 * b := by
      sorry
    
  6. Let k, l and m be integers and suppose that k divides l and l3 divides m. Show that k3 divides m.

    example {k l m : } (h1 : k  l) (h2 : l ^ 3  m) : k ^ 3  m := by
      sorry
    
  7. Let p, q and r be integers and suppose that p3 divides q and q2 divides r. Show that p6 divides r.

    example {p q r : } (hpq : p ^ 3  q) (hqr : q ^ 2  r) : p ^ 6  r := by
      sorry
    
  8. Show that there exists a natural number n>0, such that 9 is a factor of 2n1.

    example :  n : , 0 < n  9  2 ^ n - 1 := by
      sorry
    
  9. Show that there exists integers a and b, with 0<b<a, such that aba+b.

    example :  a b : , 0 < b  b < a  a - b  a + b := by
      sorry
    

3.3. Modular arithmetic: theory

Definition

The integers a and b are congruent modulo n, if n(ab).

We use the notation abmodn to denote that a and b are congruent modulo n.

def Int.ModEq (n a b : ) : Prop := n  a - b

notation:50 a " ≡ " b " [ZMOD " n "]" => Int.ModEq n a b

3.3.1. Example

Problem

Show that 113mod4.

Solution

113=42, so 4(113).

example : 11  3 [ZMOD 4] := by
  use 2
  numbers

3.3.2. Example

Problem

Show that 51mod3.

Solution

51=32, so 3(51).

example : -5  1 [ZMOD 3] := by
  sorry

3.3.3. Example

Lemma (addition rule for modular arithmetic)

Let a, b, c, d and n be integers, and suppose that abmodn and cdmodn. Then a+cb+dmodn.

Proof

Since abmodn, there exists an integer x such that ab=nx. Since cdmodn, there exists an integer y such that cd=ny. Then

a+c(b+d)=(ab)+(cd)=nx+ny=n(x+y),

so a+cb+dmodn.

theorem Int.ModEq.add {n a b c d : } (h1 : a  b [ZMOD n]) (h2 : c  d [ZMOD n]) :
    a + c  b + d [ZMOD n] := by
  dsimp [Int.ModEq] at *
  obtain x, hx := h1
  obtain y, hy := h2
  use x + y
  calc
    a + c - (b + d) = a - b + (c - d) := by ring
    _ = n * x + n * y := by rw [hx, hy]
    _ = n * (x + y) := by ring

3.3.4. Exercise

Lemma (subtraction rule for modular arithmetic)

Let a, b, c, d and n be integers, and suppose that abmodn and cdmodn. Then acbdmodn.

theorem Int.ModEq.sub {n a b c d : } (h1 : a  b [ZMOD n]) (h2 : c  d [ZMOD n]) :
    a - c  b - d [ZMOD n] := by
  sorry

3.3.5. Exercise

Lemma (negation rule for modular arithmetic)

Let a, b and n be integers, and suppose that abmodn. Then abmodn.

theorem Int.ModEq.neg {n a b : } (h1 : a  b [ZMOD n]) : -a  -b [ZMOD n] := by
  sorry

3.3.6. Example

Lemma (multiplication rule for modular arithmetic)

Let a, b, c, d and n be integers, and suppose that abmodn and cdmodn. Then acbdmodn.

Proof

Since abmodn, there exists an integer x such that ab=nx. Since cdmodn, there exists an integer y such that cd=ny. Then

acbd=(ab)c+b(cd)=(nx)c+b(ny)=n(xc+by),

so acbdmodn.

theorem Int.ModEq.mul {n a b c d : } (h1 : a  b [ZMOD n]) (h2 : c  d [ZMOD n]) :
    a * c  b * d [ZMOD n] := by
  obtain x, hx := h1
  obtain y, hy := h2
  use x * c + b * y
  calc
    a * c - b * d = (a - b) * c + b * (c - d) := by ring
    _ = n * x * c + b * (n * y) := by rw [hx, hy]
    _ = n * (x * c + b * y) := by ring

3.3.7. Example

Warning: There is no “division rule” for modular arithmetic!

Problem

It is possible to have integers a, b, c, d and n with abmodn and cdmodn, but acbdmodn.

Solution

We can take a=10, b=18, c=2, d=6. Indeed,

  • 1018=42, so 1018mod4;

  • 26=41, so 26mod4;

  • 102186=2 lies between the consecutive multiples 40 and 4(0+1) of 4, so 102186mod4.

Notice that here we’re using the test for non-divisibility from Example 3.2.6.

3.3.8. Example

Lemma (squaring rule for modular arithmetic)

Let a, b and n be integers, and suppose that abmodn. Then a2b2modn.

Proof

Since abmodn, there exists an integer x such that ab=nx. Then

a2b2=(ab)(a+b)=(nx)(a+b)=n(x(a+b)).
theorem Int.ModEq.pow_two (h : a  b [ZMOD n]) : a ^ 2  b ^ 2 [ZMOD n] := by
  obtain x, hx := h
  use x * (a + b)
  calc
    a ^ 2 - b ^ 2 = (a - b) * (a + b) := by ring
    _ = n * x * (a + b) := by rw [hx]
    _ = n * (x * (a + b)) := by ring

3.3.9. Exercise

Lemma (cubing rule for modular arithmetic)

Let a, b and n be integers, and suppose that abmodn. Then a3b3modn.

theorem Int.ModEq.pow_three (h : a  b [ZMOD n]) : a ^ 3  b ^ 3 [ZMOD n] := by
  sorry

In fact the same is true for any power, although we don’t yet have the tools to prove it. We’ll come back to this later in the book, in Example 6.1.3.

Lemma (power rule for modular arithmetic)

Let k be a natural number and let a, b and n be integers, and suppose that abmodn. Then akbkmodn.

theorem Int.ModEq.pow (k : ) (h : a  b [ZMOD n]) : a ^ k  b ^ k [ZMOD n] :=
  sorry -- we'll prove this later in the book

3.3.10. Example

Lemma (reflexivity rule for modular arithmetic)

Let a and n be integers. Then aamodn.

Proof

aa=n0, so naa.

theorem Int.ModEq.refl (a : ) : a  a [ZMOD n] := by
  use 0
  ring

3.3.11. Example

Whew! That was a lot of lemmas. But they pay off, as you will now see. Suppose we now encounter some very specific modular arithmetic problem of the same general type as we’ve seen before: the congruence modulo n of two expressions which differ in a kind of spot-the-difference way, like a “rewriting” by one congruence. For example,

Problem

Let a and b be integers, and suppose that a2mod4. Show that ab2+a2b+3a2b2+22b+32mod4.

We could solve this by working directly from the definition, which is rather painful:

example {a b : } (ha : a  2 [ZMOD 4]) :
    a * b ^ 2 + a ^ 2 * b + 3 * a  2 * b ^ 2 + 2 ^ 2 * b + 3 * 2 [ZMOD 4] := by
  obtain x, hx := ha
  use x * (b ^ 2 + a * b + 2 * b + 3)
  calc
    a * b ^ 2 + a ^ 2 * b + 3 * a - (2 * b ^ 2 + 2 ^ 2 * b + 3 * 2) =
        (a - 2) * (b ^ 2 + a * b + 2 * b + 3) :=
      by ring
    _ = 4 * x * (b ^ 2 + a * b + 2 * b + 3) := by rw [hx]
    _ = 4 * (x * (b ^ 2 + a * b + 2 * b + 3)) := by ring

Or, better, we can solve this by applying the right combination of the lemmas we already proved, in the right order. This requires much less thinking:

example {a b : } (ha : a  2 [ZMOD 4]) :
    a * b ^ 2 + a ^ 2 * b + 3 * a  2 * b ^ 2 + 2 ^ 2 * b + 3 * 2 [ZMOD 4] := by
  apply Int.ModEq.add
  apply Int.ModEq.add
  apply Int.ModEq.mul
  apply ha
  apply Int.ModEq.refl
  apply Int.ModEq.mul
  apply Int.ModEq.pow
  apply ha
  apply Int.ModEq.refl
  apply Int.ModEq.mul
  apply Int.ModEq.refl
  apply ha

3.3.12. Exercises

  1. Show that 34104mod5.

    example : 34  104 [ZMOD 5] := by
      sorry
    
  2. (symmetry rule for modular arithmetic) Let a, b and n be integers, and suppose that abmodn. Show that bamodn.

    theorem Int.ModEq.symm (h : a  b [ZMOD n]) : b  a [ZMOD n] := by
      sorry
    
  3. (transitivity rule for modular arithmetic) Let a, b, c and n be integers, and suppose that abmodn and bcmodn. Show that acmodn.

    theorem Int.ModEq.trans (h1 : a  b [ZMOD n]) (h2 : b  c [ZMOD n]) :
        a  c [ZMOD n] := by
      sorry
    
  4. Let a, c and n be integers. Show that a+ncamodn.

    example : a + n * c  a [ZMOD n] := by
      sorry
    
  5. Give an alternative solution (i.e., with different numbers) to Example 3.3.7.

  6. Let a and b be integers, and suppose that abmod5. Show that 2a+32b+3mod5.

    Give two solutions, following the style of the two solutions in Example 3.3.11.

    example {a b : } (h : a  b [ZMOD 5]) : 2 * a + 3  2 * b + 3 [ZMOD 5] := by
      sorry
    
  7. Let m and n be integers, and suppose that mnmod4. Show that 3m13n1mod4.

    Give two solutions, following the style of the two solutions in Example 3.3.11.

    example {m n : } (h : m  n [ZMOD 4]) : 3 * m - 1  3 * n - 1 [ZMOD 4] := by
      sorry
    
  8. Let k be an integer, and suppose that k3mod5. Show that 4k+k3+343+33+3mod5.

    Give two solutions, following the style of the two solutions in Example 3.3.11.

    example {k : } (hb : k  3 [ZMOD 5]) :
        4 * k + k ^ 3 + 3  4 * 3 + 3 ^ 3 + 3 [ZMOD 5] := by
      sorry
    

3.4. Modular arithmetic: calculations

3.4.1. Example

Recall the problem from Example 3.3.11.

Problem

Let a and b be integers, and suppose that a2mod4. Show that ab2+a2b+3a2b2+22b+32mod4.

After solving this and the many similar problems in Section 3.3, you probably feel like you can check the correctness of any statement of this style by sight. And that’s great! When we have built up enough theory that a particular class of statement can be checked by sight, we will stop requiring detailed proofs. So for now on we’ll take such statements for granted.

This is also usually the point at which it’s easy to write a Lean tactic which checks the correctness of a class of statement. I’ve done exactly this, updating tactic rel to cover this kind of statement. We won’t discuss tactic-writing in this book, but effectively, the tactic now throws the lemmas Int.ModEq.add, Int.ModEq.neg, Int.ModEq.sub, Int.ModEq.mul, Int.ModEq.pow, Int.ModEq.refl and the provided hypotheses at the statement until (a) the goal is solved or (b) none of these lemmas apply any more. And that’s exactly what we’re doing in our heads when we check the paper statement of the problem.

example {a b : } (ha : a  2 [ZMOD 4]) :
    a * b ^ 2 + a ^ 2 * b + 3 * a  2 * b ^ 2 + 2 ^ 2 * b + 3 * 2 [ZMOD 4] := by
  rel [ha]

3.4.2. Example

From now on, we will solve more interesting modular arithmetic problems, dealing with steps like Example 3.3.11 in a single line.

Problem

Let a and b be integers, with a4mod5 and b3mod5. Show that ab+b3+32mod5.

Solution

ab+b3+34b+b3+3mod543+33+3mod5=2+582mod5.
example {a b : } (ha : a  4 [ZMOD 5]) (hb : b  3 [ZMOD 5]) :
    a * b + b ^ 3 + 3  2 [ZMOD 5] :=
  calc
    a * b + b ^ 3 + 3  4 * b + b ^ 3 + 3 [ZMOD 5] := by rel [ha]
    _  4 * 3 + 3 ^ 3 + 3 [ZMOD 5] := by rel [hb]
    _ = 2 + 5 * 8 := by numbers
    _  2 [ZMOD 5] := by extra

3.4.3. Example

Problem

Show that there exists an integer a, such that 6a4mod11.

Solution

The integer 8 has this property. Indeed,

68=4+4114mod11.
example :  a : , 6 * a  4 [ZMOD 11] := by
  use 8
  calc
    (6:) * 8 = 4 + 4 * 11 := by numbers
    _  4 [ZMOD 11] := by extra

3.4.4. Example

Problem

Let x be an integer. Show that x3xmod3.

Solution

We consider cases according to the residue of x modulo 3.

Case 1 (x0mod3):

x303mod3=0xmod3.

Case 2 (x1mod3):

x313mod3=1xmod3.

Case 3 (x2mod3):

x323mod3=2+322mod3xmod3.
example {x : } : x ^ 3  x [ZMOD 3] := by
  mod_cases hx : x % 3
  calc
    x ^ 3  0 ^ 3 [ZMOD 3] := by rel [hx]
    _ = 0 := by numbers
    _  x [ZMOD 3] := by rel [hx]
  calc
    x ^ 3  1 ^ 3 [ZMOD 3] := by rel [hx]
    _ = 1 := by numbers
    _  x [ZMOD 3] := by rel [hx]
  calc
    x ^ 3  2 ^ 3 [ZMOD 3] := by rel [hx]
    _ = 2 + 3 * 2 := by numbers
    _  2 [ZMOD 3] := by extra
    _  x [ZMOD 3] := by rel [hx]

3.4.5. Exercises

  1. Let n be an integer for which n1mod3. Show that n3+7n2mod3.

    example {n : } (hn : n  1 [ZMOD 3]) : n ^ 3 + 7 * n  2 [ZMOD 3] :=
      sorry
    
  2. Let a be an integer for which a3mod4. Show that a3+4a2+21mod4.

    example {a : } (ha : a  3 [ZMOD 4]) :
        a ^ 3 + 4 * a ^ 2 + 2  1 [ZMOD 4] :=
      sorry
    
  3. Let a and b be integers. Show that (a+b)3a3+b3mod3.

    example (a b : ) : (a + b) ^ 3  a ^ 3 + b ^ 3 [ZMOD 3] :=
      sorry
    
  4. Show that there exists an integer a, such that 4a1mod7.

    example :  a : , 4 * a  1 [ZMOD 7] := by
      sorry
    
  5. Show that there exists an integer k, such that 5k6mod8.

    example :  k : , 5 * k  6 [ZMOD 8] := by
      sorry
    
  6. Let n be an integer. Show that 5n2+3n+71mod2.

    example (n : ) : 5 * n ^ 2 + 3 * n + 7  1 [ZMOD 2] := by
      sorry
    
  7. Let x be an integer. Show that x5xmod5.

    example {x : } : x ^ 5  x [ZMOD 5] := by
      sorry
    

3.5. Bézout’s identity

3.5.1. Example

Problem

Let n be an integer and suppose that 5n is a multiple of 8. Show that n is also a multiple of 8.

Solution

Since 85n, there exists an integer a such that 5n=8a. Then

n=3(5n)+16n=3(8a)+16n=8(3a+2n),

so 8n.

example {n : } (hn : 8  5 * n) : 8  n := by
  obtain a, ha := hn
  use -3 * a + 2 * n
  calc
    n = -3 * (5 * n) + 16 * n := by ring
    _ = -3 * (8 * a) + 16 * n := by rw [ha]
    _ = 8 * (-3 * a + 2 * n) := by ring

Such a problem will typically have many possible solutions. Here is another solution.

Solution

Since 85n, there exists an integer a such that 5n=8a. Then

n=5(5n)24n=5(8a)24n=8(5a3n),

so 8n.

Try typing the variant solution up in Lean.

example {n : } (hn : 8  5 * n) : 8  n := by
  sorry

3.5.2. Example

Problem

Show that if for some integer n we have that 5 divides 3n, then 5 also divides n.

Solution

Since 53n, there exists an integer x such that 3n=5x. Then

n=2(3n)5n=2(5x)5n=5(2xn),

so 5n.

example {n : } (h1 : 5  3 * n) : 5  n := by
  sorry

3.5.3. Example

Problem

Let m be an integer which is divisible by 8 and by 5. Show that it is also divisible by 40.

Solution

Since 8m, there exists an integer a such that m=8a. Since 5m, there exists an integer b such that m=5b. Then

m=15m+16m=15(8a)+16m=15(8a)+16(5b)=40(3a+2b),

so 40m.

example {m : } (h1 : 8  m) (h2 : 5  m) : 40  m := by
  obtain a, ha := h1
  obtain b, hb := h2
  use -3 * a + 2 * b
  calc
    m = -15 * m + 16 * m := by ring
    _ = -15 * (8 * a) + 16 * m := by rw [ha]
    _ = -15 * (8 * a) + 16 * (5 * b) := by rw [hb]
    _ = 40 * (-3 * a + 2 * b) := by ring

3.5.4. Exercises

  1. Show that if 6 divides 11n, then it divides n.

    example {n : } (hn : 6  11 * n) : 6  n := by
      sorry
    
  2. Let a be an integer and suppose that 5a is a multiple of 7. Show that a is also a multiple of 7.

    example {a : } (ha : 7  5 * a) : 7  a := by
      sorry
    
  3. Suppose that 7 and 9 are both factors of some integer n. Show that 63 is also a factor of n.

    example {n : } (h1 : 7  n) (h2 : 9  n) : 63  n := by
      sorry
    
  4. Let n be an integer which is divisible by 5 and by 13. Show that it is also divisible by 65.

    example {n : } (h1 : 5  n) (h2 : 13  n) : 65  n := by
      sorry