0% found this document useful (0 votes)
2 views

EtCPC Number Theory

Uploaded by

firahagos7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

EtCPC Number Theory

Uploaded by

firahagos7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

2024 | EtCPC

EtCPC - 2nd Tutorial Session

Number
Theory
Presenter :- Ahmed Hibet
Topics to be covered

awe

asdf

asdf

Presenter
Ahmed Hibet
Software Developer | Competitive Programmer
To identify prime numbers efficiently, various algorithms have been developed.

One popular algorithm is the Sieve of Eratosthenes. Additionally, there are primality testing
algorithms that can determine if a given number is prime.

Let's explore few of them and their time complexities.

Sieve of Eratosthenes

The Sieve of Eratosthenes is an ancient algorithm used to find all prime numbers up to a given limit.

It works by iteratively marking the multiples of each prime number, starting from 2, and eliminating the
multiples as non-prime.

The remaining unmarked numbers are prime.

Time Complexity: O(n log log n), where n is the upper limit
Sieve of Eratosthenes
1. Create a list of all integers from 2 to N.

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
Sieve of Eratosthenes
2. Initially, let p = 2, the smallest prime number.

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

Idea: For any prime N, all multiples of p larger than p i.e. 2p, 3p, ⋯ , np are composite.
Sieve of Eratosthenes
3. So, mark every multiple of p.

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
Sieve of Eratosthenes
4. Now, find the smallest number greater than p which is not marked. We know p = 3.

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
Sieve of Eratosthenes
5. Mark every multiple of p.

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
Sieve of Eratosthenes
6. Now, find the smallest number greater than p which is not marked.
We know p = 5. Mark every multiple of p.

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
Sieve of Eratosthenes
7. Now, find the smallest number greater than p which is not marked.
We know p = 7. Mark every multiple of p.

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
Sieve of Eratosthenes
Now, all multiples of 2, 3, 5, and 7 are marked. Since √N ≈ 7.07, all unmarked cells are
guaranteed to be prime. Therefore, we can consider all unmarked numbers as primes.

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
Implementation of Sieve of Eratosthenes
Prime factorizing with Sieve of Eratosthenes
Prime factorizing with Sieve of Eratosthenes

Result: 17 5 2
Divisibility by 2

Divisiblity Rules
Divisibility by 3
Divisibility rules are helpful guidelines
that allow us to determine if a number
is divisible by another number without
performing actual division. These
rules provide a quick and efficient way Divisibility by 5
to identify divisibility patterns. Let's
explore some common divisibility
rules:
Divisibility by 7

Divisibility by 9
Modulo Operator (%)

The modulo operator (%) is a fundamental operator used in


programming languages to perform modular arithmetic.

It returns the remainder of a division operation between two


numbers. i.e 𝑎 = 𝑏𝑞 + 𝑟
It is denoted by “a % b“.
Example:
19 % 5 = 4, since 19 = 5 ⋅ 3 + 4
-19 % 5 = 1, since -19 = 5 ⋅ -4 + 1
19 % -5 = -1, since 19 = -5 ⋅ 4 + -1
-19 % -5 = -4, since -19 = -5 ⋅ 3 + -4
Modulo Operator (%)
We only consider when 𝑀 is positive.
Addition, subtraction and multiplication:
(𝑎 + 𝑏) mod 𝑀 = ((𝑎 mod 𝑀) + (𝑏 mod 𝑀)) mod 𝑀
(𝑎 - 𝑏) mod 𝑀 = ((𝑎 mod 𝑀) - (𝑏 mod 𝑀)) mod 𝑀
(𝑎 * 𝑏) mod 𝑀 = ((𝑎 mod 𝑀) * (𝑏 mod 𝑀)) mod 𝑀

However it is impossible to know the result of division by:


(𝑎 / 𝑏) mod 𝑀 = ((𝑎 mod 𝑀) / (𝑏 mod 𝑀)) mod 𝑀
Caution: Modulo of negative numbers
Application of Modular Arithmetic in Programming:
Application of Modular Arithmetic in Programming:
Wrapping around a range: Modular arithmetic allows us to wrap values within a specific
range.
Example: In a 24-hour clock, adding 13 hours to 23 gives 12, as it wraps around to the
start of the day.
Congruence relationships: Modular arithmetic helps establish congruence relationships
between numbers.
Example: 15 ≡ 3 (mod 12) means that 15 and 3 have the same remainder when divided
by 12.
Hashing and indexing: Modular arithmetic is widely used in hashing and indexing
algorithms.
Example: Hashing a string into a fixed number of buckets using a modulo operation.
Generating random numbers: Modular arithmetic plays a role in generating random
numbers within a specified range.
Application of Modular Arithmetic in Programming:
GCD and Euclidean Algorithm
The Greatest Common Divisor (GCD) is a fundamental concept
in number theory that represents the largest positive integer
that divides two or more numbers without leaving a remainder.
The Euclidean algorithm is a widely used method for finding the
GCD. Let's explore GCD and the Euclidean algorithm.

The Euclidean algorithm is an efficient algorithm for


finding the GCD of two numbers.

It uses the property that the GCD of two numbers is the


same as the GCD of the smaller number and the
remainder of dividing the larger number by the smaller
number.

The algorithm repeatedly divides the larger number by


the smaller number until the remainder becomes zero.

The last non-zero remainder obtained is the GCD of the


original two numbers.
Euclidean Algorithm

GCD has some properties. We are going to use

gcd(𝑎, 𝑏) = gcd(𝑎 − 𝑏, 𝑏)

__gcd(a, b) in C++ from algorithm library


gcd(a, b) in Python from math library
Applications of GCD

Simplifying fractions: The GCD is used to simplify fractions by dividing both the
numerator and denominator by their GCD.

Modular arithmetic: The GCD is utilized in various modular arithmetic calculations


and solving congruence equations.

Cryptography: The GCD plays a role in various cryptographic algorithms, including


RSA.

In conclusion, the Greatest Common Divisor (GCD) is the largest positive integer that
divides two numbers without leaving a remainder. The Euclidean algorithm provides an
efficient method to calculate the GCD. It has applications in various areas, including
simplifying fractions, modular arithmetic, and cryptography. Programming languages
offer convenient ways to compute the GCD using built-in functions or methods.
LCM and its Calculation
The Least Common Multiple (LCM) is a fundamental
concept in number theory that represents the
smallest positive integer that is divisible by two or
more numbers. Let's explore LCM and how it can be
calculated.

There are multiple methods to calculate the LCM,


including prime factorization and using the GCD.

One common approach is to use the relationship


between GCD and LCM:

LCM(a, b) = (|a * b|) / GCD(a, b)

The absolute value of the product of the numbers


divided by their GCD gives the LCM.
Applications of LCM

Fractions: The LCM is used to find a common denominator when adding or


subtracting fractions with different denominators.

Scheduling: The LCM is utilized in scheduling tasks or events that repeat at different
intervals.

Divisibility: The LCM helps determine the smallest number divisible by a given set of
numbers.

In conclusion, the Least Common Multiple (LCM) is the smallest positive integer
divisible by two or more numbers. It can be calculated using various methods,
including the relationship with the Greatest Common Divisor (GCD). Programming
languages provide built-in functions to compute the LCM conveniently. The LCM finds
applications in fractions, scheduling, and divisibility problems.
Recap: Key Concepts

In this tutorial, we covered several key concepts in number theory,


including prime numbers, divisibility rules, modular arithmetic, Greatest
Common Divisor (GCD), and Least Common Multiple (LCM). Let's recap
these concepts and their importance.

Understanding these concepts is essential for various mathematical


computations, problem-solving, and cryptographic algorithms. They form
the foundation of number theory and provide tools for efficient calculations
and divisibility testing.
Further Exploration
To further expand your knowledge in number theory and
related topics, here are some suggestions for additional
resources and learning materials:

Books:

"Elementary Number Theory" by David M. Burton

"A Friendly Introduction to Number Theory" by Joseph H.


Silverman

"The Art of Computer Programming" by Donald E. Knuth

"Guide to Competitive Programming" by Antti Laaksonen


2023 | EtCPC

Thank You
Questions ?

You might also like