Given two integers M and K, the task is to count the number of integers between [0, M] such that GCD of that integer with M equals to K.
Examples:
Input: M = 9, K = 1
Output: 6
Explanation:
The possible numbers such that when paired with 9, there GCD is 1, are 1, 2, 4, 5, 7, 8.Input: M = 10, K = 5
Output: 1
Approach:
- Integers having GCD K with M will be of the form K, 2K, 3K, …..and so on up to M.
- Let’s consider the coefficients of K i.e 1, 2, 3, 4…up to (M/K).
- Now we just have to find the count of such coefficients which have GCD with the number (M/K) = 1. So now problem reduces to find the number of integers between 1 to (M/K) having Gcd with (m/k) = 1.
- To find this we will use the Euler totient function of (M/K).
Below is the implementation of the above approach:
C++
// C++ program to Count of numbers // between 0 to M which have GCD // with M equals to K. #include <bits/stdc++.h> using namespace std; // Function to calculate GCD // using euler totient function int EulerTotientFunction(int limit) { int copy = limit; // Finding the prime factors of // limit to calculate it's // euler totient function vector<int> primes; for (int i = 2; i * i <= limit; i++) { if (limit % i == 0) { while (limit % i == 0) { limit /= i; } primes.push_back(i); } } if (limit >= 2) { primes.push_back(limit); } // Calculating the euler totien // function of (m/k) int ans = copy; for (auto it : primes) { ans = (ans / it) * (it - 1); } return ans; } // Function print the count of // numbers whose GCD with M // equals to K void CountGCD(int m, int k) { if (m % k != 0) { // GCD of m with any integer // cannot be equal to k cout << 0 << endl; return; } if (m == k) { // 0 and m itself will be // the only valid integers cout << 2 << endl; return; } // Finding the number upto which // coefficient of k can come int limit = m / k; int ans = EulerTotientFunction(limit); cout << ans << endl; } // Driver code int main() { int M = 9; int K = 1; CountGCD(M, K); return 0; } |
Java
// Java program to Count of numbers // between 0 to M which have GCD // with M equals to K. import java.util.*; class GFG{ // Function to calculate GCD // using euler totient function static int EulerTotientFunction(int limit) { int copy = limit; // Finding the prime factors of // limit to calculate it's // euler totient function Vector<Integer> primes = new Vector<Integer>(); for (int i = 2; i * i <= limit; i++) { if (limit % i == 0) { while (limit % i == 0) { limit /= i; } primes.add(i); } } if (limit >= 2) { primes.add(limit); } // Calculating the euler totien // function of (m/k) int ans = copy; for (int it : primes) { ans = (ans / it) * (it - 1); } return ans; } // Function print the count of // numbers whose GCD with M // equals to K static void CountGCD(int m, int k) { if (m % k != 0) { // GCD of m with any integer // cannot be equal to k System.out.print(0 +"\n"); return; } if (m == k) { // 0 and m itself will be // the only valid integers System.out.print(2 +"\n"); return; } // Finding the number upto which // coefficient of k can come int limit = m / k; int ans = EulerTotientFunction(limit); System.out.print(ans +"\n"); } // Driver code public static void main(String[] args) { int M = 9; int K = 1; CountGCD(M, K); } } // This code is contributed by sapnasingh4991 |
Python3
# Python3 program to Count of numbers # between 0 to M which have GCD # with M equals to K. # Function to calculate GCD # using euler totient function def EulerTotientFunction(limit): copy = limit # Finding the prime factors of # limit to calculate it's # euler totient function primes = [] for i in range(2, limit + 1): if i * i > limit: break if (limit % i == 0): while (limit % i == 0): limit //= i primes.append(i) if (limit >= 2): primes.append(limit) # Calculating the euler totien # function of (m//k) ans = copy for it in primes: ans = (ans // it) * (it - 1) return ans # Function print the count of # numbers whose GCD with M # equals to K def CountGCD(m, k): if (m % k != 0): # GCD of m with any integer # cannot be equal to k print(0) return if (m == k): # 0 and m itself will be # the only valid integers print(2) return # Finding the number upto which # coefficient of k can come limit = m // k ans = EulerTotientFunction(limit) print(ans) # Driver code if __name__ == '__main__': M = 9 K = 1 CountGCD(M, K) # This code is contributed by mohit kumar 29 |
C#
// C# program to Count of numbers // between 0 to M which have GCD // with M equals to K. using System; using System.Collections.Generic; class GFG{ // Function to calculate GCD // using euler totient function static int EulerTotientFunction(int limit) { int copy = limit; // Finding the prime factors of // limit to calculate it's // euler totient function List<int> primes = new List<int>(); for (int i = 2; i * i <= limit; i++) { if (limit % i == 0) { while (limit % i == 0) { limit /= i; } primes.Add(i); } } if (limit >= 2) { primes.Add(limit); } // Calculating the euler totien // function of (m/k) int ans = copy; foreach (int it in primes) { ans = (ans / it) * (it - 1); } return ans; } // Function print the count of // numbers whose GCD with M // equals to K static void CountGCD(int m, int k) { if (m % k != 0) { // GCD of m with any integer // cannot be equal to k Console.Write(0 + "\n"); return; } if (m == k) { // 0 and m itself will be // the only valid integers Console.Write(2 + "\n"); return; } // Finding the number upto which // coefficient of k can come int limit = m / k; int ans = EulerTotientFunction(limit); Console.Write(ans + "\n"); } // Driver code public static void Main(String[] args) { int M = 9; int K = 1; CountGCD(M, K); } } // This code is contributed by PrinciRaj1992 |
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.
Recommended Posts:
- Sum of GCD of all numbers upto N with N itself
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Series with largest GCD and sum equals to n
- Find the number of pairs such that their gcd is equals to 1
- Print any pair of integers with sum of GCD and LCM equals to N
- Count of Octal numbers upto N digits
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Count pairs of natural numbers with GCD equal to given number
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Program to count number of distinct Squares and Cubes upto N
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count rows/columns with sum equals to diagonal sum
- GCD and Fibonacci Numbers
- GCD of two numbers formed by n repeating x and y times
- GCD of more than two (or array) numbers
- Finding LCM of more than two (or array) numbers without using GCD
- Program to find GCD or HCF of two numbers
- Queries for GCD of all numbers of an array except elements in a given range
- C++ Program for GCD of more than two (or array) numbers
- Java Program for GCD of more than two (or array) numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

