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.
Input: M = 9, K = 1
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
- 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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count of Octal numbers upto N digits
- Count of n digit numbers whose sum of digits equals to given sum
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count numbers upto N which are both perfect square and perfect cube
- Count of all values of N in [L, R] such that count of primes upto N is also prime
- Sum of GCD of all numbers upto N with N itself
- Sum of the numbers upto N that are divisible by 2 or 5
- Count of interesting primes upto N
- Sum of Fibonacci numbers at even indexes upto N terms
- Minimise N such that sum of count of all factors upto N is greater than or equal to X
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Program to print pentatope numbers upto Nth term
- Program to print tetrahedral numbers upto Nth term
- Program to count number of distinct Squares and Cubes upto N
- Count rows/columns with sum equals to diagonal sum
- Count triplet of indices (i, j, k) such that XOR of elements between [i, j) equals [j, k]
- Print all n-digit numbers whose sum of digits equals to given sum
- Check if count of Alphabets and count of Numbers are equal in the given String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.