Modular Arithmetic

(合同算術)

Discrete Mathematics I

13th lecture, January 10, 2025

https://www.sw.it.aoyama.ac.jp/2024/Math1/lecture13.html

Martin J. Dürst

AGU

© 2005-25 Martin J. Dürst Aoyama Gakuin University

 

Today's Schedule

 

Remaining Schedule

 

How to use Past Exams

Important: Do not browse the problems and solutions!

  1. Take time for a full exam (85 minutes)
  2. Print out one exam
  3. Solve this exam, with time limit
  4. Look at the example solutions to check your solutions
  5. Find out your weak points
  6. Learn more in the areas you have identified as your weak points
  7. Repeat starting at 1.

 

Important Points about Final Exam

 

Questions about Final Exam

 

Homework

Draw the Hasse diagram of a Boolean algebra of dimension 4 (16 elements). There will be a deduction if you use the same elements as another student.

Solution: For an example, see handout of last lecture

Caution:
A Hasse diagram does not have any horizontal lines!
A drawing of a four-dimensional cube is not a Hasse diagram if it has horizontal lines.

 

Summary of Last Lecture

 

Summary of Algebraic Structures

(hierarchy of objects)

 

Applications of Bitwise Operations

Operations work on 8, 16, 32, 64 or more bits concurrently

 

Other Bitwise Operations: Left Shift

 

Right Shift

 

More Applications of Bitwise Operations

For many more advanced examples, see Hacker's Delight, Henry S. Warren, Jr., Addison-Wesley, 2003

 

Modular Arithmetic

 

Congruence Relation

 

Properties of Congruence Equations

 

Properties of the Modulo Operation

Reason: a ≡(mod n) a mod n, and so on

Where to use: a and c may be very large numbers, but a mod n and c mod n may be much smaller, so calculation becomes easier.

 

Modulo Operation for Negative Operands

See English Wikipedia article on Modulo Operation

 

An Example of Using the Modulo Operation

Output some data, three items on a line.

A simple way:

int items = 0;
for (i=0; i<count; i++) {
    /* print out data[i] */
    items++;
    if (items==3) {
        printf("\n");
        items = 0;
    }
}

Using the modulo operation:

for (i=0; i<count; i++) {
    /* print out data[i] */
    if (i%3 == 2)
        printf("\n");
}

 

Congruence and Groups

 

Inverse and Division

Condition for inverse (b-1) of b: b•b-1 = b-1•b = neutral element

Inverse for multiplication (multiplicative inverse): b b-1 = b-1 b = 1

Definition of division: a/b = a·1/b = a b-1

Inverse for modular arithmetic (modular multiplicative inverse): b b-1 ≡ b-1 b ≡1 (mod n)

 

Modular Multiplicative Inverse

n b 0 1 2 3 4 5 6 7 8
2 - 1
3 - 1 2
4 - 1 - 3
5 - 1 3 2 4
6 - 1 - - - 5
7 - 1 4 5 2 3 6
8 - 1 - 3 - 5 - 7
9 - 1 5 - 7 2 - 4 8

The modular multiplicative inverse is only defined if n and b are coprime (⇔ GCD(n, b) = 1)

Various methods to calculate, very time-consuming

 

Modular Division

In general: a/b is defined as = a b-1

For modular arithmetic: a/b (mod n) is defined as a b-1 (mod n)

Example:
3/4 (mod 7) = 3 · 4-1 (mod 7) = 3 · 2 (mod 7) = 6
(Check: 6 · 4 (mod 7) = 24 (mod 7) = 3)

 

Application of Congruence: Simple Calculation of Remainder

Example: 216 mod 29 = ?

216 = 25 · 25 · 25 · 2

216 = 25 · 25 · 25 · 2 = 32 · 32 · 32 · 2 ≡(mod 29) 3 · 3 · 3 · 2 = 54 ≡(mod 29) 25

Alternative: 3 · 3 · 3 · 2 = 27 · 2 ≡(mod 29) -2 · 2 = -4 ≡(mod 29) 25

 

Remainder Calculation: More Examples

318 mod 7 = ?

318 = (33)6 = 276 ≡(mod 7) (-1)6 = 1

4110 mod 37 = ?

4110 ≡(mod 37) 410 = 220 = 324 ≡(mod 37) (-5)4 = 252 ≡(mod 37) (-12)2 = (6 · 2)2 = 36 · 4 ≡(mod 37) (-1) · 4 = -4 ≡(mod 37) 33

 

Digit Sum and Digital Root

Digit sum: Sum of all of the digits of a number

Digital root: Single-digit result of repeatedly calculating the digit sum (fixpoint)

Example in base 10:
The digit sum of 1839275 is 1+8+3+9+2+7+5 = 35
The digit sum of 35 is 3+5 = 8
The digital root of 1839275 is 8

Example in base 16:
The digit sum of A8FB is A+8+F+B (10+8+15+11) = (44) 2C
The digit sum of 2C is 2+C (2+12) = (14) E
The digital root of A8FB is E

 

Application of Congruence: Casting out Nines

 

Homework

(no need to submit)

 

Glossary

rotation
回転
reflection
反射
hierarchy
階層
concurrent
同時
shift
シフト
invert
逆転、反転
modular arithmetic
合同算術
congruence (equation)
合同式
modulus
法
residue
剰余
modulo operation
剰余演算
congruence relation
合同関係
congruence class
合同類
residue class
剰余類
cyclic group
巡回群
modular multiplicative inverse
モジュラー逆数
coprime
互いに素
operator
演算子
dividend
被除数
divisor
除数
digit
数字、桁
digit sum
数字和
digital root
数字根
casting out nines
九去法
implementation
実装
calculator
電卓