📜  可被给定素数整除的数最多为 M

📅  最后修改于: 2021-09-17 07:09:59             🧑  作者: Mango

给定一个由素数组成的数组arr[]和一个数字M ,任务是计算范围[1, M]中可被任何给定素数整除的元素的数量。

例子:

朴素的方法:这个想法是迭代范围[1, M]并检查是否有任何数组元素除以范围[1, M] 中的元素然后对元素进行计数,否则检查范围中的下一个数字。
下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the numbers that
// are divisible by the numbers in
// the array from range 1 to M
int count(int a[], int M, int N)
{
    // Initialize the count variable
    int cnt = 0;
 
    // Iterate over [1, M]
    for (int i = 1; i <= M; i++) {
 
        // Iterate over array elements arr[]
        for (int j = 0; j < N; j++) {
 
            // Check if i is divisible by a[j]
            if (i % a[j] == 0) {
 
                // Increment the count
                cnt++;
                break;
            }
        }
    }
 
    // Return the answer
    return cnt;
}
 
// Driver code
int main()
{
    // Given array arr[]
    int arr[] = { 2, 3, 5, 7 };
 
    // Given Number M
    int m = 100;
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << count(arr, m, n);
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to count the numbers that
// are divisible by the numbers in
// the array from range 1 to M
static int count(int a[], int M, int N)
{
     
    // Initialize the count variable
    int cnt = 0;
 
    // Iterate over [1, M]
    for(int i = 1; i <= M; i++)
    {
         
        // Iterate over array elements arr[]
        for(int j = 0; j < N; j++)
        {
             
            // Check if i is divisible by a[j]
            if (i % a[j] == 0)
            {
                 
                // Increment the count
                cnt++;
                break;
            }
        }
    }
     
    // Return the answer
    return cnt;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given array arr[]
    int arr[] = { 2, 3, 5, 7 };
 
    // Given number M
    int m = 100;
    int n = arr.length;
 
    // Function call
    System.out.print(count(arr, m, n));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to count the numbers that
# are divisible by the numbers in
# the array from range 1 to M
def count(a, M, N):
     
    # Initialize the count variable
    cnt = 0
 
    # Iterate over [1, M]
    for i in range(1, M + 1):
 
        # Iterate over array elements arr[]
        for j in range(N):
 
            # Check if i is divisible by a[j]
            if (i % a[j] == 0):
 
                # Increment the count
                cnt += 1
                break
 
    # Return the answer
    return cnt
 
# Driver code
 
# Given list lst
lst = [ 2, 3, 5, 7 ]
 
# Given number M
m = 100
n = len(lst)
 
# Function call
print(count(lst, m, n))
 
# This code is contributed by vishu2908


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the numbers that
// are divisible by the numbers in
// the array from range 1 to M
static int count(int []a, int M, int N)
{
     
    // Initialize the count variable
    int cnt = 0;
 
    // Iterate over [1, M]
    for(int i = 1; i <= M; i++)
    {
         
        // Iterate over array elements []arr
        for(int j = 0; j < N; j++)
        {
             
            // Check if i is divisible by a[j]
            if (i % a[j] == 0)
            {
                 
                // Increment the count
                cnt++;
                break;
            }
        }
    }
     
    // Return the answer
    return cnt;
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given array []arr
    int []arr = { 2, 3, 5, 7 };
 
    // Given number M
    int m = 100;
    int n = arr.Length;
 
    // Function call
    Console.Write(count(arr, m, n));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出
78

时间复杂度: O(N*M)
辅助空间: O(1)
另一种方法:另一种解决此问题的方法是使用动态规划和 Seive。标记所有可被数组中的任何素数整除的 M 以内的数。然后计算所有标记的数字并打印出来。