Master Number Theory
A Powerful tool for competitive programmers
Number Theory is a branch of Mathematics which deals with the properties and relationships of Positive Numbers and arithmetic operations based on them. Used for counting, numbers have captivated mathematicians throught the history. By mastering key concepts in the number theory, you will gain a significant edge in tackling the problems involving divisibility, primality, modular arithmetic and more.
Fundamental concepts of Number Theory
- The Peono Axioms
- GCD and LCM
- Prime Numbers
- Prime Factors
- Modular Arithmetic
- Integer Factorization
- Chinese Remainder Theorem
- Euclidean Algorithm
The Peano Axioms
- 0 ∈ N ; 0 is a natural number
- If x ∈ N ⇒ S(x) ∈ N - Every natural number has a successor, which is also a natural number.
- If n ∈ N ; S(n) ≠ 0 - If n ∈ N, then the successor of n cannot be 0.
- ∀ a, b ∈ N; if S(a) = S(b) ⇒ a = b - Different Natural numbers have different successors.
- If 0 belongs to set V and V contains the successor of every number in S then, S contains every natural number. This axiom is popularlu known as ‘Principle of Induction
- Further Reading - Wikipedia
GCD and LCM
Greatest Common Divisor (GCD)
GCD of two or more numbers is defined as the largest positive integer that divides them, exactly.
Least Common Factor (LCM)
LCM of two or more numbers is defined as the smallest positive integer that is divisible by each of the numbers.
GCD and LCM are related by a very simple equation: LCM(a,b) GCD(a,b) = ab. This gives a very fast way to calculate LCM of two numbers
Prime Numbers
A prime number is definde as a natural number greater than one that has no positive divisors other than 1 and itself. Prime Numbers are central to number theory because of the funcamental theorem of Arithmetic which states that 1 is either prime itself or can be factorized as product of primes. Some of the prime numbers include 2,3,5,7,11,13,…..etc.
- Further reading : Sieve of Eratosthenes
Prime Factors
A Prime Factor of a number is a factor of the number that is only divisible by 1 and itself. Example : The Prime factors of 15 and 3 are 5 ( because 3*5 =15 and 2 and 5 are prime numbers)
- Further Reading - geeksforgeeks
Modular Arithmetic
Modular Arithmetic deals with remainders after division. We write a ≡ b (mod n) to signify that a and b leave the same remainder when divided by n (the modulus).
Modular Addition
(a + b) mod m = ((a mod m) + (b mod m)) mod m
Modular Multiplication
(a x b) mod m = ((a mod m) x (b mod m)) mod m
Modular Division
(a / b) mod m is not equal to ((a mod m) / (b mod m)) mod m.
Modular Exponentiation (Fast Exponentiation)
Efficiently compute large powers under modulo
abmod m
Modular Inverse
The modular inverse of a mod m exists only if a and m are relatively prime i.e. gcd(a, m) = 1.
- Further Reading - Quotient-Remainder Theorem, Modular Arithmetic
Integer Factorization
- Further Reading - Wikipedia
Euclidean Algorithm
The Euclidean algorithm can be used to find the greatest common divisor of two positive integers. GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common prime factors.
- GCD of two numbers doesn't change even if we subtract smaller number from the larger number. So if we keep subtracting repeatedly the larger of two, we end up with GCD.
- Now instead of subtraction, if we divide the smaller number, the algorithm stops when we find the remainder 0.
- Further Reading - Euclidean Algorithm
Chinese Remainder Theorem
x = a1 mod m1
x = a2 mod m2
...
x = an mod mn
Where all pairs of m1, m2, ..., mn are coprimes.
CRT states that the above equations have a unique solution of the moduli are relatively prime.
x = (a1M1M1-1 + a2M2M2-1 + ... + anMnMn-1)mod M
- Further Reading - geeksforgeeks
Practice Makes Perfect
The key to master number theory lies in consistent practice. Here are some resourses to help you hone your skills.
- Basic Number Theory-1 : Hackerearth
- Basic Number Theory-2 : Hackerearth
- Number Theory : Leetcode
- Number Theory : CodeForces
By investing time and effort into learning number theory, you will equip yourself with a powerful arsenal of techniques to tackle wide range of problems in competitive programming.