Number Theory
- Srivaths P
Goal:
To learn:
• Basic Primality Testing
• Sieve of Eratosthenes
• Prime Factorization
• Basic Modular Arithmetic
Primality Testing
A primality test is an algorithm for determining whether a number is
prime or composite.
There are many algorithms for primality testing such as:
• Brute-Force
• Square Root method
• Using Sieve (precomputation)
Primality Testing – Code
Brute-Force: Sqrt method:
bool is_prime(int n) { bool is_prime(int n) {
for (int i = 2; i < n; i++) for (int i = 2; i*i <= n; i++)
if (n % i == 0) if (n % i == 0)
return false; return false;
return n > 1; return n > 1;
} }
Square Root Method Proof
If the number 𝑁 given is not a prime, then it must be possible to factor
into two values 𝐴 and 𝐵 such that 𝐴𝐵 = 𝑁.
Here, it is impossible for both 𝐴 and 𝐵 to be greater than 𝑁, as in that
case the product will be greater than 𝑁.
Therefore, if the number is not a prime, it must have atleast one factor
less than or equal to the square root of the number.
Sieve of Eratosthenes
Sieve of Eratosthenes can find all the prime numbers upto a given limit
in 𝑂(𝑁 log log 𝑁) time complexity.
Any composite number 𝐶 must have a prime factor 𝑃 such that 𝑃 < 𝐶.
We will repeatedly find the first number that does not have any prime
factors, as that number must be prime. Then we will mark the multiples
of the number as composite.
Sieve of Eratosthenes – Code
void sieve(int n) {
bool primes[n+1];
fill(primes, primes+n+1, true);
primes[0] = primes[1] = false;
for (int i = 2; i*i <= n; i++) {
if (primes[i])
for (int j = i*i; j <= n; j += i)
primes[j] = false;
}
}
Sieve of Eratosthenes – Time Complexity
The Prime Harmonic Series is the infinite sum of the reciprocals of all
prime numbers. [Link]
1 1 1 1
+ + + +⋯
2 3 5 7
The above sum comes out as log log 𝑁 when the primes are less than 𝑁.
The sieve takes approximately 2𝑁 iterations for 𝑁 = 107 .
Prime Factorization
Prime Factorization is finding all the prime factors of a given number.
There are multiple ways to factor primes, such as:
• Trial Division
• Wheel Factorization
• Sieve method (precomputation)
Prime Factorization – Trial Division
Trial division is the most basic method of prime factorization.
We will use the fact that the smallest divisor of any number 𝑁 is prime,
and it will be less than 𝑁.
𝑞 𝑞 𝑞 𝑞
𝑁 can be represented as 𝑝0 0 ∗ 𝑝1 1 ∗ 𝑝2 2 ∗ ⋯ ∗ 𝑝𝑘 𝑘 where 𝑝𝑖 is prime.
Let us assume that the prime array is sorted.
We will iterate through the range 2, 𝑁 to find 𝑝0 first, then 𝑝1 , etc.
Prime Factorization – Trial Division Code
vector<int> factor(int n) {
vector<int> facts;
for (long long d = 2; d * d <= n; d++) {
while (n % d == 0) {
facts.push_back(d);
n /= d;
}
}
if (n > 1)
facts.push_back(n);
return facts;
}
Modular Arithmetic
Modular Arithmetic is arithmetic operations involving taking the
modulo with some modulus. It is usually given in problem statements
so that the solution doesn’t overflow in case the answer is huge.
A few operations involving modulo are:
• Addition
• Subtraction
• Multiplication
• Division
Modular Arithmetic
Modular addition:
𝐴 + 𝐵 % 𝑀 = 𝐴 % 𝑀 + 𝐵 % 𝑀 % 𝑀.
Modular subtraction (be very careful when using C++):
𝐴 − 𝐵 % 𝑀 = 𝐴 % 𝑀 − 𝐵 % 𝑀 % 𝑀.
𝐴 − 𝐵 % 𝑀 = ((𝐴 % 𝑀 − 𝐵 % 𝑀 % 𝑀) + 𝑀) % 𝑀. (in C++)
Modular multiplication:
𝐴 ∗ 𝐵 % 𝑀 = (𝐴 % 𝑀) ∗ (𝐵 % 𝑀) % 𝑀.
Problems
• [Link]
• [Link]
• [Link]
• [Link]
Resources
• [Link]
• [Link]
• [Link] (advanced)
• [Link] (modular arithmetic basics)