📌  相关文章
📜  计算所有数组元素都可整除的范围内的数字

📅  最后修改于: 2021-06-25 23:30:49             🧑  作者: Mango

给定N个数字和L和R两个数字,任务是打印范围为[L,R]的数字计数,该数字可被数组的所有数字整除。
例子:

天真的方法是从L迭代到R并计算所有数组元素都可整除的数字。
时间复杂度: O((RL)* N)
一种有效的方法是找到N个数字的LCM,然后计算L和R范围内可被LCM整除的数字。被LCM整除的数字直到R为R / LCM。因此,使用排除原理,计数将为(R / LCM – L-1 / LCM)。
下面是上述方法的实现。

C++
// C++ program to count numbers in a range
// that are divisible by all array elements
#include 
using namespace std;
 
// Function to find the lcm of array
int findLCM(int arr[], int n)
{
    int lcm = arr[0];
 
    // Iterate in the array
    for (int i = 1; i < n; i++) {
 
        // Find lcm
        lcm = (lcm * arr[i]) / __gcd(arr[i], lcm);
    }
 
    return lcm;
}
 
// Function to return the count of numbers
int countNumbers(int arr[], int n, int l, int r)
{
 
    // Function call to find the
    // LCM of N numbers
    int lcm = findLCM(arr, n);
 
    // Return the count of numbers
    int count = (r / lcm) - ((l - 1) / lcm);
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 4, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int l = 1, r = 10;
 
    cout << countNumbers(arr, n, l, r);
    return 0;
}


Java
// Java program to count numbers in a range
// that are divisible by all array elements
class GFG
{
     
// Function to calculate gcd
static int __gcd(int a, int b)
{
       
    // Everything divides 0
    if (a == 0 || b == 0)
        return 0;
   
    // base case
    if (a == b)
        return a;
   
    // a is greater
    if (a > b)
        return __gcd(a - b, b);
           
    return __gcd(a, b - a);
}
     
// Function to find the lcm of array
static int findLCM(int arr[], int n)
{
    int lcm = arr[0];
 
    // Iterate in the array
    for (int i = 1; i < n; i++)
    {
 
        // Find lcm
        lcm = (lcm * arr[i]) / __gcd(arr[i], lcm);
    }
 
    return lcm;
}
 
// Function to return the count of numbers
static int countNumbers(int arr[], int n,
                        int l, int r)
{
 
    // Function call to find the
    // LCM of N numbers
    int lcm = findLCM(arr, n);
 
    // Return the count of numbers
    int count = (r / lcm) - ((l - 1) / lcm);
 
    return count;
}
 
// Driver Code
public static void main(String args[])
{
    int arr[] = { 1, 4, 2 };
    int n = arr.length;
    int l = 1, r = 10;
 
    System.out.println(countNumbers(arr, n, l, r));
}
}
 
// This code is contributed by Mukul Singh


Python3
# Python program to count numbers in
# a range that are divisible by all
# array elements
import math
 
# Function to find the lcm of array
def findLCM(arr, n):
 
    lcm = arr[0];
 
    # Iterate in the array
    for i in range(1, n - 1):
 
        # Find lcm
        lcm = (lcm * arr[i]) / math.gcd(arr[i], lcm);
 
    return lcm;
 
# Function to return the count of numbers
def countNumbers(arr, n, l, r):
 
    # Function call to find the
    # LCM of N numbers
    lcm = int(findLCM(arr, n));
     
    # Return the count of numbers
    count = (r / lcm) - ((l - 1) / lcm);
    print(int(count));
 
# Driver Code
arr = [1, 4, 2];
n = len(arr);
l = 1;
r = 10;
 
countNumbers(arr, n, l, r);
 
# This code is contributed
# by Shivi_Aggarwal


C#
// C# program to count numbers in a range
// that are divisible by all array elements
using System;
 
class GFG
{
     
// Function to calculate gcd
static int __gcd(int a, int b)
{
         
    // Everything divides 0
    if (a == 0 || b == 0)
        return 0;
     
    // base case
    if (a == b)
        return a;
     
    // a is greater
    if (a > b)
        return __gcd(a - b, b);
             
    return __gcd(a, b - a);
}
     
// Function to find the lcm of array
static int findLCM(int []arr, int n)
{
    int lcm = arr[0];
 
    // Iterate in the array
    for (int i = 1; i < n; i++)
    {
 
        // Find lcm
        lcm = (lcm * arr[i]) / __gcd(arr[i], lcm);
    }
 
    return lcm;
}
 
// Function to return the count of numbers
static int countNumbers(int []arr, int n,
                        int l, int r)
{
 
    // Function call to find the
    // LCM of N numbers
    int lcm = findLCM(arr, n);
 
    // Return the count of numbers
    int count = (r / lcm) - ((l - 1) / lcm);
 
    return count;
}
 
// Driver Code
public static void Main()
{
    int []arr = { 1, 4, 2 };
    int n = arr.Length;
    int l = 1, r = 10;
 
    Console.WriteLine(countNumbers(arr, n, l, r));
}
}
 
// This code is contributed by Ryuga


PHP
 $b)
        return __gcd($a - $b, $b);
         
    return __gcd($a, $b - $a);
}
     
// Function to find the lcm of array
function findLCM($arr, $n)
{
    $lcm = $arr[0];
 
    // Iterate in the array
    for ($i = 1; $i < $n; $i++)
    {
 
        // Find lcm
        $lcm = ($lcm * $arr[$i]) /
                 __gcd($arr[$i], $lcm);
    }
 
    return $lcm;
}
 
// Function to return the count of numbers
function countNumbers($arr, $n, $l, $r)
{
 
    // Function call to find the
    // LCM of N numbers
    $lcm = findLCM($arr, $n);
 
    // Return the count of numbers
    $count = (int)($r / $lcm) -
             (int)(($l - 1) / $lcm);
 
    return $count;
}
 
// Driver Code
$arr = array(1, 4, 2);
$n = sizeof($arr);
$l = 1; $r = 10;
echo countNumbers($arr, $n, $l, $r);
 
// This code is contributed
// by Akanksha Rai
?>


Javascript


输出:
2

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。