EtCPC Number Theory
EtCPC Number Theory
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.
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.
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 (%)
gcd(𝑎, 𝑏) = gcd(𝑎 − 𝑏, 𝑏)
Simplifying fractions: The GCD is used to simplify fractions by dividing both the
numerator and denominator by their GCD.
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.
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
Books:
Thank You
Questions ?