0% found this document useful (0 votes)
29 views35 pages

Numerics Lecture - G5 - No Code

The document outlines a lecture on fundamental math concepts for algorithmic problem-solving, covering topics such as divisibility, prime numbers, greatest common divisor (GCD), and the inclusion-exclusion principle. It includes prerequisites, mathematical definitions, algorithms, and examples to illustrate these concepts. Additionally, it features exercises and a quote by Carl Friedrich Gauss to inspire deeper exploration of mathematics.

Uploaded by

remidan37
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)
29 views35 pages

Numerics Lecture - G5 - No Code

The document outlines a lecture on fundamental math concepts for algorithmic problem-solving, covering topics such as divisibility, prime numbers, greatest common divisor (GCD), and the inclusion-exclusion principle. It includes prerequisites, mathematical definitions, algorithms, and examples to illustrate these concepts. Additionally, it features exercises and a quote by Carl Friedrich Gauss to inspire deeper exploration of mathematics.

Uploaded by

remidan37
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/ 35

Numerics/Math

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

● Time and Space Complexity Analysis


● Recursion I
● Loops and Conditionals
Divisibility
Divisibility
● We can describe any number N as the product of the divisor d and the quotient
q, plus a remainder r.
○ N = q * d + r, 0 <= r < d

● 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

● Example: solve for x%k


○ (a + x)%k = 0
○ Answer: x%k = (-a)%k
Prime Numbers
Prime Numbers

● What are prime numbers?


● 2, 3, 5, 7 . . .
Prime Numbers

● How can we check if a number is prime?


● We could check if the number is divisible by all the numbers
before it?
Primality Test
● If x = d1*d2.
● Either d1 <= d2 or d2 < d1.
● Thus d1*d1 <= d1*d2 = x or d2*d2 < d1*d2 = x.
● What does this mean?
Primality Test - Time and Space Complexity

● The loop runs until we reach the first time d*d = n


● Hence, the time complexity is O(d) = O(sqrt(n))
Fundamental Theorem Of Arithmetic

● Every positive integer can be written in a unique way as a


product of primes:
● n = p1 * p2 * p3 * . . . pn
● Example:
○ 84 = 2*2*3*7
○ 52 = ?
Prime Factorization

How can we factorize a given number n into prime factors?


● Start from d = 2.
● Divide n by d as long as you can. Append d.
● Increment d by 1 and try again.
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 - 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
Exercise

Count Primes
GCD
Greatest Common Divisor
● GCD (Greatest Common Divisor) of two numbers a
and b is the greatest number that divides evenly
into 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

Thus m also divides r.

If r = 0, then a is a multiple of b and thus b is the GCD.


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

● used to count the number of elements in the union of multiple sets.

○ n(AUB) = n(A) + n(B) - n(A𝉅B)


○ n(AUBUC) = n(A) + n(B) + n(C)
- n(A𝉅B) - n(B𝉅C) - n(A𝉅C)
+ n(A𝉅B𝉅C)
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

"The enchanting charms of this sublime science


reveal only to those who have the courage to go
deeply into it."

~ Carl Friedrich Gauss

You might also like