0% found this document useful (0 votes)
15 views51 pages

Lecture 3 Number Theory

The document provides a comprehensive overview of prime numbers, their definitions, and methods for determining primality, including naive and better approaches. It also covers modular arithmetic concepts, including congruences, modular inverses, and Fermat's Little Theorem, as well as techniques for calculating GCD and the Sieve of Eratosthenes for finding prime numbers. Additionally, it includes links to related resources and practice problems for further study in number theory.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views51 pages

Lecture 3 Number Theory

The document provides a comprehensive overview of prime numbers, their definitions, and methods for determining primality, including naive and better approaches. It also covers modular arithmetic concepts, including congruences, modular inverses, and Fermat's Little Theorem, as well as techniques for calculating GCD and the Sieve of Eratosthenes for finding prime numbers. Additionally, it includes links to related resources and practice problems for further study in number theory.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Prime

Numbers
Basic Definition of Prime Numbers

Prime Numbers: Any natural number greater than 1, which has


exactly two factors ( 1 and the number itself ).
Composite Numbers: Natural numbers which have more than 2
factors.
● 1 is neither prime nor composite.
● 2 is the only even prime number
Primality Tests

Given a positive number N, determine whether N is prime or


not.
Naive Approach

We iterate from i=2 to i=N-1, and in each of these iterations,


we check if i divides N or not.

Complexity:
Naive Approach
Better Approach

Consider any number > 1. Suppose is expressed as a


product of two of its factors, say a and b.

Assume
What’s the upper limit of ?
Better Approach

Proof by contradiction:
Suppose a > √(n), then b >= a > √(n)
=> b > √(n)
=> a*b > √(n)*√(n)= n
=> a*b > n (Contradiction)
Better Approach
Prime Factorization

● Fundamental Theorem of Arithmetic : It states that every


integer greater than 1 can be represented uniquely as a
product of prime numbers, up to the order of factors.
● Ex: 20 = 2x2x5 or 5x2x2 or 2x5x2
Prime Factorization

● There can be at most one prime factor greater than


● Iterate in and remove all factors of N
● After iteration ends, if N>1, then N is the biggest prime
factor.
● Complexity
Number of divisors

Then the number of divisors is given by


Number of Divisors = (a₁ + 1) (a₂ + 1) . . . (a ₖ + 1)
Question - Codeforces : 1294C

https://codeforces.com/problemset/problem/129
4/C
Number
Theory and
Modular
Arithmetic
Modular Number System
Mathematically, a number in the decimal number
system can be as large as you want. But computers
(even the most sophisticated ones) have a finite
capability up to which they can process numbers.

This is due to the fact that a computer will always rely


on a finite amount of resources and thus, its limitation
will always be finite.
In C++ , the Range of data types is finite.
For example:
‘int’ ranges from : - 231 to 231 - 1
‘long long’ ranges from : - 2 63 to 263 - 1
So, how can we deal with numbers that are beyond the
capabilities of our machine?
Congruences
Let m be a positive integer.
We say that two integers a and b are congruent modulo
m, and denote this as a ≡ b (mod m), if m|(a-b).
i.e., if m divides (a-b)
OR
a = b + m*q where q belongs to Z
Here, m is called the modulus of the congruency.
156 ≡0 mod 12
Since (156 - 0) = 156 = 12 x 13
Which means 12|156

121 ≡1 mod 10
Since (121 - 1) = 120 = 10 x 12
Which means 10|120
100 ≡ x mod 7

Find ‘x’ in the above congruence


Modular Integer System

If m is a positive integer, the set of integers modulo m, denoted


by Zm is the set of possible remainders when dividing by m:
Zm = {0, 1, 2, …, m-1}
Properties

•(a + b) mod m = ( (a mod m) + (b mod m) ) mod m


•(a - b) mod m = ( (a mod m) – (b mod m) ) mod m
•(a x b) mod m = ( (a mod m) x (b mod m) ) mod m
•(a / b) mod m ≠ ( (a mod m) / (b mod m) ) mod m
•(a / b) mod m = ( (a mod m) x (b-1 mod m) ) mod m
Modular Inverse

We know that in our Decimal Number System,


a x (1/a) = 1 is true for all ‘a’ except 0

Here (1/a) is called the inverse of ‘a’


Similarly, we have a modular inverse for the Modular
Number System.
Modular Inverse is defined as :

The Modular Inverse of ‘a’ is ‘a -1 ’ , if and only if ,

(a x a-1 ) mod m = 1 mod m

Unlike the Decimal Number System, Modular Inverse


may or may not exist for all the elements.

Modular Inverse exists only when GCD (m , a) = 1


Fermat’s Little Theorem

Fermat’s Little Theorem states that for every prime ‘m’ ,


am-1 ≡ 1 mod m
Finding Modular Inverse

Using Fermat’s Little Theorem, we can find the modular inverse


for all prime ‘m’ :
Let ‘a’ belong to Zm

Then, a-1 = am-2


Since, ( a * am-2 ) = am-1 ≡ 1 mod m
For any general ‘m’, i.e., prime or non-prime,
We use a method called the
Extended Euclidean Algorithm :

https://cp-algorithms.com/algebra/module-inverse.html
https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
Binary Exponentiation

Binary exponentiation is a trick which allows us to


calculate ‘ an ’ using only O(log⁡n) multiplications instead
of O(n) multiplications required by the naive approach.
In Binary Exponentiation, instead of simply multiplying
‘a’ n times, we use the following idea :
Iterative Approach
Recursive Approach
Question – CSES : Binomial
Coefficients

https://cses.fi/problemset/task/1079
Greatest Common Divisor (GCD)

GCD or HCF (Highest common factor ) of two numbers is the


largest number that number that divides both the numbers.

GCD (42,49) = 7
GCD(0,5) =5
Greatest Common Divisor (GCD)

The naive approach is to iterate from and


return the first number that divides both of them.
Complexity:
Greatest Common Divisor (GCD)
Important Property: If a<b , we have

Let g1 be the gcd of a,b and g2 be the gcd of (a,b-a);


Greatest Common Divisor (GCD)

gcd(a,b) = gcd(a,b-a);
gcd(a,b-a) = gcd(a,b-2*a);
gcd(a,b-2*a) =gcd(a,b-3*a);
……
gcd(a,b-(k-1)*a) = gcd(a,b-k*a)
So gcd(a,b) = gcd(a,b-k*a) =gcd(a,b%a)
Greatest Common Divisor (GCD)

Complexity :
Sieve of Eratosthenes
Suppose, we have to find all prime numbers in the range
?
Naive Approach : Iterate from i =2 to i = N and for each i
calculate whether i is prime or not.
Complexity :
Can we perform better ?
Algorithm

● We begin with a boolean array of length N+1. We initialise


every element from with true.
● For every number in range , we mark all its proper
multiples as false.
● We keep on repeating this until all numbers are processed
in the array.
Complexity

● Complexity :

The outer loop runs from p=2 tp p=n


And in each iteration the inner loop runs n/p times.
So, Complexity:
Complexity

● Complexity :

The outer loop runs from p=2 tp p=n


And in each iteration the inner loop runs n/p times.
So, Complexity:
Complexity

So overall complexity:
Algorithm

● We begin with a boolean array of length N+1. We initialise


every element from with true.
● We begin with marking all proper multiples of 2 as
composite (false).
● We then move to next number which isn’t marked
composite (say X) and then mark all proper multiples X as
composite.
● We keep on repeating this until all numbers are processed
in the array.
Complexity

● Complexity :

The proof of this depends on prime number theorem (which


states the distribution of the prime numbers)
The rigorous proof can be read from here.
Calculating Prime Factorization
of a Range of Numbers

● Suppose we have to calculate the factorization of all


numbers in the range
Question - Codeforces : 1512G

https://codeforces.com/contest/1512/problem/G
Resources

● https://usaco.guide/gold/modular
● https://usaco.guide/gold/divisibility
● https://cp-algorithms.com/algebra/primality_tests.html
● https://cp-algorithms.com/algebra/sieve-of-eratosthenes.html
Practice Contest for Number Theory

You might also like