Professional Documents
Culture Documents
01-Teoribilangan Slides
01-Teoribilangan Slides
01-Teoribilangan Slides
Adila A. Krisnadhi
Fakultas Ilmu Komputer, Universitas Indonesia
Integer Representations
Primes
Linear Congruences
2/71
Agenda
Integer Representations
Primes
Linear Congruences
3/71
Introduction
4/71
Notation
5/71
Discussion
6/71
Divisibility
Definition 1
Let a and b be two integers with a 6= 0.
We say that a divides b iff there exists an integer c such that b = ac. That is, a
divides b iff ab ∈ Z.
a | b denotes “a divides b”. Also, a6 | b denotes “a does not divide b”.
If a | b, then a is called a factor or divisor of b, and b is called a multiple of a.
7/71
Divsibility Examples
• Is 7 | 13?
• Is 3 | 12?
• If n and d are positive integers, how many positive integers are there that is
no greater than n and divisible by d?
8/71
Discussion
Can we generalize anything from the following facts?
• 13 | 65, 13 | 221, and 13 | 286
• 8 | −24, 8 | 32, and 8 | 56
• 11 | 44. So, 11 | −88, 11 | 88, 11 | 176, etc.
• 7 | 35, 35 | 245, and 7 | 245.
• 6 | 18, 6 | 24, and 6 | 102.
9/71
Theorem 2
Let a, b, c be integers with a 6= 0. Then,
(i) if a | b and a | c, then a | (b + c);
(iv) (generalization of part (i)) if a | b and a | c, then a | (mb + nc) for any two
integers m dan n.
Proof?
10/71
Discussion
11/71
Division algorithm, quotient, and remainder
The following theorem is often called division algorithm despite not exactly an
algorithm.
Theorem 3 (The Division Algorithm)
Let a, d be integers with d 6= 0. Then, there exists two unique integers q and r
with 0 ≤ r < d such that a = dq + r
In the above theorem,
• the integer q is called the quotient and written q = a div d
• the integer r is called remainder and written r = a mod d.
Note that r is never negative.
12/71
Divison algorithm: Examples
13/71
Divisibility and mod operations
What is the relation between divisibility and mod?
14/71
Divisibility and mod operations
What is the relation between divisibility and mod?
Theorem 4
Let a and b be integers with a 6= 0. Then, a | b if and only if b mod a = 0.
Proof?
14/71
Modular congruences
15/71
Modular congruences
Definition 5
Let a, b, m be integers with m positive. Then, a ≡ b (mod m) iff m | (a − b).
15/71
Relationship between mod dan mod
16/71
Relationship between mod dan mod
Theorem 6
Let a, b be integers and m a positive integer.
Then, a ≡ b (mod m) if and only if a mod m = b mod m.
That is, a ≡ b (mod m) if and only if a dan b have the same remainder when
divided by m.
Proof?
17/71
Modular congruence and division algorithm
18/71
Modular congruence and division algorithm
Theorem 7
Let m be a positive integer. Then, a ≡ b (mod m) if and only if there exists an
integer k such that a = b + km.
Proof?
19/71
Congruence classes
If we take 5 as the modulus,
• how many integers are congruent to 7? What are they?
• what integers are congruent to 6?
20/71
Congruence classes
If we take 5 as the modulus,
• how many integers are congruent to 7? What are they?
• what integers are congruent to 6?
Definition 8
Let a be an integer and m a positive integer. The congruence class of a
modulo m, written [a]m , is the set of all integers congruent to a modulo m.
20/71
Modular addition and multiplication
Theorem 9
Let m be a positive integer.
Then, whenever a ≡ b (mod m) and c ≡ d (mod m) hold, then the following
also hold:
• a + c ≡ b + d (mod m), and
• ac ≡ bd (mod m).
Proof?
Give an application example of the above theorem!
21/71
Remarks
22/71
Remarks
22/71
Remarks
22/71
Modulo addition and multiplication
Theorem 10
Let m be a positive integer and a, b integers. Then,
• (a + b) mod m = ((a mod m) + (b mod m)) mod m
• ab mod m = ((a mod m)(b mod m)) mod m.
Proof?
23/71
Example
24/71
Agenda
Integer Representations
Primes
Linear Congruences
25/71
Integer representation: Overview
26/71
• Write the binary representation of 21
• Write the decimal representation (326)8
27/71
Integer representation
Theorem 11
Given an integer b > 1 as base, every positive integer n can be expressed
uniquely in the following form:
n = ak bk + ak−1 bk−1 + . . . a1 b + a0
28/71
Decimal to non-decimal conversion
29/71
Example
30/71
Example
54321 = 8 · 6790 + 1
30/71
Example
54321 = 8 · 6790 + 1
6790 = 8 · 848 + 6
30/71
Example
54321 = 8 · 6790 + 1
6790 = 8 · 848 + 6
848 = 8 · 106 + 0
30/71
Example
54321 = 8 · 6790 + 1
6790 = 8 · 848 + 6
848 = 8 · 106 + 0
106 = 8 · 13 + 2
30/71
Example
54321 = 8 · 6790 + 1
6790 = 8 · 848 + 6
848 = 8 · 106 + 0
106 = 8 · 13 + 2
13 = 8 · 1 + 5
30/71
Example
54321 = 8 · 6790 + 1
6790 = 8 · 848 + 6
848 = 8 · 106 + 0
106 = 8 · 13 + 2
13 = 8 · 1 + 5
1=8·0+1
30/71
Example
54321 = 8 · 6790 + 1
6790 = 8 · 848 + 6
848 = 8 · 106 + 0
106 = 8 · 13 + 2
13 = 8 · 1 + 5
1=8·0+1
30/71
Example
31/71
Example
331771 = 16 · 20735 + 11
31/71
Example
331771 = 16 · 20735 + 11
20735 = 16 · 1295 + 15
31/71
Example
331771 = 16 · 20735 + 11
20735 = 16 · 1295 + 15
1295 = 16 · 80 + 15
31/71
Example
331771 = 16 · 20735 + 11
20735 = 16 · 1295 + 15
1295 = 16 · 80 + 15
80 = 16 · 5 + 0
31/71
Example
331771 = 16 · 20735 + 11
20735 = 16 · 1295 + 15
1295 = 16 · 80 + 15
80 = 16 · 5 + 0
5 = 16 · 0 + 5
31/71
Example
331771 = 16 · 20735 + 11
20735 = 16 · 1295 + 15
1295 = 16 · 80 + 15
80 = 16 · 5 + 0
5 = 16 · 0 + 5
The result is (50FFB)16 . Here, B dan F are the hexadecimal digit for 11 and 15,
resp.
31/71
Why does our base conversion work?
32/71
Conversion between binary, octal, and hexadecimal expansion
• Converting between two non-decimal expansion b1 dan b2
1 convert base-b1 expansion to a decimal expansion (Theorem 11);
2 convert the result into a base-b2 expansion (Algorithm 1).
• Rapid conversion between binary, octal and hexadecimal:
• 3 binary digits for 1 octal digit, and 4 binary digits for 1 hexadecimal
• proceed from right
• Floor function:
bxc = the largest integer less than or equal to x.
• Ceiling function:
dxe = the smallest integer greater than or equal to x.
Theorem 12
For integers a, d with d > 1,
• a div d = ad
• a mod d = a − d ad
Proof?
34/71
Modular exponentiation
In cryptography applications, we often need to calculate bn mod m rapidlly
without calculating bn first, for example, 3644 mod 645
Main idea:
• By Theorem 11, n can be written in binary as (ak−1 . . . a1 a0 )2 :
n = ak−1 · 2k−1 + · · · + a1 · 2 + a0
where a0 , . . . , ak−1 are either 0 atau 1.
• So, bn = bak−1 2k−1 +···+a1 ·2+a0 = bak−1 ·2k−1 · · · ba1 ·2 ba0
• If ai = 0 for some i, then bai ·2i = b0 = 1. So, it suffices to consider bai ·2i for
which ai 6= 0 in the above product. For example, for the case of 311 , we
note that 11 = (1011)2 = 1 · 23 + 0 · 22 + 1 · 21 + 1 · 20 = 8 + 2 + 1. Hence,
we only need to consider 23 , 21 , 20 .
• We perform exponentiation and multiplication while doing modulo operation
every time exponentiation and multiplication is done.
35/71
Modular exponentiation algorithm
36/71
Modular exponentiation example
Calculate 3644 mod 645.
37/71
Agenda
Integer Representations
Primes
Linear Congruences
38/71
Primes
Definition 13
An integer p > 1 is called a prime iff p has exactly two positive factors, namely 1
and p.
An integer n > 1 that is not a prime is called a composite. So, n is a composite
iff there exists an integer a with 1 < a < n such that a | n.
• The integer 1 is not a prime since it only has one positive factor.
39/71
Fundamental theorem of arithmetics
Why are primes important?
40/71
Fundamental theorem of arithmetics
Why are primes important?
Theorem 14 (Fundamental theorem of arithmetics)
Every integer n > 1 can be written uniquely as:
• a (single) prime; or
• a product of two or more primes (with duplicates allowed) such that those
prime factors are written in an increasing order.
Theorem 15
√
If n is a composite, then n has a prime factor that is less than or equal to n.
Proof?
41/71
Determining if n is prime
Trial division
Theorem 15
√
If n is a composite, then n has a prime factor that is less than or equal to n.
Proof?
• To determine if n is prime, it suffices to divide n with all primes less than or
√
equal to n.
• If any of those primes divides n, then n is composite. Otherwise, n is prime.
• Example: Is 101 prime?
41/71
Sieve of Eratosthenes
Finding all primes that are less than or equal to a given positive integer n.
• List all integers from 2 to n.
• Cross out all multiples of 2 that is greater than 2.
• From the remaining numbers, the smallest and not crossed out is 3. So,
cross out all multiples of 3 that is greater than 3.
• From the rest, the smallest and not crossed out is 5. So, cross out all
multiples of 5 that is greater than 5.
• From the rest, the smallest and not crossed out is 7. So, cross out all
multiples of 7 that is greater than 7.
• From the rest, the smallest and not crossed out is 11. So, cross out all
multiples of 11 that is greater than 11.
• and so forth ..
42/71
Find all primes not exceeding 100.
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
43/71
How many primes are there?
44/71
How many primes are there?
Theorem 16 (from Euclid)
There are infinitely many primes.
Proof:
44/71
Agenda
Integer Representations
Primes
Linear Congruences
45/71
Motivating question
Jakarta’s recent flood damaged Dobbie’s room quite significantly. Before Dobbie
can use his room again, he has to conduct some renovation work, which includes
installing new tiles to the floor.
The room’s shape is rectangular measuring 420 cm × 364 cm. Dobbie only
wants square tiles and moreover, he wants as few tiles as possible such that the
whole floor is perfectly covered only by square tiles, i.e., no non-square tile has to
be used. Fortunately, one of Dobbie’s business partners is a tile supplier that can
provide him with square tiles of any size.
What is the minimum number of tiles do Dobbie actually use?
46/71
Greatest common divisor (GCD)
Definition 17 (GCD)
Let a, b be integers, not both zero. The greatest common divisor (GCD) of a
and b, denoted gcd(a, b), is the largest integer that divides both a and b, i.e.,
the largest integer d such that d | a and d | b.
• gcd(36, 48) =
• gcd(25, 21) =
47/71
GCD and prime factorization
48/71
GCD and prime factorization
Is there a relation between GCD and prime factorization? Write the prime
factorization of:
• 36, 48, and gcd(36, 48)
• 25, 21, and gcd(25, 21)
• 120, 500, and gcd(120, 500).
Can you spot the pattern?
48/71
Let a, b be integers with a ≤ b. Also, let p1 , p2 , . . . , pn be primes such that for
every pi , pi | a or pi | b. Then, prime factorizations of a and b can be written:
49/71
Relatively prime
Definition 19
Two integers a and b are called coprime or relatively prime iff gcd(a, b) = 1.
n integers a1 , a2 , . . . , an are pairwise coprime or pairwise relatively prime iff
gcd(ai , aj ) = 1 for 1 ≤ i < j ≤ n.
That is, two integers are coprime if their only common positive factor is 1.
• Are 10 and 21 relatively prime?
• Are 44, 50, and 63 pairwise relatively prime?
• Are 21, 25, and 56 pairwise relatively prime?
50/71
Least common multiple (LCM)
Definition 20
Least common multiple of a and b, denoted lcm(a, b), is the smallest positive
integer that is divisible by both a and b.
51/71
LCM and prime factorization
52/71
LCM and prime factorization
52/71
Theorem 21
If a, b are integers with prime factorizations according to Theorem 18, then
max(a1 ,b1 ) max(a2 ,b2 )
lcm(a, b) = p1 p2 · · · pmax(a
n
n ,bn )
53/71
Relationship between GCD and LCM
Is there a relation between GCD and LCM of two integers?
54/71
Relationship between GCD and LCM
Is there a relation between GCD and LCM of two integers?
Guess the relation by first calculating the following integers:
• 36, 48, gcd(36, 48), lcm(36, 48)
• 25, 21, gcd(25, 21), lcm(25, 21)
• 120, 500, gcd(120, 500), lcm(125, 500)
54/71
Theorem 22
Let a, b be positive integers. Then, ab = gcd(a, b) · lcm(a, b)
• gcd(95256, 432) =
• lcm(36, 48) =
55/71
Calculating GCD
• Based on what we’ve seen so far, how do we calculate the GCD of two
integers?
56/71
Calculating GCD
• Based on what we’ve seen so far, how do we calculate the GCD of two
integers?
• Prime factorization
56/71
Calculating GCD
• Based on what we’ve seen so far, how do we calculate the GCD of two
integers?
• Prime factorization
• Is the above approach efficient?
56/71
Calculating GCD
• Based on what we’ve seen so far, how do we calculate the GCD of two
integers?
• Prime factorization
• Is the above approach efficient?
• In current practice, no.
• In theory, No polynomial-time algorithm for prime factorization of all
integers has been found so far. Prime factorization is in class NP, but has
not been shown to be NP-complete. It is suspected that the problem is
neither in class P nor it is NP-complete.
• Note: In 1994, Peter Shor found an algorithm for prime factorization that
could be run in polynomial time on quantum computers.
56/71
Calculating GCD
• Based on what we’ve seen so far, how do we calculate the GCD of two
integers?
• Prime factorization
• Is the above approach efficient?
• In current practice, no.
• In theory, No polynomial-time algorithm for prime factorization of all
integers has been found so far. Prime factorization is in class NP, but has
not been shown to be NP-complete. It is suspected that the problem is
neither in class P nor it is NP-complete.
• Note: In 1994, Peter Shor found an algorithm for prime factorization that
could be run in polynomial time on quantum computers.
• Is there an efficient way to calculate GCD?
56/71
Calculating GCD
• Based on what we’ve seen so far, how do we calculate the GCD of two
integers?
• Prime factorization
• Is the above approach efficient?
• In current practice, no.
• In theory, No polynomial-time algorithm for prime factorization of all
integers has been found so far. Prime factorization is in class NP, but has
not been shown to be NP-complete. It is suspected that the problem is
neither in class P nor it is NP-complete.
• Note: In 1994, Peter Shor found an algorithm for prime factorization that
could be run in polynomial time on quantum computers.
• Is there an efficient way to calculate GCD?
• Yes: the Euclidean algorithm.
56/71
Euclidean algorithm: the key idea
Let’s compute gcd(21, 78). For this, consider the integers 78, 21, 15, 6, 3, 0.
• Calculate the GCD of every two consecutive integers using any means you
know. Can you spot the pattern?
• Generalize it so that we consider gcd(a, b) for any integers a, b.
57/71
Theorem 23 (Core of Euclidean algorithm)
Let a, b be integers. Then, gcd(a, b) = gcd(b, a mod b)
• gcd(21, 78) =
• gcd(25, 21) =
58/71
Algorithm 3 (Euclidean algorithm to calculate gcd(a, b))
Input: a, b positive integers
x := a
y := b
while y 6= 0:
r := x mod y
x := y
y := r
return x x is gcd(a, b)
59/71
Euclidean algorithm using tabulation
60/71
Calculate gcd(662, 414) using tabulation: iterate on j = 0, 1, . . . with r0 = a,
r1 = b, rj = rj+1 qj+1 + rj+2 for j, and terminate at j = n when rj+2 = 0 dan
rj+1 = gcd(a, b)
61/71
gcd(a, b) as linear combination of a and b
62/71
Theorem 24 (Bezout’s Theorem)
Let a, b be integers. Then, there exists integers s and t such that
gcd(a, b) = sa + tb.
63/71
Example
Express gcd(252, 198) as a linear combination of 252 and 198.
64/71
Agenda
Integer Representations
Primes
Linear Congruences
65/71
Linear congruence
Modular congruence can be generalized into linear congruence of the form
ax ≡ b (mod m)
Definition 25
Let a, m be integers with m positive. The integer ā sastisfying āa ≡ 1 (mod m)
is called inverse of a modulo m.
67/71
When is a modular inverse guaranteed to exist?
Theorem 26
If a and m are relatively prime with m > 1, then a modular inverse of a (modulo
m) always exists. Furthermore, it is unique modular m, i.e., every other inverse
of a modulo m is congruent to it.
If gcd(a, m) = 1, inverse of a modulo m can be calculated using Bezout’s
theorem.
68/71
Examples
Calculate inverse of 4 modulo 7 and of 101 modulo 4620.
69/71
Solving linear congruences with modular inverse
70/71
Example
71/71