Count of numbers between L and R that are coprime with P
Find the count of numbers between L and R and are coprime with P.
Note: Two numbers X and Y are coprime with each other if their GCD is 1.
Examples:
Input: L = 1, R = 10, P = 16
Output: 5
Explanation: The numbers coprime with P are 1, 3, 5, 7, 9
Input: L = 10, R = 20, P = 15
Output: 6
Approach: The problem can be solved by using simple mathematical concept
Traverse from i = L to R and check if the GCD between i and P is 1 or not.
Follow the below steps to solve the problem:
- Initialize count = 0.
- Traverse in the range and check if the number is coprime with P.
- For checking coprime, check if the GCD of two numbers is 1 or not, if yes then it is else not.
- If yes add 1 to the count.
- After traversing the range, return the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isCoprime( int a, int b)
{
return __gcd(a, b) == 1;
}
int solve( int L, int R, int P)
{
int Cnt = 0;
for ( int i = L; i <= R; i++) {
Cnt += P % 2 == 0 && i % 2 == 0 ? 0
: isCoprime(i, P);
}
return Cnt;
}
int main()
{
int L = 1, R = 10, P = 16;
cout << solve(L, R, P) << endl;
L = 5, R = 50, P = 35;
cout << solve(L, R, P) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int gcd( int a, int b)
{
if (b == 0 ) {
return a;
}
else {
return gcd(b, Math.abs(a - b));
}
}
static boolean isCoprime( int a, int b)
{
return gcd(a, b) == 1 ;
}
static int solve( int L, int R, int P)
{
int Cnt = 0 ;
for ( int i = L; i <= R; i++) {
Cnt += (P % 2 == 0 && i % 2 == 0
? 0
: (isCoprime(i, P) ? 1 : 0 ));
}
return Cnt;
}
public static void main(String[] args)
{
int L = 1 , R = 10 , P = 16 ;
System.out.println(solve(L, R, P));
L = 5 ;
R = 50 ;
P = 35 ;
System.out.println(solve(L, R, P));
}
}
|
Python3
import math
def isCoprime(a,b):
return math.gcd(a,b) = = 1
def solve(L,R,P):
Cnt = 0
for i in range (L,R + 1 ):
Cnt = Cnt + ( 0 if ((P % 2 = = 0 ) and (i % 2 = = 0 )) else ( 1 if isCoprime(i,P) else 0 ))
return Cnt
L,R,P = 1 , 10 , 16
print (solve(L,R,P))
L,R,P = 5 , 50 , 35
print (solve(L,R,P))
|
C#
using System;
public class GFG {
static int gcd( int a, int b)
{
if (b == 0) {
return a;
}
else {
return gcd(b, Math.Abs(a - b));
}
}
static bool isCoprime( int a, int b)
{
return gcd(a, b) == 1;
}
static int solve( int L, int R, int P)
{
int Cnt = 0;
for ( int i = L; i <= R; i++) {
Cnt += (P % 2 == 0 && i % 2 == 0
? 0
: (isCoprime(i, P) ? 1 : 0));
}
return Cnt;
}
static public void Main()
{
int L = 1, R = 10, P = 16;
Console.WriteLine(solve(L, R, P));
L = 5;
R = 50;
P = 35;
Console.WriteLine(solve(L, R, P));
}
}
|
Javascript
function gcd(a,b)
{
if (b == 0) {
return a;
}
else {
return gcd(b, Math.abs(a - b));
}
}
function isCoprime(a,b)
{
return (gcd(a, b) == 1);
}
function solve(L,R,P)
{
let Cnt = 0;
for (let i = L; i <= R; i++) {
Cnt += P % 2 == 0 && i % 2 == 0 ? 0 : isCoprime(i, P);
}
return Cnt;
}
let L = 1, R = 10, P = 16;
console.log(solve(L, R, P));
let a = 5, b = 50, c = 35;
console.log(solve(a,b,c));
|
Time Complexity: O((R – L) * logN)
Auxiliary Space: O(1)
Last Updated :
13 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...