Given an integer . The task is to count all such numbers that are less than or equal to N which are divisible by any of 2 or 3 or 5.
Note: If a number less than N is divisible by both 2 or 3, or 3 or 5, or all of 2,3 and 5 then also it should be counted only once.
Input : N = 5 Output : 4 Input : N = 10 Output : 8
Simple Approach: A simple approach is to traverse from 1 to N and count multiple of 2, 3, 5 which are less than equal to N. To do this, iterate up to N and just check whether a number is divisible by 2 or 3 or 5. If it is divisible, increment the counter and after reaching N, print the result.
Time Complexity: O(N).
Efficient Approach: An efficient approach is to use the concept of set theory. As we have to find numbers that are divisible by 2 or 3 or 5.
Now the task is to find n(a),n(b),n(c),n(ab), n(bc), n(ac), and n(abc). All these terms can be calculated using Bit masking. In this problem we have taken three numbers 2,3, and 5. So, the bit mask should be of 2^3 bits i.e 8 to generate all combination of 2,3, and 5.
Now according to the formula of set union, all terms containing odd numbers of (2,3,5) will add into the result and terms containing even number of (2,3,5) will get subtracted.
Below is the implementation of the above approach:
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.
- Count of Multiples of A ,B or C less than or equal to N
- Sum of multiples of A and B less than N
- Highest and Smallest power of K less than and greater than equal to N respectively
- Find all factorial numbers less than or equal to n
- Find unique pairs such that each element is less than or equal to N
- Find Largest Special Prime which is less than or equal to a given number
- Find three integers less than or equal to N such that their LCM is maximum
- Find maximum product of digits among numbers less than or equal to N
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find all powers of 2 less than or equal to a given number
- Find all possible subarrays having product less than or equal to K
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Largest number less than N with digit sum greater than the digit sum of N
- Highest power of 2 less than or equal to given number
- Sum of largest prime factor of each number less than equal to n
- Highest power of 2 less than or equal to given Integer
- Count number of integers less than or equal to N which has exactly 9 divisors
- Largest number less than or equal to N/2 which is coprime to N
- Greatest number less than equal to B that can be formed from the digits of A
- Print all Semi-Prime Numbers less than or equal to N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.