📌  相关文章
📜  给定质数最多可除以M的数的计数

📅  最后修改于: 2021-04-24 16:10:28             🧑  作者: 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的数字。然后计算所有标记的数字并打印。