0% found this document useful (0 votes)
45 views112 pages

Discrete structures 2 lec 1

The document is a lecture outline for a course on Discrete Structures II, covering topics such as divisibility, modular arithmetic, greatest common divisors, primes, and integer representations. It emphasizes the importance of number theory in both pure mathematics and practical applications, particularly in cryptography and computer science. Key concepts include congruence relations, Euclidean division, and properties of arithmetic modulo m.

Uploaded by

terrymutheu57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views112 pages

Discrete structures 2 lec 1

The document is a lecture outline for a course on Discrete Structures II, covering topics such as divisibility, modular arithmetic, greatest common divisors, primes, and integer representations. It emphasizes the importance of number theory in both pure mathematics and practical applications, particularly in cryptography and computer science. Key concepts include congruence relations, Euclidean division, and properties of arithmetic modulo m.

Uploaded by

terrymutheu57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 112

Discrete Structures II

Mwangi H. (Ph.D.)

CS Yr 3.1
Department of Computing
J.K.U.A.T.

February 29, 2024


1 Divisibility and Modular Arithmetic
Divisibility
Division with Remainder
Congruence Relations
2 Greatest Common Divisors and Primes
Prime Numbers
Finding Primes
Computing Primes
Euclidean Algorithm
3 Solving Congruences
Linear Congruences
Systems of Linear Congruences
Exercises
4 Integer Representations
Positional Number System
Converting to radix-r
Binary, Octal, Hex Conversion
Binary Arithmetic
henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 2 / 117
This chapter discusses introduction to number theory, the study of
integers and their properties
Key considerations in number theory are the division and prime
numbers which extends to modular arithmetic, and even cryptography.
Number theory is typically a very “pure” mathematical topic, but it
has many practical applications.
Cryptography is very useful in practice, including internet security,
banking, and more.
Number theory also has many other applications in computer science.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 3 / 117


Divisibility and Modular Arithmetic

In this section we will explore the properties of integers


. . . , −4, −3, −2, −1, 0, 1, 2, 3, 4, . . .
In so-called ring theory the integers are an integral domain. The key
property of integral domain is the cancellation property.
If a ̸= 0 and ab = ac, then b = c
This property suggests a natural setting for the study of a divisibility.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 4 / 117


Divisibility and Modular Arithmetic Divisibility

Definition 1.1

If a and b are integers, with a ̸= 0,we say a divides b if there exists


an integer q such that b = aq. When a divides b we write a|b,
otherwise a ∤ b

From these definitions we get special names for a, b, q. When we


have b = aq
1 a is the divisor or factor of b
2 b is the divided or multiple of a
3 q is the quotient
3|9, 3 ∤ 17, 3|15
The property of divisibility leads to many combinations and later
results. Important Divisibility Theorems.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 5 / 117


Divisibility and Modular Arithmetic Divisibility

Theorem 1.1

let a,b and c be integers with a ̸= 0


a if a|b and a|c then a|(b + c)
b if a|b then a|bc
c if a|b and b|c then a|c

Proof of the first statement


Proof.
Suppose a|b and a|c. Then there exists integers q1 and q2 such that
b = aq1 and c = aq2. Hence, b + c = aq1 + aq2. Therefore
b + c = a(q1 + q2). Since q1 + q2 is an integer, a|(b + c)

Proof the second and third statement of the theorem

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 6 / 117


Divisibility and Modular Arithmetic Division with Remainder

Division with remainder is also called Euclidean Division. It is both an


algorithm and a theorem for computing quotients and remainders
Recall that when a number divides another number perfectly, then we
get a quotient and an equation of the form b = aq.
However, it is often the case that division cannot be performed
exactly. This is the role of the remainder.
Example 3 ∤ 14. Therefore 14 = 3.4 + 2. We say that 3 divides 14,
four times with a remainder of 2

Theorem 1.2: Euclidean Division

Let a,b be integers with a ̸= 0. There exist unique integers q and r


such that b = aq + r , and 0 ≤ r < a

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 7 / 117


Divisibility and Modular Arithmetic Division with Remainder

Proof
Notice that we always re-write a division with remainder relation in terms
of positive integers. Indeed if a < 0 then b = aq + r can be re-written as
b = a′ q ′ + r with a′ = −a and q ′ = −q. The case of b < 0 is similar.
Therefore, we only have to consider the case where a, b, q, r are all
non-negative integers. Now, we prove the existence of quotient and
remainder. We will show q = ⌊ ba ⌋. By definition we have that 0 ≤ r < a
and b = aq + r Therefore

aq ≤ aq + r < a(q + 1)
aq ≤ b < a(q + r )
b
q ≤ <q+1
a
b
q=⌊ ⌋
a

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 8 / 117


Divisibility and Modular Arithmetic Division with Remainder

Proof Cont’d
Since a, b, q, r are integers with a ̸= 0 and 0 ≤ r < b, r can be uniquely be
determined from a, b, and q Next we prove that the quotient and
remainder are unique. Let b = aq1 + r 1 = aq2 + r 2 such that a, b, q, r are
non-negative integers and 0 ≤ r 1 < a and 0 ≤ r 2 < a. Proceed by
contradiction and assume r 1 ̸= r 2. W.L.O.G. assume r 2 > r 1. Then

aq1 − aq2 = r 2 − r 1
a(q1 − q2) = r 2 − r 1

From a(q1 − q2) = r 2 − r 1 we have that a|r 2 − r 1 However, since


0 ≤ r 1 < a and 0 ≤ r 2 < a, and r 2 > r 1 it must be that 0 < (r 2 − r 1) < a

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 9 / 117


Divisibility and Modular Arithmetic Division with Remainder

Proof Cont’d
Yet, the multiples of a are 0, ±a, ±2a, ±3a, . . .. Since r 2 − r 1 < a. it must
be that r 2 − r 1 = 0 and hence r 2 = r 1. A contradiction. Since r 2 = r 1, it
follows that q2 = q1 from a ̸= 0 and

aq1 + r 1 = aq2 + r 2
aq1 = aq2
a(q1 − q2) = 0 =⇒ q1 − q2 = 0

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 10 / 117


Divisibility and Modular Arithmetic Division with Remainder

From Euclidean division, we get two sub-operations: div and mod.


Div refers to the quotient and mod to the remainder
1 b div a = q and
2 b mod a = r

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 11 / 117


Divisibility and Modular Arithmetic Congruence Relations

Congruence Relations or Congruences are special kinds of


equivalences.
Congruence is very similar to modulus

Definition 1.2

Two integers a and b are congruent modulo a positive integer m if


m divides a − b

Congruences are all about remainders. Modulo m, two integers a and


b are congruent if a mod m = b mod m
When two numbers are congruent modulo m, we write a ≡ b mod m
and we say ”a is congruent to b modulo m”. The relation a ≡ b is a
congruence and m is the modulus.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 12 / 117


Divisibility and Modular Arithmetic Congruence Relations

Examples of Modulo 6: 17 ≡ 5mod6


One way to this of modulo is an operation which removes all
multiples of the modulus from an expression. It is a simplification
Consider 26 mod 5.
26 = 5.5 + 1
By Euclidean division, this implies that the remainder of 26 when
divided by 5 is 1. Hence 26 ≡ 1 mod 5

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 13 / 117


Divisibility and Modular Arithmetic Congruence Relations

Theorem 1.3

Let m be a positive integer. The integers a and b are congruent


modulo m if and only if there exists and integer k such that a =
k.m + b

Proof.
If a ≡ b mod m then, by definition m|a − b. Hence, there exist an integer
k such a − b = km. Rearranging, a = b + km. Conversely, if there exists
and integer k such that a = b + km, then we of course have km = a − b
and thus m|a − b and a ≡ b mod m

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 14 / 117


Divisibility and Modular Arithmetic Congruence Relations

mod versus mod

Note that mod and mod are not exactly the same thing. Although highly
related, one is a function a mod b = r is a function taking a and b as
arguments and returning the remainder r of a divided by b while the other
is a binary relation. It defines an equivalence relation.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 15 / 117


Divisibility and Modular Arithmetic Congruence Relations

Congruence, Sums and Products

Congruence relations allow for interesting algebraic manipulation.

Theorem 1.4

Let a, b, c, d be integers, and let m be a positive integer. If a ≡ b


mod m and c ≡ d mod m then a + c ≡ b + d mod m

Proof.
By a previous theorem, we have that the congruences implies existence of
integers k, l and the equations b = a + km and c = d + lm results. From
these equations we have b + d = (a + km) + (c + lm) = (a + c) + (k + l)m.
Therefore b + d ≡ a + c mod m

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 16 / 117


Divisibility and Modular Arithmetic Congruence Relations

Congruence, Sums and Products

More generally, we can perform algebraic manipulations on several


equations while ”working modulo m”.
As a consequence of theorem above, if we multiply both sides of a
congruence by the same integer, the congruence still holds.
For any integer c we have a ≡ b mod m =⇒ c.a ≡ c.b mod m
As a caution, division does not always maintain congruences. Notice
14 ≡ 8 mod 6. However dividing by 2: 14 8
2 ̸≡ 2 mod 6

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 17 / 117


Divisibility and Modular Arithmetic Congruence Relations

Congruence, Sums and Products -Arithmetic Modulo m

Two important properties derived from the above theorems for


modulo m are:
1 (a + b) mod m = ((a mod m) + (b mod m)) mod m
2 (a.b) mod m = ((a mod m).(b mod m)) mod m
These equations have a very important consequence
They mean we can perform either arithmetic first, and then take
remainders or we can take remainders first and then perform
arithmetic
Let Z = 0, 1, 2, ...m − 1 be the set of non-negative integers less that
m. We can define a special kind of addition and multiplication using
the above properties.
These addition and multiplication operations act on elements of Zm
and always return another element of Zm

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 18 / 117


Divisibility and Modular Arithmetic Congruence Relations

Congruence, Sums and Products - Arithmetic Modulo m

(Addition modulo m)Let +m be addition on the set of numbers Zm


defined as a +m b = (a + b)mod m
(Multiplication mudulo m) Let ×m be multiplication on the set of
numbers Zm be defined as a ×m b = (a × b) mod m
+m and ×m rather than the normal addition and subtraction of the
integers is called ”working modulo m” or ”doing arithmetic modulo
m”
Working modulo 11. Let Z11 be the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.
Then
7 +11 9 = (7 + 9) mod 11 = 16 mod 11 = 5
7 ×11 9 = (7 × 9) mod 11 = 63 mod 11 = 8

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 19 / 117


Divisibility and Modular Arithmetic Congruence Relations

Congruence, Sums and Products -Exercises

1 let m = 17 Compute 16 +17 13


2 let m = 103 Compute 45 ×103 77
3 let m = 32 Compute 4 ×32 8
4 let m = 25 Compute −103 +25 13

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 20 / 117


Divisibility and Modular Arithmetic Congruence Relations

Properties of arithmetic modulo m

The operations of sum and product of modulo m are very similar to


the normal addition and multiplication of the integers.
These operations define a commutative ring or the ring theory
Property Description
Closure If a, b ∈ Zm , then a +m b ∈ Zm and a ×m b ∈ Zm
Associativity If a, b , c ∈ Zm , then (a +m b ) +m c = a +m (b +m c ) and (a ×m b ) ×m c = a ×m (b ×m c )
Commutativity If a, b ∈ Zm , then a +m b = b +m a and a ×m b = b ×m a
Identity The elements 0 and 1 are identities of addition and multiplication. a +m 0 = a and a ×m 1 = a
Additive inverses If a ̸= 0 ∈ Zm then m − a is the additive inverse of a such that a +m (m − a) = 0
Distributivity If a, b , c ∈ Zm then a ×m (b +m c ) = (a ×m b ) + (a ×m c )

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 21 / 117


Divisibility and Modular Arithmetic Congruence Relations

Congruences as equivalence relations

Over the integers, congruence modulo m induces an equivalence


relation on Z
As a binary relation on Z, congruence mudulo m is Reflexive,
Symmetric and Transitive
1 Reflexive. For any a ∈ Z, a ≡ a mod m since m|a − a
2 Symmetric. For a, b ∈ Z, a ≡ b mod m implies b ≡ a mod m since
m|a − b also implies m|b − a
3 Transitive. If a, b, c ∈ Z with a ≡ b mod m and b ≡ c mod m then
a ≡ c mod m. Indeed, we have m|(b − a) and m|b − c, thus ∃k, l ∈ Z
such that: b − a = mk and c − b = ml =⇒ c − ml − a = mk,
rearranging c − a = m(l + k). Hence m|c − a and a ≡ c mod m
Since congruence is a an equivalence relation, it also implies the
existence of equivalence classes. These are also called congruence
classes.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 22 / 117


Divisibility and Modular Arithmetic Congruence Relations

Congruences as equivalence relations - Congruence classes

Definition 1.3: Congruence Class

The congruence class modulo m of an integer x is the set of all


integers congruent to x modulo m

Denote congruence classes as x̄. This can also be denoted by typical


equivalence class notation {x}. The set x̄ is x̄ = {a ∈ X|x ≡ a mod
m}
Notice that modulo itself does not define a congruence relation or
congruence classes. A specific modulo need to be worked on.
The set of all congruence classes, and the members of each congruent
class, changes with the choice of modulus

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 23 / 117


Divisibility and Modular Arithmetic Congruence Relations

Congruences as equivalence relations - Congruence classes

m = 5 The set of all equivalence classes modulo m are {0̄, 1̄, 2̄, 3̄, 4̄}
This set is similar to Z5 = {0, 1, 2, 3, 4}. The set of congruence
classes modulo 5 and Z5 are essentially the same. In other words they
are isomorphic

Proposition 1.1: Proposition

An integer is congruent modulo m to its remainder on division by m.


There are m congruence classes modulo m, each corresponding to
the m possible remainders

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 24 / 117


Divisibility and Modular Arithmetic Congruence Relations

Congruences as equivalence relations - Congruence classes

Let m Z represent all integers multiples of m. That is, the set


{· · · , −2m, −m, 0, m, 2m · · · }. Then, the set of congruence classes
modulo m are:

0 =m X
1 =m X + 1
2 =m X + 2
.
.
.
m − 1 =m X + (m + 1)

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 25 / 117


Divisibility and Modular Arithmetic Congruence Relations

Congruence - Exercises
1 Proof the following theorem

Theorem 1.5

let a, b, c, d be integers and m be a positive integer. If a ≡ b


mod m and c ≡ d mod m the ac ≡ bd mod m

2 Prove that an integer is congruent modulo m to its remainders on


division by m
3 Compute the following values
a The quotient of 54 divided by 6
b The remainder of 54 divided by 6
c The Quotient of 1235 divided by 12
d 144 mod 7
e 123 mod 7
f -17 mod 3
g -101 mod 13

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 26 / 117


Divisibility and Modular Arithmetic Congruence Relations

Congruence - Exercise

1 Consider an analog clock which shows the numbers 1 through 12.


What time does it show :
i 48 hours after it shows 5:00?
ii 17 hours after it shows 11:00?
iii 103 hours after it shows 4:00?
2 Find an integer satisfying the following
i x ≡ 43 mod 23 where −22 ≤ x ≤ 0
ii x ≡ 17 mod 29 where −14 ≤ x ≤ 14
iii x ≡ −11 mod 21 where 90 ≤ x ≤ 110

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 27 / 117


Greatest Common Divisors and Primes

GCD and prime numbers are a fundamental part of number theory.


They have extensively been studied for years. Euclid was fundamental
to the study of prime numbers and the number theory.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 28 / 117


Greatest Common Divisors and Primes Prime Numbers

Definition 2.1: Prime

An integer p > 1 is prime if the only divisors of p are 1 and p. An


integer which is not prime is called composite

Examples
7 is prime because only 1 and 7 divide 7. On the other hand, 3 divides
9 and so 9 is not prime
Recall that another way of describing divisors is as factors. Therefore,
an equivalent definition of a prime number p is one whose factors are
only 1 and p
The fundamental theorem of arithmetic strengthens the idea of
primarity to a prime factorization

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 29 / 117


Greatest Common Divisors and Primes Prime Numbers

Theorem 2.1: Fundamental theorem of Arithmetic

Every integer greater that 1 is either prime or can be written as the


product of two or more primes.
Formally, the theorem can be stated as follows. For Every integer
c > 1 there exists a positive integer n, prime numbers p1 , · · · , pn
and exponents e1 , · · · , en such that

c = p1e1 p2e2 · · · pnen

This theorem is also called the Unique factorization theorem. It means


that any number which is not prime is the product of some primes
a 6 = 2.3
b 16 = 2.2.2.2.2 = 24
c 42 = 2.3.7
d 1234 = 2.617
e 1008 = 2.2.2.2.3.3.7 = 24 .32 .7

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 30 / 117


Greatest Common Divisors and Primes Prime Numbers

Since multiplication is commutative, prime factorization is only


unique to the ordering of factors

420 = 22 .3.5.7 = 7.3.5.22

To get a unique prime factorization, we often add an additional


constraint to the fundamental theorem of arithmetic. This constraint
requires the primes to be listed in increasing order: p1 < p2 · · · < pn

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 31 / 117


Greatest Common Divisors and Primes Finding Primes

How can we determine if a numbers is prime?


A Simple and brute force solution is to try and divide the number in
question by every other integer less than it. If there are no divisors,
then the number is prime. This methods is very inefficient though.
The Sieve of Eratosthenes is a more efficient method. It is based on
the following observation:

Proposition 2.1

If a positive integer n is composite, then it must have a prime



divisor less than or equal to n.

Proof.
If n is a positive composite then there exists two integers a, b greater than
√ √
1 such that n = ab. Certainly a ≤ n or b ≤ n. If n is a perfect square
√ √
then a = b = n. Otherwise, one of a or b must be smaller than n.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 32 / 117


Greatest Common Divisors and Primes Finding Primes

Sieve of Eratosthenes uses this proposition to remove all composite


numbers from a list and retain only the prime ones
Let S = {2, 3, · · · , 100}. Since the maximum √
element of S is 100, we
only need to consider prime divisors less than 100 = 10.
1 Find the smallest element of S. This is 2. This element is prime.
Remove from S all multiples of 2 other than 2 itself.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 33 / 117


Greatest Common Divisors and Primes Finding Primes

Sieve of Eratosthenes uses this proposition to remove all composite


numbers from a list and retain only the prime ones
Let S = {2, 3, · · · , 100}. Since the maximum √
element of S is 100, we
only need to consider prime divisors less than 100 = 10.
1 Find the smallest element of S. This is 2. This element is prime.
Remove from S all multiples of 2 other than 2 itself.
2 Find then next smallest element of the remaining numbers. This is 3.
Remove from S all multiples of 3 other than 3 itself.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 33 / 117


Greatest Common Divisors and Primes Finding Primes

Sieve of Eratosthenes uses this proposition to remove all composite


numbers from a list and retain only the prime ones
Let S = {2, 3, · · · , 100}. Since the maximum √
element of S is 100, we
only need to consider prime divisors less than 100 = 10.
1 Find the smallest element of S. This is 2. This element is prime.
Remove from S all multiples of 2 other than 2 itself.
2 Find then next smallest element of the remaining numbers. This is 3.
Remove from S all multiples of 3 other than 3 itself.
3 Find then next smallest element of the remaining numbers. This is 5.
Remove from S all multiples of 5 other than 5 itself.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 33 / 117


Greatest Common Divisors and Primes Finding Primes

Sieve of Eratosthenes uses this proposition to remove all composite


numbers from a list and retain only the prime ones
Let S = {2, 3, · · · , 100}. Since the maximum √
element of S is 100, we
only need to consider prime divisors less than 100 = 10.
1 Find the smallest element of S. This is 2. This element is prime.
Remove from S all multiples of 2 other than 2 itself.
2 Find then next smallest element of the remaining numbers. This is 3.
Remove from S all multiples of 3 other than 3 itself.
3 Find then next smallest element of the remaining numbers. This is 5.
Remove from S all multiples of 5 other than 5 itself.
4 Find then next smallest element of the remaining numbers. This is 7.
Remove from S all multiples of 7 other than 7 itself.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 33 / 117


Greatest Common Divisors and Primes Finding Primes

Sieve of Eratosthenes uses this proposition to remove all composite


numbers from a list and retain only the prime ones
Let S = {2, 3, · · · , 100}. Since the maximum √
element of S is 100, we
only need to consider prime divisors less than 100 = 10.
1 Find the smallest element of S. This is 2. This element is prime.
Remove from S all multiples of 2 other than 2 itself.
2 Find then next smallest element of the remaining numbers. This is 3.
Remove from S all multiples of 3 other than 3 itself.
3 Find then next smallest element of the remaining numbers. This is 5.
Remove from S all multiples of 5 other than 5 itself.
4 Find then next smallest element of the remaining numbers. This is 7.
Remove from S all multiples of 7 other than 7 itself.
5 Find next smallest element of S is 11. Since 11 > 10, we can stop.
Every remaining number is prime. Every remaining number is prime.
The prime numbers less than 100 are
2,3,7,11,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 33 / 117


Greatest Common Divisors and Primes Finding Primes

Multiples of 2

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 34 / 117


Greatest Common Divisors and Primes Finding Primes

Multiples of 2

2 3 4 5 6 7 8 9 10
10 11 12 13 14 15 16 17 18 19 20
20 21 22 23 24 25 26 27 28 29 30
30 31 32 33 34 35 36 37 38 39 40
40 41 42 43 44 45 46 47 48 49 50

100

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 34 / 117


Greatest Common Divisors and Primes Finding Primes

Multiples of 3

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 35 / 117


Greatest Common Divisors and Primes Finding Primes

Multiples of 3

2 3 - 5 - 7 - 9 -
- 11 - 13 - 15 - 17 - 19 -
- 21 - 23 - 25 - 27 - 29 -
- 31 - 33 - 35 - 37 - 39 -
- 41 - 43 - 45 - 47 - 49 -

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 35 / 117


Greatest Common Divisors and Primes Finding Primes

Multiples of 5

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 36 / 117


Greatest Common Divisors and Primes Finding Primes

Multiples of 5

2 3 - 5 - 7 - - -
- 11 - 13 - - - 17 - 19 -
- - - 23 - 25 - - 29 -
- 31 - - - 35 - 37 - - -
- 41 - 43 - 45 - 47 - 49 -

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 36 / 117


Greatest Common Divisors and Primes Finding Primes

Multiples of 7

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 37 / 117


Greatest Common Divisors and Primes Finding Primes

Multiples of 7

2 3 - 5 - 7 - - -
- 11 - 13 - - - 17 - 19 -
- - - 23 - - - - 29 -
- 31 - - - - - 37 - - -
- 41 - 43 - - - 47 - 49 -

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 37 / 117


Greatest Common Divisors and Primes Finding Primes

Primes Less than 100

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 38 / 117


Greatest Common Divisors and Primes Finding Primes

Primes Less than 100

2 3 - 5 - 7 - - -
- 11 - 13 - - - 17 - 19 -
- - - 23 - - - - 29 -
- 31 - - - - - 37 - - -
- 41 - 43 - - - 47 - - -
53 59
61 67
71 73 79
83 89
97 -

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 38 / 117


Greatest Common Divisors and Primes Computing Primes

Generating primes is a practical very problem.


A large class of digital security and Cryptography algorithm rely on
prime numbers.
However, there is no known closed formula or function which always
produces primes
The function f (n) = n2 − n + 41 results in prime numbers for all
choices of n between 1 and 40. However, f (41) = 412 is not prime

Theorem 2.2

There are infinitely many prime numbers

Proof the above theorem

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 39 / 117


Greatest Common Divisors and Primes Computing Primes

Though there are infinitely many primes, generating them is a


challenge.
The trial division or the sieve of eratosthenes, can determine if a
number is prime.
However for large numbers, these methods are inefficient.
Another test of primality is based on Fermat’s little theorem

Theorem 2.3: Fermat’s little theorem

For two possitive integers a and p if p is prime and p ∤ a, then


ap−1 ≡ 1 mod p

Since 5 is prime and 5 ∤ 16, By Fermat’s little theorem, it must be the


case that 164 ≡ 1 mod 5.
4
Indeed we have 164 = 24 = 216 = 65536 and 65536 ≡ 1 mod 5
Fermat’s little theorem gives rise to the Fermat primality test.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 40 / 117


Greatest Common Divisors and Primes Computing Primes

Probablilistic Method

For any +integer n, we can deduce that n is not prime if we can find
a number a such that an−1 ̸≡ 1 mod n. Such an a is called Fermat
witness
Fermat probability test leads to a probablilistic method to determine
if a number is prime.
Probabilistic algorithm is one which produces the correct result ”with
high probability” but not necessarily all the time.
It tries to find Fermat witness. If after a certain number of attempts
it cannot find such witness, then the algorithm terminates and
assumes that the number is prime.
The assumption is what makes the algorithm probabilistic. The
number of times to perform the test is not clear.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 41 / 117


Greatest Common Divisors and Primes Computing Primes

A python code implementing the Fermat Primality test.


A random number a is chosen as a possible Fermat Witness. It
generates random primes by choosing using this test
from random i m p o r t r a n d i n t

def isPrime (p , numInter ) :


f o r i i n range ( numInter ) :
a=r a n d i n t ( 2 , p−1)
e=a ∗∗( p−1)%p
i f $( e !=1):
return False :
r e t u r n True

d e f randomPrime ( n ) :
w h i l e ( True ) :
p=r a n d i n t ( 2 ∗ ∗ ( n −1) , 2∗∗( n ) −1):
i f isPrime (p ,128)
return p ;
#p r i n t a random 32− b i t p r i m e
p r i n t ( randomPrime ( 1 6 ) )

o u t p u t : 49103

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 42 / 117


Greatest Common Divisors and Primes Computing Primes

Prime Conjectures

Primes have been studied for thousand of years by countless


researchers yet many properties are unproved
Goldbach’s conjecture Every even integer n greater than 2 is the
sum of two primes. This has been verified for numbers up to
1.6 × 1018
Landau’s conjecture There are infinitely many primes of the form
n2 + 1 for a positive integer n
Twin Prime conjecture There are infinitely many primes that differ
by 2. Twin prime include 5 and 7, 11 and 13, 71 and 73 e.t.c.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 43 / 117


Greatest Common Divisors and Primes Computing Primes

Definition 2.2: GCD

For two non-zero integers a and b, d is the greatest common divisor


of a and b if d|a, d|b, and any other common divisor of a and b also
divides d

Definition 2.3: Co-prime

Two integers are relatively prime if their Greatest Common Divisor is


1. Such integers are co-prime

The GCD of two numbers can be determined if they are relatively


prime based on their factorization
a = p1e1 p2e2 · · · pnen
b = q1e1 q2e2 · · · qnen
If any of the primes pi equals a prime pj , then a and b have a non
trivial GCD.
henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 44 / 117
Greatest Common Divisors and Primes Computing Primes

If a and b have no primes in common between their prime


factorization, then they are relatively prime
The GCD of a and b can be computed from their prime factorization
by getting the product of all common prime raised to the minimum
exponent of that prime in either number.

Example 2.1: GCD from Primes

To compute the GCD of 1470 and 350 then

1470 = 2.3.5.72
350 = 2.52 .7
The GCD of 1470 and 350 is thus 2min(1,1) .5min(1,2) .7min(2,1) =
70

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 45 / 117


Greatest Common Divisors and Primes Computing Primes

Least Common Multiples


Prime factorization can also used to compute the LCM between two numbers.

Definition 2.4: Least Common Multiple

The LCM of two positive integers a and b is the smallest positive integer
that is divisible by both a and b

GCD is computed by taking the common primes of two integers(i.e. the


intersection of primes in their factorization) whereas the LCM is computed by
taking the Union of primes in their factorization. In this case, each prime is raised
to the maximum exponent of that prime in either number

Example 2.2: Computing LCM from primes

We compute the LCM of 1470 and 350 thus

1470 = 2.3.5.72
350 = 2.52 .7
The LCM of 1470 and 350 is thus 2max(1,1) .5max(1,2) .7max(2,1) = 7350

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 46 / 117


Greatest Common Divisors and Primes Computing Primes

Least Common Multiples

Computing the prime factorization of a number in general is a challenge.


Therefore the above method for computing the LCM is not practical. A
more practical method is the Euclidean Algorithm

Theorem 2.4

For any two positive integers a and b, we have

a · b = gcd(a, b) · lcm(a, b)

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 47 / 117


Greatest Common Divisors and Primes Euclidean Algorithm

This Algorithm is efficient for computing the GCD of two numbers.


Has been there for many years and it is attributed to Euclid.
Based on simple idea rooted in Euclidean division.
Let a = bq + r by Euclidean division.
Then, by rearranging r = a − bq
If we let d be a common divisor of a and b,
Thus d | a and d | b, Certainly, then we have d | r since
r = a − bq

Lemma 2.1

Let a and b be integers with a = bq + r by Euclidean division, Thus


0≤r <b
gcd(a, b) = gcd(b, r )

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 48 / 117


Greatest Common Divisors and Primes Euclidean Algorithm

Example 2.3: Euclidean by example

Let us find the GCD of 287 and 91 by the Euclidean algorithm.


287 = 91 · 3 + 14 : 287 mod 91 = 14
91 = 14 · 6 + 7 : 91 mod 14 = 7
14 = 7 · 2 + 0 : 14 mod 7 = 0
Since we cannot divide by 0 in the next step, the
process terminates and we have:
gcd(287, 91) = gcd(91, 14) = gcd(14, 7) =
gcd(7, 0) = 7

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 49 / 117


Greatest Common Divisors and Primes Euclidean Algorithm

In this example, notice remainder sequence starting at 287 and ending at 0

r0 = 287
r1 = 91
r2 = 14
r3 = 7
r4 = 0

Notice that the remainder sequence is strictly decreasing.


From the Euclidean division(ED) we have a = bq + r with 0 ≤ r < b.
Since the magnitude of the remainder strictly reduces, and has a
lower 0 as lower bound, then repeated ED will lead to a 0 remainder,
terminating the algorithm. The correctness follows from the previous
lemma.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 50 / 117


Greatest Common Divisors and Primes Euclidean Algorithm

Algorithm gcd(a,b)

Algorithm gcd(a,b)
d e f gcd ( a , b ) :
Input: a, b ∈ Z+ , a > b x = a
Output x, the GCD of a and b y = b
1. x ←− a p r i n t ( ” r 0 : %d” % x )
2. y ←− b p r i n t ( ” r 1 : %d” % y )
3. while y ̸= 0 do i = 2;
4. r ←− x mod y w h i l e y != 0 :
r = x % y
5. x ←− y
p r i n t ( ” r%d : %d” % ( i , r ) )
6. y ←− r i += 1
7. end while x = y
8. return x y = r
return x

p r i n t ( ”GCD( 1 5 2 1 5 2 , 1 5 4 7 0 0 ) =
% gcd ( 1 5 2 1 5 2 , 1 5 4 7 0 0 ) )

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 51 / 117


Greatest Common Divisors and Primes Euclidean Algorithm

Ouput of the above program

r0: 152152
r1: 154700
r2: 152152
r3: 2548
r4: 1820
r5: 728
r6: 364
r7: 0

GCD(152152, 154700) = 364

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 52 / 117


Greatest Common Divisors and Primes Euclidean Algorithm

Bezout Relations and GCDs

The GCD have the property that it can be expressed as a linear


combination of the two input integers

Theorem 2.5: Bezout Theorem

For any positive integers a and b there exists integers s and t


such that gcd(a, b) = sa + tb

The formula gcd(a, b) = sa + tb is called bezout identity and the


integers s and t are called the Bezout coefficients of a and b
Example 2.4

Find the be Bezout coefficient of 6 and 14

gcd(6, 14) = 2
gcd(6, 14) = (−2).6 + 1.14 = 2

The Bezout coefficients of 6 and 14 are -2 and 1

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 53 / 117


Greatest Common Divisors and Primes Euclidean Algorithm

Bezout Relations and GCDs


The Bezout coefficient of two numbers can be computed through
”two pass” method using the Euclidean Algorithm. Consider the
Euclidean algorithm for computing the GCD of 252 and 198.
252 = 1 · 198 + 54
198 = 3 · 54 + 36
54 = 1 · 36 + 18
36 = 2 · 18 + 0
The gcd(252, 198) = 18. After this we express 18 as a combination of
252 and 198 through successive bottom-up successive Euclidean
divisions and back-substitution of one equation at time
18 = 54 − 1 · 36
18 = 54 − 1 · (198 − 3 · 54) = 4 · 54 − 1 · 198
18 = 4 · (252 − 1 · 198) − 1 · 198 = 4 · 252 − 5 · 198

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 54 / 117


Greatest Common Divisors and Primes Euclidean Algorithm

Consequences of Bezout
Lemma 2.2

Let a, b, c be positive integers such that a and b are relatively prime.


If a | bc then a | c

Proof.
Since a and b are relatively prime then gcd(a, b) = 1
By Hypothesis, assume a | bc
By Bezout theorem, then sa + tb = gcd(a, b) = 1
Multiplying both side c
csa + ctb = c
Since
a | bc, a | ctb
That is there exists q such that ctb = qa
csa + ctb = c
csa + qa = c
a(cs + q) = c
Hence, a | c as required
henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 55 / 117
Greatest Common Divisors and Primes Euclidean Algorithm

Consequences of Bezout
Lemma 2.3

Let p be a prime integer and a1 , a2 , . . . , an be integers.


If p | a1 a2 · · · an then p | ai for at least one i

Though Division does not always maintain congruence relations as is the


case for sums and products, it does under Bezout relations

Theorem 2.6

Let m be a positive integer and a, b, c be integers. If gcd(c, m) = 1


and ac ≡ bc mod m, then a ≡ b mod m

Proof.
By Hypothesis ac ≡ bc mod m hence m | ac − bc = c(a − b). And from the previous
lemma, m must therefore divide c or (a − b). Since, by assumption, gcd(c, m) = 1, it
must be that m | a − b. That is a ≡ b mod m
henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 56 / 117
Greatest Common Divisors and Primes Euclidean Algorithm

Exercises

1 Write a Java program with a public function that takes an integer n


and prints all prime numbers between 2 and n. Use the sieve of
Eratosthenes as your algorithm
2 Determine the following values
a gcd(−24, 18)
b gcd(756, 210)
c gcd(−756, 210)
d gcd(742, 14)
3 Compute the prime factorization of the following numbers
1 78
2 672
3 7920

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 57 / 117


Greatest Common Divisors and Primes Euclidean Algorithm

Exercises

RSA is a cryptography system which relies on exponentiation and modula


numbers. In particular, it is relatively easy to find three integers e, d, m
such that, for any integer n, 0 ≤ n < m (ne )d ≡ n mod m. We call ne the
encrypted message and e the public key. Then (ne )d ≡ n is the decrypted
message and d is the private key

In particular, we can compute such an m as the least common multiple of


p − 1 and q − 1 for two different prime numbers p and q. If p = 7 and
q = 11 then m = 30

Find e and d such that (3e )d ≡ 3 mod 30

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 58 / 117


Solving Congruences

In the previous section we saw positive integer forms a special kind of


equivalence known as congruence relation of the form 4 ≡ 16 mod 6 since
6 | 16 − 4. In this section variables are included to make the equations
comprehensive.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 59 / 117


Solving Congruences Linear Congruences

A linear congruence is an equivalence of the form ax ≡ b mod m where x


is a variable, a, b are positive integers and m is the modulus.
The solution to this congruence is all integers x which satisfy the
congruence

Example 3.1

2x ≡ 1 mod 5

By inspection we find: 2 · 3 = 6 ≡ 1 mod 5


Solution to this congruence has x = 3
However, notice that x = 8 is also a solution as 2 · 8 = 16 ≡ 1 mod 5

Linear congruence have infinitely many possible solutions.


In the above example since x = 3 was a solution, then so is every
element in the congruence class of 3. Recall 3 = {x | x ≡ 3 mod 5}.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 60 / 117


Solving Congruences Linear Congruences

Modular Inverses

Solving the equation of the form ax = b over the reals.


b
Normally divide through by a assuming a ̸= 0 to get x = a
This is equivalent to multiplying both sides by the multiplicative
inverse of a - This is another number such that their product is
identity.
1
Over real numbers (R) then a · a = 1 for any a
As is the case over rational numbers or real numbers, there are (often,
but not always) multiplicative inverses when working modulo a
number.
Given a number x and a modulus m, the multiplicative inverse of x is
another number a such that ax ≡ 1 mod m

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 61 / 117


Solving Congruences Linear Congruences

Modular Inverses
Example 3.2

Compute the inverse of 3 modulo 7

3a ≡ 1 mod 7
15 ≡ 1 mod 7
→a≡5 mod 7

Since in the above example, 5 is the modular inverse 3 mod 7, any number in the
congruence class of 5 modulo is a multiplicative inverse.
Modulo inverses can be used to solve linear congruences. Let a′ be the inverse of a
modulo m, then we have the following relations

ax ≡ b mod m
′ ′
a ax ≡ a bx mod m
x ≡ a′ b mod m

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 62 / 117


Solving Congruences Linear Congruences

Modular Inverses May Not Exist


Sometimes some certain numbers do not have multiplicative inverses
modulo a particular modulus.
Example 3.3

Consider 2 modulo 6. It does not have an inverse. This can be verified by


multiplying 2 by each of the {0, 1, 2, 3, 4, 5}

2·0≡0 mod 6
2·1≡2 mod 6
2·2≡4 mod 6
2·3≡0 mod 6
2·4≡2 mod 6
2·5≡4 mod 6

But when do they not exist? This is because there is no identity and also
gcd(2, 6) ̸= 1 and also the fact that, 2 is called the a zero-divisor for arith-
metic modulo m

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 63 / 117


Solving Congruences Linear Congruences

Computing Modular Inverses

When the number is small, inspection comes in handy to compute the


inverse. eg. What is the inverse of 3 mod 8?
What is the inverse of 151 mod 951

Theorem 3.1

If a and m are relatively prime integers with m > 1, then there exists
a unique modular inverse x of a mod m satisfying 0 < x < m

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 64 / 117


Solving Congruences Linear Congruences

Proof
Proof.
First we proof the existence of x.
By Hypothesis we have gcd(a, m) = 1. Therefore by Bezout theorem there exists
integers s and t such that sa + tm = 1
We therefore have:

1 − sa = tm

m | (1 − sa)

1 ≡ sa mod m

∴ s = x is the modular inverse of m

Next, we show uniqueness.


Assume that there is another modular inverse b of a
By definition of modular inverses we have xa ≡ 1 mod m and
ba ≡ 1 mod m ∴ xa ≡ ba mod m. From a previous theorem x ≡ b mod m since
gcd(a, m) = 1
∴ x is unique for 0 < x < m

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 65 / 117


Solving Congruences Linear Congruences

Examples

Example 3.4: Easy Example

Find the inverse of 3 modulo 7.


The gcd(3, 7) = 1, ∴ an inverse must exist. From Euclidean Division
we have 7 = 2 · 3 + 1 and thus −2 · 3 + 1 · 7 = 1. Hence −2 is the
Bezout coefficient of 3 and −2 ≡ 5 mod 7 is the modular inverse of
3

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 66 / 117


Solving Congruences Linear Congruences

Extended Modular Inverse

Find the inverse of 151 modulo 951


Using the Euclidean The Bezout relation between 951 and 151 via back
Algorithm, we find substitution

1 = 13 − 4 · 3
951 = 6 · 151 + 45
151 = 3 · 45 + 16 1 = 13 − 4(16 − 1 · 13) = −4 · 16 + 5 · 13

45 = 2 · 16 + 13 1 = −4 · 16 + 5(45 − 2 · 16) = 5 · 45 − 14 · 16
16 = 1 · 13 + 3
1 = 5 · 45 − 14(151 − 3 · 45) = −14 · 151 + 47 · 45
13 = 4 · 3 + 1
3=3·1+0 1 = −14 · 151 + 47(951 − 6 · 151) = −296 · 151 + 47 · 951

∴, gcd(951, 151) = 1 ∴ the modular inverse of 151 modulo 951 is


−296 ≡ 655 mod 951

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 67 / 117


Solving Congruences Linear Congruences

Example

Solve the following linear congruence by first computing an inverse.


57x ≡ 13 mod 67

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 68 / 117


Solving Congruences Linear Congruences

Solution

First find the inverse of 57 modulo 67 . Use the Euclidean algorithm:

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 69 / 117


Solving Congruences Linear Congruences

Solution

First find the inverse of 57 modulo 67 . Use the Euclidean algorithm:

67 = 1 · 57 + 10

57 = 5 · 10 + 7

10 = 1 · 7 + 3

7=2·3+1

3=3·1+0

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 69 / 117


Solving Congruences Linear Congruences

Solution

Since the gcd(67, 57) = 1 so a modular exists. Follow this then by


computing the Bezout coeffiecients

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 70 / 117


Solving Congruences Linear Congruences

Solution

Since the gcd(67, 57) = 1 so a modular exists. Follow this then by


computing the Bezout coeffiecients

1=7−2·3

1 = 7 − 2(10 − 1 · 7) = −2 · 10 + 3 · 7

1 = −2 · 10 + 3(57 − 5 · 10) = 3 · 57 − 17 · 10

1 = 3 · 57 − 17(67 − 1 · 57) = −17 · 67 + 20 · 57

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 70 / 117


Solving Congruences Linear Congruences

Solution

The inverse of 57 modulo 67 is 20. This yields

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 71 / 117


Solving Congruences Linear Congruences

Solution

The inverse of 57 modulo 67 is 20. This yields

57x ≡ 13 mod 67

20(57x) ≡ 20 · 13 mod 67

1 · x ≡ 260 mod 67

x ≡ 59 mod 67

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 71 / 117


Solving Congruences Systems of Linear Congruences

Systems of Linear Congruences

For a system of linear congruences of the form



 x ≡ 3 mod 7
 x ≡6 mod 13

Can we find a value of x that simultaneously satisfies both of these


equations? Inspecting, 45 is a possible solution as 7 | (45 − 3) = 42 and
13 | (45 − 6) = 39
Is there an algorithmic process to find such an x? Yes

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 72 / 117


Solving Congruences Systems of Linear Congruences

Chinese Remainder Theorem

Theorem 3.2: Chinese Remainder Theorem

Let m, n be two co-prime integers greater than 1 then



 x ≡ a mod m
 x ≡b mod n

Has a unique solution modulo m · n

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 73 / 117


Solving Congruences Systems of Linear Congruences

Chinese Remainder Theorem - Proof

Proof.
Since m and n are co-prime then by bezout theorem, there exists integers s, t such that;

sm + tn = 1

Then notice that x = bsm + atn satisfies the linear congruences

x = bsm + atn x = bsm + atn


= bsm + a(1 − sm) = b(1 − tn) + atn
= bsm + a − asm = b − btn + atn
≡a mod m ≡b mod n

Consider uniqueness. Let x = y and x = z be two solutions of this system. Then, y and
z must give the same remainder when divided by m or n. ∴ m | y − z and n | y − z.
Since m and n are co-prime, it follows that m · n | y − z ∴ y ≡ z mod m · n

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 74 / 117


Solving Congruences Systems of Linear Congruences

CRT - Examples

Example 3.5: Linear Congruences

Find all integers x, 0 ≤ x < 15 such that



 x ≡ 1 mod 3
 x ≡2 mod 5

Since 3 and 5 are co-prime, CRT states that there exists a unique
solution modulo 15
∴ exactly one solution x with 0 ≤ x < 15
Applying the Euclidean algorithm to find s, t such that 3s + 5t = 1
Or, by inspection 3(2) + 5(−1) = 1 ∴ s = 2, t = −1
Thus x = 2(3s) + 1(5t) = 2(3)(2) + 1(5)(−1) = 7 ≡ 7 mod 15

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 75 / 117


Solving Congruences Systems of Linear Congruences

CRT - Examples

Example 3.6: Linear Congruences

For the following Linear congruences solve for x where 0 ≤ x < 91



 x ≡ 3 mod 7
 x ≡6 mod 13

Using Euclidean Algorithm 7 and 13 are co-prime


∴ there exist s, t such that 7s + 13t = 1
By inspection s = 2 and t = −1
∴ a solution x = 6(7s) + 3(13t) = 84 − 39 = 45
Hence we verify that 7 | 45 − 3 = 42 and 13 mod 45 − 6 = 39

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 76 / 117


Solving Congruences Exercises

1 Find the Multiplicative inverse of:


a 8 modulo 17
b 9 modulo 13
c 11 modulo 71
2 Prove the follwing Lemma

Lemma 3.1

Let m and n be co-prime integers


For any integer x such that m | x and n | x then mn | x

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 77 / 117


Solving Congruences Exercises

Solution of the Lemma in the exercises

Proof.
By Hypothesis m | x and n | x
∴, ∃ qm , qn such that x = mqm and x = nqn hence mqm = nqn
Since m and n are co-primes, ∃ s, t such that sm + tn = 1
∴, smqm + tnqm = qm and combining with our hypothesis gives
snqn + tnqm = qm and thus
n(sqn + tqm ) = qm
From x = mqm we get
x = mn(sqn + tqm ) Hence
mn | x

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 78 / 117


Solving Congruences Exercises

Excises Cont’d
3 Solve the following linear congruences. Give the unique positive
solution which is less than the modulus.
i x ≡ 12 mod 7
ii 2x ≡ 12 mod 7
iii 13x ≡ 15 mod 23
4 Solve the following system of linear congruences for 0 ≤ x < 77

 3x ≡ 2 mod 11
 4x ≡ 6 mod 7

5 Solve the following system of linear congruences for 0 ≤ x < 221



 4x ≡ 11 mod 13
 2x ≡ 7 mod 17

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 79 / 117


Integer Representations

Consider the number 496.

This number, Four Hundred and Ninety Six means what?

Rarely do we think about about this because the decimal system of


numbers was introduced to us from the beginning.

However, there are many ways we could represent and understand numbers

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 80 / 117


Integer Representations Positional Number System

Positional Number System

Other than using roman numeral and or series of dots to encode the
things being counted it is much more convenient to use a positional
number system
In the roman numerals I mean 1 v means 5 · · · , the position of the
symbol does not change its value
Decimal representation of number is positional. Putting two 1
symbols together to form 11 means something differenct from II in
Roman numerals.
Decimal System of numbers has radix or base 10.
Position of a digit in a number represents a multiple of a certain
power of 10

12345 = (1 · 104 ) + (2 · 103 ) + (3 · 102 ) + (4 · 101 ) + (5 · 100 )

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 81 / 117


Integer Representations Positional Number System

Radix-r Representations
The positional number system can be represented with any choice of radix.
In modtern times base 10 has been used as the main representation because humans
have 10 fingers.
Historically, different groups used different bases. The Mayans used base 20 (10 toes
and 10 fingers). The Babylonians used base 60. This is why time and angles are
measured in groups of 60 seconds, 60 minutes.
Given some radix r , we can construct a number system using r as the radix or base.
This results from the following theorem.

Theorem 4.1

Let r be a positive integer greater than 1. Any positive integer n can be expressed
uniquely in the form:
n = ak r k + ak−1 r k−1 + · · · + a2 r 2 + a1 r + a0
where k is a non-negative integer, aj (0 ≤ j ≤ k) belongs to the set
{0, 1, . . . , r − 1} and ak ̸= 0

This formula for n is called the radix-r representation of n.


henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 82 / 117
Integer Representations Positional Number System

Radix-r Representations

In the modern technological age, base-2, base-8, and base-16 are


important number systems.
1 Base-2 (binary) is used throughout electronics as the “digital
numbers”. Each digit is 0 or 1, a bit, representing “off” or “on” of
the electrical voltage.
2 Base-8 (octal) is used throughout computing where numbers were
represented using 6, 12, or 24 bits, and thus 2, 4, or 8 octal digits.
3 Base-16 (hexadecimal) has become popular in computing where
computers now represent numbers using 32 or 64 bits or and thus 8
hex digits.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 83 / 117


Integer Representations Positional Number System

Radix-r Representations

Example 4.1: Binary Numbers

(1010)2 = (1 · 23 ) + (0 · 22 ) + (1 · 21 ) + (0 · 20 ) = 8 + 2 = 10

(10101)2 = (1 · 24 ) + (0 · 23 ) + (1 · 22 ) + (0 · 21 ) + (1 · 20 ) = 16 + 4 + 1 = 21

15
X
(1111111111111111)2 = 2i = 216 − 1 = 65535
i=0

A binary number with n digits has a value which ranges from 0 to 2n − 1

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 84 / 117


Integer Representations Positional Number System

Radix-r Representations

Hexadecimal is the most obscure of the number system because of use of


letters as numbers (rather than variables representing numbers). In
hexadecimal, A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15

Example 4.2: Hexadecimal numbers

(123)16 = (1 · 162 ) + (1 · 161 ) + (1 · 160 ) = 256 + 2(16) + 3 = 291

(BC 123)16 = (11 · 164 ) + (12 · 163 ) + (1 · 162 ) + (2 · 161 ) + (3 · 161 )

= 11(65536) + 12(4096) + 291

= 770339

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 85 / 117


Integer Representations Positional Number System

Radix-r Representations - Exercises

Convert the below numbers to decimal numbers.


1 (11001)2
2 (1000011001)2
3 (7612)8
4 (7612)16

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 86 / 117


Integer Representations Positional Number System

Radix-r numbers in practice

In practice, we write radix-r representations different from the


mathematical way of (23DFEA4)16
Binary numbers “0b” prefix 0b101101 = 45
Octal Numbers “0” or “0o” prefix 0o12654 = 5548
Hexadecimal Numbers “0x” prefix 0x23DFEA4 = 37617316
•Programming languages feature native support for many different radix
representations.
• For example in Python one can define a “literal” numbers in different
radix representations use the aforementioned prefixes.
• Convert a decimal number to binary with bin(), to octal with oct(),
and to hexadecimal with hex().
• Note that these functions return a string representing the number.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 87 / 117


Integer Representations Converting to radix-r

Converting to radix-r
Radix-r representations are an expansion of a number using powers of as the
base. This suggests that converting from decimal to a radix- r
representation can be performed by (repeated) division.
Let r be some radix and n be some integer number to convert to radix- r .
By Euclidean division we have:
n = q0 r + a0
with 0 ≤ a0 < r
Notice that a0 is thus a digit in the radix-r number system. In fact, a0 is the
first digit (counting from the right) of the radix-r representation or n.
Continue dividing q0 by r
q0 = q1 r + a1
with 0 ≤ a1 < r Again, a1 is a digit in the radix-r number system and a1 is
the second digit of the radix-r representation of n.
This continues until a qk is 0, using the successive remainders as the digits
of the radix-r representation of n
henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 88 / 117
Integer Representations Converting to radix-r

Converting to radix-r

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 89 / 117


Integer Representations Converting to radix-r

Converting to radix-r

Example 4.3: Converting to Radix-16

Convert 93752 to hexadecimal

93752 = 5859(16) + 8

5859 = 366(16) + 3

366 = 22(16) + 14

22 = 1(16) + 6

1 = 0(16) + 1

∴ 93752 = (16E 38)16

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 90 / 117


Integer Representations Converting to radix-r

Converting to radix-r
How does this work?

Notice that we have n − a0 = q0 r .


Hence, r | n − a0
and n ≡ a0 mod r
This makes sense because we want:

n = ak r k + ak−1 r k−1 + · · · + a2 r 2 + a1 r + a0 ↔ n ≡ a0 mod r

↔ n ≡ a1 r + a0 mod r 2

..
.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 91 / 117


Integer Representations Binary, Octal, Hex Conversion

Binary, Octal, Hex Conversion

Recall that binary, octal, and hexadecimal numbers all have roots in
electronics and computing.
Each of these representations have their own merits in computer
science.
Therefore very useful to be able to convert between these
representations. Doing such a conversion is very easy.
The binary system uses one digit to represent each bit in a computer
system.
The octal system, with digits 0 through 7 represents three binary
digits at once: 7 = (111)2 , 6 = (110)2 , etc.
Therefore, we can easily convert from binary to octal by grouping
binary digits into threes and then converting each group to its
corresponding decimal (octal) digit.

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 92 / 117


Integer Representations Binary, Octal, Hex Conversion

Example 4.4: Binary to Octal

(1101010100010101001010)2 → 001 101 010 100 010 101 001 010

→1 5 2 4 2 5 1 2

→ (15242512)8

Hexadecimal system represents 4 binary digits at once 15 = (1111)2 , 14 = (1110)2 · · ·

Example 4.5: Binary to Hexadecimal

(1101010100010101001010)2 → 0011 0101 0100 0101 0100 1010

→3 5 4 5 4 A

→ (35454A)16

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 93 / 117


Integer Representations Binary Arithmetic

Binary Arithmetic
Regardles of which number systems, arithmetic like addition,
multiplication, division, etc. always give the same result.
Whether in binary, hexadecimal, octal, or decimal, the sum of two
numbers is still its sum.
The only thing that changes is the way we write down the numbers
being added and their sum.
Doing addition and subtraction in the binary number system is not so
different from doing it in the decimal system.
The key is to understand how we add individual digits, just like in
“long addition” in the decimal system.
There are three possibilities for adding single bits: both are 0, both
are 1, or one is 1 and the other is 0
(0)2 + (0)2 = (0)2
(1)2 + (0)2 = (1)2
(1)2 + (1)2 = (10)2
henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 94 / 117
Integer Representations Binary Arithmetic

There are four cases for adding 3 bits together: there are zero 1s, there is
one 1, there are two 1s, there are three 1s.

(0)2 + (0)2 + (0)2 = (0)2


(1)2 + (0)2 + (0)2 = (1)2
(1)2 + (1)2 + (0)2 = (10)2
(1)2 + (1)2 + (1)2 = (11)2

Using this basic addition of three bits and the ideas of “carrying” digits,
we can compute the addition of any two binary numbers.

Example 4.6: Binary Addition

Compute (1101)2 + (110)2 in binary

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 95 / 117


Integer Representations Binary Arithmetic

Binary Multiplication
Binary multiplication can be derived from binary addition with a simple observation. Let
m = (ak ak−1 · · · a2 a1 )2 and n = (bℓ bℓ−1 · · · b2 b1 )2
Then
m · n = m · (bℓ 2ℓ + bℓ−1 2ℓ−1 + · · · + b2 22 + b1 2 + b0 )

= mbℓ 2ℓ + mbℓ−1 2ℓ−1 + · · · + mb2 22 + mb1 2 + mb0


As m is itself a binary number each term mbℓ 2ℓ has a simple computation

First, since bℓ is a binary digit, it is either 0 or 1.

If it is 0, the product is also 0.

If it is 1 then abℓ 2ℓ = a2ℓ

Notice
m2ℓ = 2ℓ (ak 2k + ak−1 2k−1 + · · · + a2 22 + a1 2 + a0 )

= ak 2k+ℓ + ak−1 2k−1+ℓ + · · · + a2 22+ℓ + a1 21+ℓ + a0 2ℓ


Therefore, m2ℓ is just a shift
henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 96 / 117
Integer Representations Binary Arithmetic

Binary Multiplication

The algorithm of computing the binary multiplication for two numbers m


given as m = (ak ak−1 · · · a2 a1 )2 and n given as n = (bℓ bℓ−1 · · · b2 b1 )2 is as
follows
1 Let p = 0
2 For i = 0, . . . , ℓ, if bi = 1, then p = p + (m · 2i )
3 p is the product m · n

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 97 / 117


Integer Representations Binary Arithmetic

Binary Shift
When multiplying a binary number by a power of 2, the result is simply a shift of digits to the
left, with the corresponding number of 0 digits added on the right.
(1101011)2 · 25 = 110101100000

Example 4.7: Binary Multiplication

Compute the product of (1010)2 and (11001)2

In this case, we have two 1 digits correspoding to 23 and 21


Hence the product p is

p = (11001)2 23 + (11001)2 21
= (11001000)2 + (110010)2

00000000 (carry bits)


11001000
+ 00110010
(11111010)2

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 98 / 117


Integer Representations Binary Arithmetic

Exercises

1 Convert the following numbers to decimal


a (11100)2
b (11101101)2
c (235014)8
d (56D9A0D)16
2 Convert the following to Octal
a 174
b (11100)2
c (FFDE )16
d 262144
3 Convert 12847 to
a radix- 6 representation
b radix- 13 representation HINT Use digits {0, 1, · · · 9, A, B, C }

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 99 / 117


Integer Representations Binary Arithmetic

Exercises

4 Write a Java function convert(n,r) which returns a string-encoding of


the radix-r representation of the integer n. Assume that n is
non-negative and that 1 < r ≤ 10 Therefore, you do not have to
worry about digits like A, B, C · · ·
5 Using “long” binary multiplication, compute the product of (101011)2
and (1101)2

henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 100 / 117

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy