Numerics Lecture - With Code
Numerics Lecture - With Code
Lecture Objectives
● Understanding Fundamental Math Concepts for Algorithmic
Problem-solving
● Utilizing Common Numerical Algorithms in DSA Problem Solving
Lecture Outline
● Prerequisites
● Divisibility and Modular Arithmetic
● Prime Numbers
● Greatest Common Divisor
● Inclusion-Exclusion Principle
● Quote of the Day
Prerequisites
● Python syntax
○ N % d == r
○ N // d == q
○ divmod(N, d) == (q, r), divmod is a function that returns (q, r)
Divisibility - Modular Arithmetic
● (a + b) % m = (a%m + b%m) % m
● (a - b) % m = (a%m - b%m) % m
● (a*b)%m = (a%m * b%m) % m
● If (a - b) % m = 0, then a%m = b%m
● Division is a little complicated.
Divisibility - Modular Arithmetic
● Sometimes, it could be difficult to work with modular arithmetic, especially
under time pressure.
● If we need to express the modulo of an unknown variable in modular arithmetic,
we can follow these steps
○ Get rid of the modulo operator from each term
○ Rearrange the terms in the usual arithmetic rules so that you can express
the unknown variable on one side and the knowns on the other
○ Modulo every term
● Note: This works only if the involved operations are addition, subtraction and
multiplication (of integers)
Divisibility - Modular Arithmetic
Problem Link
return factorization
Exercise
Almost Prime
Divisibility and Prime Factors
How does prime factorization relate to divisibility?
24 4 Example: 24 is divisible by 4
2 2 4=2*2
3 2
24 = 2 * 2 * 2 * 3
Generating Primes
Sieve of Eratosthenes
Sieve of Eratosthenes
● To determine all the primes in the range [2, n]
○ Start with two and mark all its multiples until n not including
it
○ Then mark the multiples of the next unmarked number not
including it
○ At the end the unmarked elements are the primes
Sieve of Eratosthenes
def prime_sieve(n: int) -> list[bool]:
is_prime: list[bool] = [True for _ in range(n + 1)]
is_prime[0] = is_prime[1] = False
i = 2
while i <= n:
if is_prime[i]:
j = 2 * i
while j <= n:
is_prime[j] = False
j += i
i += 1
return is_prime
Sieve of Eratosthenes - Optimization
● How can we optimize it?
○ Sieve till root
○ Consider only proper multiples greater than
square of the number
○ Sieving by the odd numbers only
Sieve of Eratosthenes
def prime_sieve(n: int) -> list[bool]:
is_prime: list[bool] = [True for _ in range(n + 1)]
is_prime[0] = is_prime[1] = False
i = 2
while i * i <= n:
if is_prime[i]:
j = i * i
while j <= n:
Time Complexity: O(n log log n)
is_prime[j] = False
j += i
i += 1
return is_prime
Proof of the bound on sum of the
reciprocal of primes
Exercise
Count Primes
GCD
Greatest Common Divisor
● GCD (Greatest Common Divisor) of two numbers a
and b is the greatest number that divides both a
and b.
○ Naive algorithm
○ Fast algorithm (log(n))
Greatest Common Divisor
● How would you calculate gcd by hand?
Greatest Common Divisor
● In the prime factorisation method, each given number is written
as the product of prime numbers and then find the product of
the smallest power of each common prime factor. Why?
Greatest Common Divisor
● Find the GCD of 32 and 28
Greatest Common Divisor
Let a = b*q + r, and m be a common divisor
● r%m = (a - b*q)%m
= (a%m - (b*q)%m)%m
= (a%m - (b%m)*(q%m))%m
= (0 - 0)%m = 0
Formal Proof
Greatest Common Divisor
● Find the GCD of 32 and 28
Least Common Multiples
● LCM(a, b): the smallest positive integer that is a multiple of a and b
● How would you calculate LCM by hand?
● LCM(a, b) x GCD(a, b) = a x b
○ Why?
● How can we implement the LCM function?
Inclusion-Exclusion Principle
Inclusion-Exclusion Principle
● Find the total number of integers between 1 and 100 that are either divisible by 2 or by 3.
○ How many multiples of 2 are there between 1 and 100?
○ How many multiples of 3 are there between 1 and 100?
○ How many multiples of 6 are there between 1 and 100?
What about the number of integers that are divisible by either of 2, 3 and 5?
Complicated GCD
Exercise
Find Greatest Common Divisor of Array
Number of Subarrays With GCD Equal to K
Count Primes
Divisibility by 2^n
Block Game
Divide and Equalize
Quote of the Day