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 = 100
Output: 78
Explanation:
In total there are 78 numbers that are divisible by either of 2 3 5 or 7.Input: arr[] = {2, 5, 7, 11} M = 200
Output: 137
Explanation:
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; } |
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 = 100n = len(lst) # Function call print(count(lst, m, n)) # This code is contributed by vishu2908 |
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
- Sum of the numbers upto N that are divisible by 2 or 5
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count of numbers upto M with GCD equals to K when paired with M
- Count of Octal numbers upto N digits
- Count numbers which are divisible by all the numbers from 2 to 10
- XOR of all Prime numbers in an Array at positions divisible by K
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Count all prime numbers in a given range whose sum of digits is also prime
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Count the numbers divisible by 'M' in a given range
- Count numbers in range 1 to N which are divisible by X but not by Y
- Absolute Difference between the Sum 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 all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count n digit numbers divisible by given number
- Count the numbers with N digits and whose suffix is divisible by K
- Count of N digit palindromic numbers divisible by 9
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.
Improved By : vishu2908

