Given an array **arr[]** of Prime Numbers and a number **M**, the task is to count the number of elements in the range **[1, M]** that are divisible by any of the given prime numbers.

**Examples:**

Input:arr[] = {2, 3, 5, 7} M = 100Output:78Explanation:

In total there are 78 numbers that are divisible by either of 2 3 5 or 7.

Input:arr[] = {2, 5, 7, 11} M = 200Output:137Explanation:

In total there are 137 numbers that are divisible by either of 2 5 7 or 11.

**Naive Approach:** The idea is iterate over the range **[1, M]** and check if any of the array element is divides the element in the range **[1, M]** then count the element else check for the next number in the range.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <iostream> ` `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; ` `}` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output**

78

**Time Complexity:** O(N*M) **Auxiliary Space:** O(1) **Another Approach:** Another method to solve this problem is use Dynamic Programming and Seive. Mark all the numbers up to M that are divisible by any prime number in the array. Then count all the marked numbers and print it.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of largest divisor of numbers upto N not divisible by given prime number P
- Count of all values of N in [L, R] such that count of primes upto N is also prime
- Sum of the numbers upto N that are divisible by 2 or 5
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Count all prime numbers in a given range whose sum of digits is also prime
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Count numbers upto N which are both perfect square and perfect cube
- Count of Octal numbers upto N digits
- Count of numbers upto M with GCD equals to K when paired with M
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.