Count of numbers upto M with GCD equals to K when paired with M
Last Updated :
18 Nov, 2021
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 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 the 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++
#include <bits/stdc++.h>
using namespace std;
int EulerTotientFunction( int limit)
{
int copy = limit;
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);
}
int ans = copy;
for ( auto it : primes) {
ans = (ans / it) * (it - 1);
}
return ans;
}
void CountGCD( int m, int k)
{
if (m % k != 0) {
cout << 0 << endl;
return ;
}
if (m == k) {
cout << 2 << endl;
return ;
}
int limit = m / k;
int ans = EulerTotientFunction(limit);
cout << ans << endl;
}
int main()
{
int M = 9;
int K = 1;
CountGCD(M, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int EulerTotientFunction( int limit)
{
int copy = limit;
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);
}
int ans = copy;
for ( int it : primes) {
ans = (ans / it) * (it - 1 );
}
return ans;
}
static void CountGCD( int m, int k)
{
if (m % k != 0 ) {
System.out.print( 0 + "\n" );
return ;
}
if (m == k) {
System.out.print( 2 + "\n" );
return ;
}
int limit = m / k;
int ans = EulerTotientFunction(limit);
System.out.print(ans + "\n" );
}
public static void main(String[] args)
{
int M = 9 ;
int K = 1 ;
CountGCD(M, K);
}
}
|
Python3
def EulerTotientFunction(limit):
copy = limit
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)
ans = copy
for it in primes:
ans = (ans / / it) * (it - 1 )
return ans
def CountGCD(m, k):
if (m % k ! = 0 ):
print ( 0 )
return
if (m = = k):
print ( 2 )
return
limit = m / / k
ans = EulerTotientFunction(limit)
print (ans)
if __name__ = = '__main__' :
M = 9
K = 1
CountGCD(M, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int EulerTotientFunction( int limit)
{
int copy = limit;
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);
}
int ans = copy;
foreach ( int it in primes)
{
ans = (ans / it) * (it - 1);
}
return ans;
}
static void CountGCD( int m, int k)
{
if (m % k != 0)
{
Console.Write(0 + "\n" );
return ;
}
if (m == k)
{
Console.Write(2 + "\n" );
return ;
}
int limit = m / k;
int ans = EulerTotientFunction(limit);
Console.Write(ans + "\n" );
}
public static void Main(String[] args)
{
int M = 9;
int K = 1;
CountGCD(M, K);
}
}
|
Javascript
<script>
function EulerTotientFunction(limit)
{
let copy = limit;
let primes = [];
for (let i = 2; i * i <= limit; i++) {
if (limit % i == 0) {
while (limit % i == 0) {
limit /= i;
}
primes.push(i);
}
}
if (limit >= 2) {
primes.push(limit);
}
let ans = copy;
for (let it in primes) {
ans = (ans / primes[it]) * (primes[it] - 1);
}
return ans;
}
function CountGCD(m, k)
{
if (m % k != 0) {
document.write(0 + "<br/>" );
return ;
}
if (m == k) {
document.write(2 + "<br/>" );
return ;
}
let limit = Math.floor(m / k);
let ans = EulerTotientFunction(limit);
document.write(ans + "\n" );
}
let M = 9;
let K = 1;
CountGCD(M, K);
</script>
|
Time Complexity: O((m / k)1/2 * log(m/k))
Auxiliary Space: O((m / k)1/2)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...