You are on page 1of 47

Number Theory

Prepared by :
Rami Zouari
Yessine Jallouli
Number Theory?

Number Theory, branch of mathematics concerned with properties of the


positive integers.
It is among the oldest and most natural of mathematical pursuits.
What’s interesting about numbers?
- Number theory has always fascinated amateurs as well as professional
mathematicians.

- In contrast to other branches of mathematics, many of the problems and


theorems of number theory can be understood by laypersons.

- But be aware that solutions can be too advanced for such problems!!!
“Mathematics is the queen of the sciences and
number theory is the queen of mathematics.”
Carl Friedrich Gauss
Why should I bother?
Until the 20th century, number theory was largely seen as only an “art”, with no real
world applications.
With the advent of computers, large breakthroughs in computer science took
advantage of number theory problems
● Security System like in banking securities
● Coding theory
● Making of modular designs
● Memory management system
● Authentication system
● Crypto-currencies
Applications of Number Theory

Number Theory is a crucial tool for:

- Modern cryptography: (RSA + other algorithms)


- Computer Science: (Hashing)
- Optimizing algorithms: (FFT on cyclic fields)
Plan

● GCD / LCM
● Modular arithmetic
● Linear Congruential Equations
● Sieving
● Primality Test
● Factorisation
● Important Arithmetic Functions
● Chinese Remainder Theorem
Greatest common divisors :

Definition :
The greatest common divisors of two or more integers is the largest positive integer that
divides each of the integers without a remainder.

How to calculate the GCD of two numbers :

1s method: Brute Forces: Find such integer by checking all number in {1 .. min(n,m) }
Complexity: O(min(n,m))

2nd method : Factorization : The GCD is the product of the common prime factors raised to
the lowest exponent.

Complexity : Depending on factorization method (We’ll see them later)


3rd method : Euclidean algorithm.
This algorithm is based on the principle that the GCD of two numbers does not change if the larger
number is replaced by its difference with the smaller number. This algorithm reduces the problem of
finding the GCD of two numbers to the problem of finding the GCD of the smaller number and the
remainder of the larger number divided by the smaller number.
More Formally : GCD(a,b) = GCD(b, a%b)

C++ implementation :

Time complexity : O(log(min(a,b))


Lowest Common Multiple :

Definition :
The least common multiple (LCM) of two or more integers is the smallest positive integer that is a multiple
of each of the integers.

How to calculate the LCM of two numbers :

The LCM can be found using the following formula: LCM(a,b) = (a * b) / GCD(a,b)
Problem :
Modular Arithmetic

In mathematics, modular arithmetic is a system of arithmetic for


integers, where numbers "wrap around" when reaching a certain
value, called the modulus.
Modular Arithmetic System

All arithmetic operations (addition, subtraction, multiplication, division, etc.) are


performed with respect to a fixed integer, called the modulus.

When an operation results in a value that is greater than or equal to the modulus,
the value is "reduced" by subtracting/adding the modulus as many times as
necessary to obtain a positive value less than the modulus.

For convenience, when a number a is written as i in an expression, it means that


we will use modular arithmetic.
Addition

It is defined as follow:

For example, if m=7:


Implementation

Addition : (a+b)%m = (a%m + b%m)%m

Complexity : O(1)

Warning : in C/C++ don’t use a+= b%m, that gives a = a + b%m, instead, you should use a = (a+b)%m
Negation / Additive Inverse

It is defined as follow:

For example, if m=13:


Substraction

It is defined as follow:

For example, if m=3:


Multiplication
The multiplication is defined as follow:

For example, if m=5:


Important:
Problem:

Given n integers output the product of all elements modulo m

Bad implementation Good implementation :

Be aware of integer
overflow when multiplying!!!
Power
The modular power can be thought as a repeated multiplication modulo m:

For example, if m=3:


Power Implementation
Inversion

When the modulus is m, the modular inverse, if it exists, is defined as follow:

For m=5, the modular inverse of 3 is 2 because:


Existence and Calculation of the Inverse
Given a modulus m, the modular inverse of an integer a exists if and only if:

It can be calculated using one of the three methods:


- Fermat Theorem
- Extended Euclidean Algorithm
- Euler-Fermat Theorem/ Euler Totient Theorem
Fermat Theorem

It does only work on prime moduluses, that is when m is prime:

The term s is calculated using the fast modular pow


Extended Euclidean Algorithm
It is an extension to the Euclidean algorithm, and computes, in addition to the
greatest common divisor (gcd) of integers a and b, also the coefficients of
Bézout's identity, which are integers x and y such that:
EGCD Implementation

Complexity:
Linear Diophantine Equation
A linear diophantine equation is an equation of the following form:

Where the unknowns are x and y

It can be solved as follow:


- Solve ax+by=1 using the extended Euclidean algorithm
- Multiply both solutions by c
Linear Congruence Equation
A linear congruence equation is an equation of the form:

Where the unknown is x.

It can be solved as follow:


- Transform the problem to a diophantine equation: ax+km=b
- Solve it for x and k
Normal sieve :
Problem : Given n, how many prime numbers exist between 1 and n.

Complexity : O(nlogn)
Primality test :

Problem : Check if an integer n is prime or not.


Method 1 : Normal sieve
Works only for n <= 10⁷
Complexity : O(nlogn) Precomputation, O(1) primality test
Method 2 : Normal primality test
Check if n has a divisor less or equal to its square root.
Method 3 : Fermat primality test :
- We want to test whether m is prime or not
- Let a be a positive integer
The test says that m is prime if:
Method 4 : Probabilistic Rabin-Miller
- We want to test whether m is prime or not
- We will decompose m-1 as follow:

- We impose that q is odd


- Let a be a positive integer
The test says that m is prime if:
Advanced methods of primality test :
● Deterministic Rabin-Miller
● AKS primality test (Agrawal-Kayal-Saxena)
Sieve of Eratosthenes
Problem : Calculate the smallest divisor of each number from 2 to n :

Complexity : O(nloglogn)
How to factorize any number using
sieve of Eratosthenes ?
Factorization :
Problem : Given n, output its prime factorization

Complexity : O(log(n))
Advanced methods of factorization :

● Rho factorization
● Pollard p-1
● Quadratic sieve
● General sieve
Counting Divisors
Problem:

Calculate the number of divisors of n modulo 10⁹+7

Constraints:
- 1 <= m <= 10⁶
- 1 <= pi <= 10⁹
- 1 <= ai <= 10⁹
Sum of Divisors
Problem:

Calculate the following sum:

Constraints:
- 1 <= m <= 10⁶
- 1 <= pi <= 10⁹
- 1 <= ai <= 10⁹
Precompute all divisors :
Problem : For each i from 1 to n : precompute the divisors of i.
Euler Totient
Problem:
Given n, how many positive integers x less than n and co-prime with n.

Constraints:
- 1 <= n <= 10⁶
Properties of Euler’s Totient
Multiplicative:
If a and b are co-prime, then

Totient of prime Power:

General Formula
Sieving Euler Totient’s values
System of 2 Linear Congruence Equations
It is a system of the following equations:
Chinese Remainder Theorem
The chinese remainder theorem is a method for solving
the following system of congruences:

You might also like