📌  相关文章
📜  计算[A,B]范围内不能被C和D整除的整数

📅  最后修改于: 2021-05-06 08:51:33             🧑  作者: Mango

给定四个整数A,B,CD。任务是找到[A,B]范围内不能被CD整除的整数计数。

例子:

方法:首先将范围内的所有整数包括在所需答案中,即B – A + 1 。然后删除所有可以被CD整除的数字,最后添加所有可以被CD整除的数字。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of
// integers from the range [a, b] that
// are not divisible by c and d
int countNums(int a, int b, int c, int d)
{
    // Numbers which are divisible by c
    int x = b / c - (a - 1) / c;
  
    // Numbers which are divisible by d
    int y = b / d - (a - 1) / d;
  
    // Find lowest common factor of c and d
    int k = (c * d) / __gcd(c, d);
  
    // Numbers which are divisible by both c and d
    int z = b / k - (a - 1) / k;
  
    // Return the required answer
    return b - a + 1 - x - y + z;
}
  
// Driver code
int main()
{
    int a = 10, b = 50, c = 4, d = 6;
  
    cout << countNums(a, b, c, d);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
// Function to return the count of
// integers from the range [a, b] that
// are not divisible by c and d
static int countNums(int a, int b, int c, int d)
{
    // Numbers which are divisible by c
    int x = b / c - (a - 1) / c;
  
    // Numbers which are divisible by d
    int y = b / d - (a - 1) / d;
  
    // Find lowest common factor of c and d
    int k = (c * d) / __gcd(c, d);
  
    // Numbers which are divisible by both c and d
    int z = b / k - (a - 1) / k;
  
    // Return the required answer
    return b - a + 1 - x - y + z;
}
static int __gcd(int a, int b) 
{ 
    if (b == 0) 
        return a; 
    return __gcd(b, a % b);     
}
  
// Driver code
public static void main(String []args) 
{
    int a = 10, b = 50, c = 4, d = 6;
  
    System.out.println(countNums(a, b, c, d));
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach
from math import gcd
  
# Function to return the count of 
# integers from the range [a, b] that 
# are not divisible by c and d 
def countNums(a, b, c, d) :
  
    # Numbers which are divisible by c 
    x = b // c - (a - 1) // c; 
  
    # Numbers which are divisible by d 
    y = b // d - (a - 1) // d; 
  
    # Find lowest common factor of c and d 
    k = (c * d) // gcd(c, d); 
  
    # Numbers which are divisible 
    # by both c and d 
    z = b // k - (a - 1) // k; 
  
    # Return the required answer 
    return (b - a + 1 - x - y + z); 
  
# Driver code 
if __name__ == "__main__" : 
  
    a = 10; b = 50; c = 4; d = 6; 
  
    print(countNums(a, b, c, d)); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
      
class GFG
{
  
// Function to return the count of
// integers from the range [a, b] that
// are not divisible by c and d
static int countNums(int a, int b, int c, int d)
{
    // Numbers which are divisible by c
    int x = b / c - (a - 1) / c;
  
    // Numbers which are divisible by d
    int y = b / d - (a - 1) / d;
  
    // Find lowest common factor of c and d
    int k = (c * d) / __gcd(c, d);
  
    // Numbers which are divisible by both c and d
    int z = b / k - (a - 1) / k;
  
    // Return the required answer
    return b - a + 1 - x - y + z;
}
  
static int __gcd(int a, int b) 
{ 
    if (b == 0) 
        return a; 
    return __gcd(b, a % b);     
}
  
// Driver code
public static void Main(String []args) 
{
    int a = 10, b = 50, c = 4, d = 6;
  
    Console.WriteLine(countNums(a, b, c, d));
}
}
  
// This code is contributed by Rajput-Ji


输出:
28