Skip to main content

Modular Arithmetic

Modular arithmetic is a system of arithmetic for integers, which considers the remainder.

In modular arithmetic, numbers "wrap around" upon reaching a given fixed quantity (this given quantity is known as the modulus) to leave a remainder.

Parity

Parity refers to the state of being even or odd.

  • Integers have one of two different parities:
    • xx is even means 2x2|x
    • xx is odd means 2(x1)2|(x-1)
  • Some properties:
    • even ±\pm even = even
    • even ±\pm odd = odd
    • odd ±\pm odd = even

Clock Arithmetic

Time wraps around at 12. In addition, adding any multiple of 12 hours does not change the o'clock.

example 1: now is 10 o'clock. What time is it after 5 hours?

3 o'clock

example 2: what time is it after 27 hours from now? What time is it after 39 hours from now?

So a and b are the same o'clock if 12 divides a - b or (a - b) % 12 == 0

const a = 15;
const b = 15 + 12 * 2; // 39
const result = (a - b) % 12;
console.log(result); // 0

Modular Arithmetic

Modular arithmetic is an abstraction of parity and clock arithmetic.

  • Parity is arithmetic modulo 2
  • Clocks use arithmetic modulo 12

Modular Equivalence

Modular equivalence is also called modular congruence.

Definition: n(ab)n | (a - b), if aba - b is divisible by nn. Then aa and bb are equivalent modulo n.

n(ab)    ab (mod n)n | (a - b) \implies a \equiv b \ (\textrm{mod}\ n)

Example:

a=50b=20n=10abn=3010=30ab (mod n)\begin{aligned} a & = 50 \\ b & = 20 \\ n & = 10 \\ \\ \frac{a-b}{n} & = \frac{30}{10} = 3 \dots 0 \\ a & \equiv b \ (\textrm{mod}\ n) \end{aligned}

Properties of Modular Arithmetic

Modular equivalence plays nicely with ["addition", "subtraction", "multiplication"].

Suppose that a1b1 (mod n)a_1 \equiv b_1 \ (\textrm{mod}\ n) and a2b2 (mod n)a_2 \equiv b_2 \ (\textrm{mod}\ n). Then:

  • a1+a2b1+b2 (mod n)a_1 + a_2 \equiv b_1 + b_2 \ (\textrm{mod}\ n)
  • a1a2b1b2 (mod n)a_1 - a_2 \equiv b_1 - b_2 \ (\textrm{mod}\ n)
  • a1×a2b1×b2 (mod n)a_1 \times a_2 \equiv b_1 \times b_2 \ (\textrm{mod}\ n)

This means we can use many tools from normal arithmetic with modular arithmetic.

mod Operator

Definition: a mod na\ \textrm{mod}\ n is the smallest non-negative integer cc such that ac (mod n)a \equiv c\ (\textrm{mod}\ n).

Equivalently, a mod na\ \textrm{mod}\ n is the remainder when you divide aa by nn (an\frac{a}{n}).

Example: 11 mod 2=111\ \textrm{mod}\ 2 = 1, because 11=(5×2)+111 = (5 \times 2) + 1

Proof of Lemma

A lemma is little statement that is true in a mathematical theory, derived from its axioms. We can show that it is true by using a proof which shows the steps necessary to get from the axioms to the statement. We can also use other lemmas in the proof if we have already proved them.

Lemma: Let aa and bb be integers. a mod b=0    baa\ \textrm{mod}\ b = 0 \implies b|a.

Proof:

  1. According to the definition of mod operator, a0 (mod b)a \equiv 0 \ (\textrm{mod}\ b).
  2. Use modular equivalence definition, b(a0)b | (a - 0)
  3. Based on a well known lemma, we known that a0=aa - 0 = a, so bab | a

Applications of Modular Arithmetic

Appendix

Code

modular equivalence

Vocabulary

Cannot find definitions for "congruence".

Cannot find definitions for "modulo".

Cannot find definitions for "axioms".