Given a number N. The task is to find the number of unordered coprime pairs of integers from 1 to N. There can be multiple queries.
Input: 3 Output: 4 (1, 1), (1, 2), (1, 3), (2, 3) Input: 4 Output: 6 (1, 1), (1, 2), (1, 3), (1, 4), (2, 3), (3, 4)
Approach: Here Euler’s Totient Function will be helpful. Euler’s totient function denoted as phi(N), is an arithmetic function that counts the positive integers less than or equal to N that are relatively prime to N.
The idea is to use the following properties of Euler Totient function i.e.
- The formula basically says that the value of Φ(n) is equal to n multiplied by product of (1 – 1/p) for all prime factors p of n. For example value of Φ(6) = 6 * (1-1/2) * (1 – 1/3) = 2.
- For a prime number p, Φ(p) is p-1. For example Φ(5) is 4, Φ(7) is 6 and Φ(13) is 12. This is obvious, gcd of all numbers from 1 to p-1 will be 1 because p is a prime.
Now, find the sum of all phi(x) for all i between 1 to N using prefix sum method. Using this, one can answer in o(1) time.
Below is the implementation of above approach.
Number of unordered coprime pairs of integers from 1 to 3 are 4 Number of unordered coprime pairs of integers from 1 to 4 are 6
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count all pairs of divisors of a number N whose sum is coprime with N
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Count of unordered pairs (x, y) of Array which satisfy given equation
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- XOR of pairwise sum of every unordered pairs in an array
- Count of integers up to N which are non divisors and non coprime with N
- Range Queries to count the number of even parity values with updates
- Count of elements having odd number of divisors in index range [L, R] for Q queries
- Count number of pairs (i, j) such that arr[i] * arr[j] = arr[i] + arr[j]
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B
- Coprime divisors of a number
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Count pairs with sum as a prime number and less than n
- Count pairs with Bitwise AND as ODD number
- Given two arrays count all pairs whose sum is an odd number
- Largest number less than or equal to N/2 which is coprime to N
- Count number of ordered pairs with Even and Odd Product
- Count number of ordered pairs with Even and Odd Sums
- Count number of pairs with positive sum in an array
- Count number of pairs in array having sum divisible by K | SET 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.