Modular Arithmetic ICS 6 D Sandy Irani DIV

  • Slides: 14
Download presentation
Modular Arithmetic ICS 6 D Sandy Irani

Modular Arithmetic ICS 6 D Sandy Irani

DIV and MOD functions • d an integer d ≥ 1 • n an

DIV and MOD functions • d an integer d ≥ 1 • n an integer – There are unique integers • q for “quotient” • r for “remainder” – Such that • r ∈ {0, 1, 2, …, d-1} • n = d·q + r

DIV and MOD functions for n < 0 • q = floor(n/d) • r

DIV and MOD functions for n < 0 • q = floor(n/d) • r = n - q·d Example: n = -25, d = 6

DIV and MOD functions for n < 0 • q = floor(n/d) • r

DIV and MOD functions for n < 0 • q = floor(n/d) • r = n - q·d Example n = -75, d = 12

DIV and MOD functions for n < 0 • r=n • q=0 • while

DIV and MOD functions for n < 0 • r=n • q=0 • while (r < 0) – r=r+d – q=q-1 Example n = -25, d = 6

DIV and MOD functions for n < 0 • r=n • q=0 • while

DIV and MOD functions for n < 0 • r=n • q=0 • while (r < 0) r=r+d q = q -1 Example n = -75, d = 12

Modular Arithmetic • “Mod n” is a function from ℤ to {0, 1, …,

Modular Arithmetic • “Mod n” is a function from ℤ to {0, 1, …, n-1} • Addition mod n: (x + y) mod n Multiplication mod n: xy mod n

Modular Arithmetic • In computing arithmetic expressions mod n, can compute partial results mod

Modular Arithmetic • In computing arithmetic expressions mod n, can compute partial results mod n and the result is the same: – ((x mod n) + (y mod n)) mod n = (x + y) mod n – (158 + 219) mod 5 = – ((x mod n) · (y mod n)) mod n = (x · y) mod n – (158 · 219) mod 5 =

Modular Arithmetic • (3474 + 120) mod 11 (56· 72 + 62) mod 7

Modular Arithmetic • (3474 + 120) mod 11 (56· 72 + 62) mod 7

Modular Arithmetic • 210 mod 7 = (25 mod 7) mod 7

Modular Arithmetic • 210 mod 7 = (25 mod 7) mod 7

Modular Arithmetic • Any multiple of n acts like 0 mod n: – (1235

Modular Arithmetic • Any multiple of n acts like 0 mod n: – (1235 · 170 + 2) mod 17 – (8 + 170 · 98) mod 17 =

Rings – A ring is a closed mathematical system with addition and multiplication operations

Rings – A ring is a closed mathematical system with addition and multiplication operations that – Obeys certain laws (associative, distributive, etc. ) – Has identities: • 0+x=x • 1·x = x • The elements of a ring can be different kinds of objects: – Polynomials, sequences, numbers, etc.

The ring ℤn • The ring ℤn is the set {0, 1, 2, …,

The ring ℤn • The ring ℤn is the set {0, 1, 2, …, n-1} along with – Addition mod n – Multiplication mod n • Example: ℤ 5 x 0 1 2 3 4 0 1 2 + 3 4 0 1 2 3 4

Equivalence mod n x mod n = y mod n ↔ (x-y) = integer

Equivalence mod n x mod n = y mod n ↔ (x-y) = integer multiple of n ↔ x ≡ y mod n ↔ “x is equivalent to y mod n” Example: n = 5 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9