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

© 2005-25 Martin
J. Dürst Aoyama Gakuin
University
Today's Schedule
- Remaining schedule
- Homework and summary of last lecture
- Applications of bitwise operations
- Modular arithmetic
- Properties of the modulo operation
- Congruence and division
- Digit sum and digital root
Remaining Schedule
- January 10: 13th lecture
- January 17: 14th lecture (makeup lecture/補講, same
time, same place)
Important: Contents of makeup class is part of final
exam!
If you have another makeup lecture at the same time, please tell me as soon
as possible.
- January 24: 15th lecture
- January 31, 11:10-12:35 (85 min): Term Final Exams
How to use Past Exams
Important: Do not browse the problems and solutions!
- Take time for a full exam (85 minutes)
- Print out one exam
- Solve this exam, with time limit
- Look at the example solutions to check your solutions
- Find out your weak points
- Learn more in the areas you have identified as your weak points
- Repeat starting at 1.
Important Points about Final Exam
- Most problems are in Japanese and English
- Most answers can be in Japanese or English
- Read problems carefully (distinguish between calculation, proof,
explanation,...)
- Be able to explain concepts in your own words
- Be able to do calculations (base conversions, truth tables,...)
speedily
- Combine and apply knowledge from
different lectures
- Write clearly
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
- Boolean algebra is (an example of) an algebraic structure
- Boolean algebras have one set (B), two special elements
(0 and 1), and three operations (⫬,
△, and ▽)
- The axioms of boolean algebra are the same as the axioms of basic logic;
there are various choices with tradeoffs between obviousness and
compactness
- Boolean algebras are half-orders and lattices
- There are many examples of Boolean algebras: basic logic (with logic
operations), bit strings (with bitwise logic operations), powersets (with
set operations), certain sets of integers (with divisibility), gardens with
flowers,...
- All finite Boolean algebras are of size 2n, and are
isomorphic to an n-dimensional cube
Summary of Algebraic Structures
(hierarchy of objects)
Applications of Bitwise Operations
- Representation of sets and operations on sets
(finite universal set, each bit position represents one element in the
universal set)
- Storage and check of (binary) properties
(finite set of properties, each bit position represents one specific
property)
(examples: CPU
features, character
conversion UPPER↔lower case)
- Detailled operations on data
(example: data transformations, data compression)
Operations work on 8, 16, 32, 64 or more bits concurrently
Other Bitwise Operations: Left Shift
b << n in C and many other programming languages
- Shifts each of the bits in
b by n positions to
the left (more significant direction)
n bits on the right are set to 0
- The leftmost
n bits in b disappear
- If there is no overflow ('1' bits disappearing on the left),
b << n is equivalent to b
×2n
- Example: '101 1001' << 5 ⇒ '1011 0010 0000' (in actual program:
89 << 5 ⇒ 2848)
Right Shift
b >> n in C and many other programming languages
- Shifts each of the bits in
b by n positions to
the right (less significant direction)
n bits on the left are set to 0 or to the value of the
leftmost bit in b, depending on programming lanugage and data
type
- The rightmost
n bits in b disappear
b >> n is equivalent to b /
2n (integer division)
- Example: '101 1001' >> 3 ⇒ '1011' (in actual program:
89
>> 3 ⇒ 11)
More Applications of Bitwise Operations
- In bitstring a, set bits from mask m:
a |= m
- In bitstring a, clear bits from mask m:
a &= ~m
- In bitstring a, invert bits from mask m:
a ^= m
- In bitstring a, set bit number n:
a |= (1<<n) (rightmost bit is number 0)
- In bitstring a, clear bit number n:
a &= ~(1<<n) (rightmost bit is number 0)
- In bitstring a, invert bit number n:
a ^= (1<<n) (rightmost bit is number 0)
For many more advanced examples, see Hacker's Delight, Henry S. Warren, Jr.,
Addison-Wesley, 2003
Modular Arithmetic
Congruence Relation
- Each modulus n creates a congruence relation on
ℤ
- Congruence relations are equivalence relations
- The equivalence classes are called congruence classes or
residue classes
- The representative k of a congruence class is usually choosen
so that 0 ≦ k <
n
- The representatives are the result of the modulo operation
Properties of Congruence Equations
- a ≡ b ∧ c ≡ d ⇒
a + c ≡ b + d (mod
n)
- a ≡ b ∧ c ≡ d ⇒
a - c ≡ b - d (mod
n)
- a ≡ b ∧ c ≡ d ⇒
ac ≡ bd (mod
n)
- a ≡ b ⇒ am ≡
bm (mod n)
- But: a ≡ b ∧ c ≡ d
⇏ a / c ≡ b /
d (mod n)
Properties of the Modulo Operation
- (a + c) mod n = (a mod
n + c mod n) mod n (addition
modulo n)
- (a - c) mod n = (a mod
n - c mod n) mod n
(substraction modulo n)
- (a · c) mod n = (a mod
n · c mod n) mod n
(multiplication modulo n)
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
- Operator:
% (C, Ruby and many other programming languages),
mod (Mathematics)
- Definitions for negative numbers:
|
remainder for negative operands |
| operands |
always non-negative |
same sign as divisor |
same sign as dividend |
| a (dividend) |
n (divisor) |
q (a/n) |
r (a%n) |
q (a/n) |
r (a%n) |
q (a/n) |
r (a%n) |
| 11 |
7 |
1 |
4 |
1 |
4 |
1 |
4 |
| 11 |
-7 |
-1 |
4 |
-2 |
-3 |
-1 |
4 |
| -11 |
7 |
-2 |
3 |
-2 |
3 |
-1 |
-4 |
| -11 |
-7 |
2 |
3 |
1 |
-4 |
1 |
-4 |
| Programming languages,... |
Pascal, Scheme, Mathematics, this
lecture |
Ruby, Python, Perl, Excel |
C (ISO 1999), Java, JavaScript, PHP |
- Always true: qn+r=a ∧
|r|<|n|
- In some programming languages (e.g. C before ISO 1999), the result is
implementation-defined
- Some programming languages offer more than one function
- a ≡n b ⇔ a mod
n = b mod n only true for "always
non-negative"
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
- The digital root of a number n in base b is equal
to n mod (b-1)
(dr(nb) = n mod
(b-1))
- Reason: For n =
dk...d1d0
= dk·bk+
d (k-1)·b (k-1)+...+d1·b1+d0·b0,
because bm ≡(mod
(b-1)) 1m = 1, n
≡(mod (b-1)) dk+
d(k-1)+...+d1+d0
- If b=10, then b-1=9
- Example in base 10: 183927510 mod 9 = 8
- Example in base 16: A8FB16 mod F = E
- This can be used for cross-checking the result of an arithmetic
operation:
x · y = z ⇒ dr(dr(x) ·
dr(y)) ≡mod (b-1) dr(z)
x · y ≠ z ⇐ dr(dr(x) ·
dr(y)) ≢mod (b-1) dr(z)
Homework
(no need to submit)
- Standing Homework: Prepare for final exam using past
exams.
- Create some addition problems in different bases, and then solve them
(see lecture 2). Check your results using
casting out nines (or b-1). Also, check your results by
converting the numbers to decimal notation.
- Create some subtraction problems in different bases, and figure out how
to do subtraction in bases other than 10. Check your results as above.
- Create some problems for remainder calculation using congruence, and
solve them. Check your results using a calculator.
- Only one of the two equations below is correct. Which one?
1346021 · 6292041 = 8469219318861
3615137 · 8749019 = 31628902349603
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
- 電卓