Skip to content
Related Articles
Open in App
Not now

Related Articles

Count of numbers between L and R that are coprime with P

Improve Article
Save Article
Like Article
  • Last Updated : 13 Dec, 2022
Improve Article
Save Article
Like Article

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++

// C++ code to implement the approach

#include <bits/stdc++.h>
using namespace std;

// Function to check if two numbers are coprime
bool isCoprime(int a, int b)
{
    return __gcd(a, b) == 1;
}

// Function to count the numbers which
// is coprime to P and in the range [L, R]
int solve(int L, int R, int P)
{
    // Initialize count
    int Cnt = 0;

    // Traverse in the range
    for (int i = L; i <= R; i++) {
        Cnt += P % 2 == 0 && i % 2 == 0 ? 0
                                        : isCoprime(i, P);
    }

    // Return count
    return Cnt;
}

// Driver Code
int main()
{
    // Testcase1
    int L = 1, R = 10, P = 16;
    cout << solve(L, R, P) << endl;

    // Testcase2
    L = 5, R = 50, P = 35;
    cout << solve(L, R, P) << endl;

    return 0;
}

Java

// Java code to implement the approach

import java.io.*;

class GFG {

    // Function to calculate GCD
    static int gcd(int a, int b)
    {
        if (b == 0) {
            return a;
        }
        else {
            return gcd(b, Math.abs(a - b));
        }
    }

    // Function to check if two numbers are coprime
    static boolean isCoprime(int a, int b)
    {
        return gcd(a, b) == 1;
    }

    // Function to count the numbers which
    // is coprime to P and in the range [L, R]
    static int solve(int L, int R, int P)
    {
        // Initialize count
        int Cnt = 0;

        // Traverse in the range
        for (int i = L; i <= R; i++) {
            Cnt += (P % 2 == 0 && i % 2 == 0
                        ? 0
                        : (isCoprime(i, P) ? 1 : 0));
        }

        // Return count
        return Cnt;
    }

    public static void main(String[] args)
    {
        // Testcase1
        int L = 1, R = 10, P = 16;
        System.out.println(solve(L, R, P));

        // Testcase2
        L = 5;
        R = 50;
        P = 35;
        System.out.println(solve(L, R, P));
    }
}

// This code is contributed by lokesh.

Python3

#Python code to implement the approach
import math

# Function to check if two numbers are coprime
def isCoprime(a,b):
    return math.gcd(a,b)==1
    
# Function to count the numbers which
# is coprime to P and in the range [L, R]
def solve(L,R,P):
    # Initialize count
    Cnt=0
    
    # Traverse in the range
    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 count
    return Cnt
    
# Driver Code

# Testcase1
L,R,P=1,10,16
print(solve(L,R,P))

# Testcase2
L,R,P=5,50,35
print(solve(L,R,P))

# This code is contributed by Pushpesh Raj.

C#

// C# code to implement the approach

using System;

public class GFG {

    // Function to calculate GCD
    static int gcd(int a, int b)
    {
        if (b == 0) {
            return a;
        }
        else {
            return gcd(b, Math.Abs(a - b));
        }
    }

    // Function to check if two numbers are coprime
    static bool isCoprime(int a, int b)
    {
        return gcd(a, b) == 1;
    }

    // Function to count the numbers which
    // is coprime to P and in the range [L, R]
    static int solve(int L, int R, int P)
    {
        // Initialize count
        int Cnt = 0;

        // Traverse in the range
        for (int i = L; i <= R; i++) {
            Cnt += (P % 2 == 0 && i % 2 == 0
                        ? 0
                        : (isCoprime(i, P) ? 1 : 0));
        }

        // Return count
        return Cnt;
    }

    static public void Main()
    {

        // Testcase1
        int L = 1, R = 10, P = 16;
        Console.WriteLine(solve(L, R, P));

        // Testcase2
        L = 5;
        R = 50;
        P = 35;
        Console.WriteLine(solve(L, R, P));
    }
}

// This code is contributed by lokesh.

Javascript

// JS code to implement the approach

  // Function to calculate GCD
function gcd(a,b)
    {
        if (b == 0) {
            return a;
        }
        else {
            return gcd(b, Math.abs(a - b));
        }
    }

// Function to check if two numbers are coprime
function isCoprime(a,b)
{
    return (gcd(a, b) == 1);
}

// Function to count the numbers which
// is coprime to P and in the range [L, R]
function solve(L,R,P)
{
    // Initialize count
    let Cnt = 0;

    // Traverse in the range
    for (let i = L; i <= R; i++) {
        Cnt += P % 2 == 0 && i % 2 == 0 ? 0 : isCoprime(i, P);
    }

    // Return count
    return Cnt;
}

// Driver Code

    // Testcase1
    let L = 1, R = 10, P = 16;
    console.log(solve(L, R, P));

    // Testcase2
    let a = 5, b = 50, c = 35;
    console.log(solve(a,b,c));

// This code is contributed by ksam24000
Output

5
30

Time Complexity: O((R – L) * logN)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!