Number Theory
GCD and LCM
GCD of say n numbers is the greatest number which divides each of the n numbers. LCM of n numbers is the smallest number that can be divided by each of the n numbers. These definitions are well known to most of the readers. It is also quite obvious to see that
Task: Find GCD of two numbers
Approach 1:
Iterate over all positive integers less than
Approach 2 (Euclidean Algorithm):
Suppose we have two positive integers
-
Divide
by . You get a quotient and a remainder . -
If
, then we’re done. The GCD of is . -
Else, replace
with and with and go to Step 1.
In other words, you do a series of divisions until you have a zero remainder
Let’s now find the GCD of
A sample C++ implementation of the Euclidean algorithm:
This algorithm works in __gcd(int a, int b)
(defined in header <algorithm>
) or std::gcd(int a, int b)
(defined in header <numeric>
).
Resources
-
Check this link for a more detailed explanation on how Euclidean Algorithm works and why it runs in
time. -
Check out this link to have a visual understanding of what Euclidean Algorithm is
-
Check geeksforgeeks for implementations of Euclidean Algorithm in different languages.
Problems
Additional Resources
-
Check out the Extended Euclidean Algorithm (Not really used much in CP)
- Go through this link to have a better understanding of the Extended Euclidean Algorithm.
- Check out this link for more numerical examples.
- Problems on EEA: Euclid Problem, Border.
-
Additional Problems on GCD
- Mike and gcd problem
- Vasya’s Function
- Optimal Denomination (Note that GCD can crop up in literally any problem XD)
- HUGE GCD
- (add more here)
Primes
A prime number (or prime in short) is a positive integer having exactly two factors :
Task: Check whether a number is a prime
Approach 1:
Brute force. Iterate from
Approach 2:
If you’re smart enough, you’ll know that anything more than
Approach 3:
If
Sample implementation:
There are also some more primality tests (Fermat’s Primality test, Miller-Rabin Primality test, etc), but they are only probabilistic primality tests. For example, Fermat’s Primality test would classify the number
So now we have an algorithm to check whether a number is a prime. How about we try to find all primes till
Task: Find all primes till
Approach 1:
Iterate from checkPrime(i)
function defined above that works in
Approach 2 (Sieve of Eratosthenes){#approach-soe}:
This technique is used by most elementary/middle schoolers to count number of primes less than
It also turns out that once we know that
Here’s a sample implementation of SoE in C++:
This algorithm works in
Resources
- Check out this video on SoE from Khan Academy.
- Check out this page to see a detailed explanation and an implementation of SoE.
- Checkout GeeksforGeeks
Problems
- Conjecture of Paul Erdos
- Primal Fear
- Noldbach Problem
- Prime Matrix
- Remaining problems from here
We’ve seen a fast algorithm to find all primes in
Task: Find all primes in
Approach 1:
Use SoE to find all the primes till
Approach 1 seems to be a very good option. However, this approach cannot solve this problem in the given time limit. The problem here is that for large
Approach 2:
We now use our previous approach of finding whether a number is a prime in
We see that for finding all the primes till a natural number
Approach 3 (Segmented Sieve of Eratosthenes):
By striking off the composite numbers in
Resources
- Read more about SSoE here. Though the time complexity doesn’t reduce, the space complexity reduces considerably from
to . - http://translate.google.com/translate?hl=en&sl=auto&tl=en&u=https%3A%2F%2Fprodeportiva.wordpress.com%2F2013%2F02%2F11%2Fcriba-de-eratostenes%2F&sandbox=1