Number Theory Basics
Basic number theory knowledge for Competitive Programming
Number Theory Basics
Number Theory is one of the important topics frequently found in competitions. Many problems require knowledge of integers, divisibility, and prime numbers.
Divisibility
Integer is divisible by , written as , means for some integer .
// Check divisibility
bool isDivisible(int a, int b) {
return a % b == 0;
}
Greatest Common Divisor (GCD)
GCD is the largest number that divides both numbers.
Euclidean Algorithm
// Euclidean method O(log(min(a,b)))
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// Or using recursion
int gcdRecursive(int a, int b) {
if (b == 0) return a;
return gcdRecursive(b, a % b);
}
// C++ has __gcd and std::gcd (C++17)
#include <algorithm>
int g = __gcd(12, 18); // 6
How It Works
Example: Find
Least Common Multiple (LCM)
LCM is the smallest number that is divisible by both numbers.
// Formula: lcm(a, b) = (a * b) / gcd(a, b)
long long lcm(long long a, long long b) {
return a / __gcd(a, b) * b; // Divide before multiply to prevent overflow
}
// C++17 has std::lcm
#include <numeric>
long long l = std::lcm(12, 18); // 36
⚠️ Warning: Be careful of overflow when multiplying . Divide by gcd before multiplying.
Extended Euclidean Algorithm
Extended GCD finds values such that
// Returns gcd and finds x, y such that ax + by = gcd(a,b)
int extGcd(int a, int b, int &x, int &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
int x1, y1;
int g = extGcd(b, a % b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
return g;
}
Usage Example
int x, y;
int g = extGcd(35, 15, x, y);
// g = 5, x = 1, y = -2
// Verify: 35 * 1 + 15 * (-2) = 35 - 30 = 5 ✓
Factorization
Trial Division
// Find all prime factors O(sqrt(n))
vector<pair<int, int>> factorize(int n) {
vector<pair<int, int>> factors;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
int count = 0;
while (n % i == 0) {
n /= i;
count++;
}
factors.push_back({i, count});
}
}
if (n > 1) {
factors.push_back({n, 1});
}
return factors;
}
// Example: factorize(60)
// Returns: [(2, 2), (3, 1), (5, 1)]
// 60 = 2² × 3 × 5
Number of Divisors
If
Number of divisors of is
int countDivisors(int n) {
int count = 1;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
int exp = 0;
while (n % i == 0) {
n /= i;
exp++;
}
count *= (exp + 1);
}
}
if (n > 1) count *= 2;
return count;
}
Sum of Divisors
long long sumDivisors(int n) {
long long sum = 1;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
long long p = i;
long long term = 1;
while (n % i == 0) {
n /= i;
term = term * p + 1;
}
sum *= term;
}
}
if (n > 1) sum *= (1 + n);
return sum;
}
Finding All Divisors
// O(sqrt(n))
vector<int> getDivisors(int n) {
vector<int> divisors;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
divisors.push_back(i);
if (i != n / i) {
divisors.push_back(n / i);
}
}
}
sort(divisors.begin(), divisors.end());
return divisors;
}
// Example: getDivisors(12)
// Returns: [1, 2, 3, 4, 6, 12]
Euler’s Totient Function (φ)
is the count of positive integers less than or equal to that are coprime with (gcd = 1)
Formula
int phi(int n) {
int result = n;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0) n /= i;
result -= result / i;
}
}
if (n > 1) result -= result / n;
return result;
}
Important Properties
- for prime
- when
Example Problem
Problem: Count pairs (i, j) where gcd(i, j) = 1
// Given n, count pairs (i, j) where 1 ≤ i < j ≤ n and gcd(i, j) = 1
long long countCoprimePairs(int n) {
long long count = 0;
for (int i = 1; i <= n; i++) {
count += phi(i);
}
return count - 1; // Subtract pair (1, 1)
}
Summary
| Function | Formula/Method | Complexity |
|---|---|---|
| GCD | Euclidean | |
| LCM | ||
| Factorization | Trial Division | |
| Count Divisors | ||
| Euler’s φ |
💡 Tip: In C++, you can use
__gcd()for GCD andstd::lcm()(C++17) for LCM directly.