
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find Multiples of 2 or 3 or 5 less than or equal to N in C++
In this problem, we are given a number N. Our task is to Find Multiples of 2 or 3 or 5 less than or equal to N.
Problem Description − We will be counting all elements from 1 to N that are divisible by 2 or 3 or 5.
Let’s take an example to understand the problem,
Input
N = 7
Output
5
Explanation
All the elements from 1 to 7 are : 1, 2, 3, 4, 5, 6, 7. Elements divisible by 2/3/5 are 2, 3, 4, 5, 6
Solution Approach
A simple approach to solve the problem is by traversing all numbers from 1 to N and count all numbers that are divided by 2 or 3 or 5.
ALGORITHM
Initialize − count = 0
Step 1 − loop for i = 1 to N.
Step 1.1: if(i%2 == 0 || i%3 == 0 || i%5 == 0), count++.
Step 2 − return count.
Another approach
A more effective approach to solve the problem is using the set theory.
The count of number divisible by 2 is n(2)
The count of number divisible by 3 is n(3)
The count of number divisible by 5 is n(5)
The count of number divisible by 2 and 3 is n(2 n 3)
The count of number divisible by 2 and 5 is n(2 n 5)
The count of number divisible by 3 and 5 is n(3 n 5)
The count of number divisible by 2 and 3 and 5 is n(2 n 3 n 5)
The count of number divisible by 2 or 3 or 5 is n(2 U 3 U 5)
Based on set theory,
n(2 ∪ 3 ∪ 5) = n(2) + n(3) + n(5) - n(2 ∩ 3) - n(2 ∩ 5) - n(3 ∩ 5) + n(2 ∩ 3 ∩ 5)
The solution is found by calculating the bit masks of numbers.
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; int countMultiples(int n) { int values[] = { 2, 3, 5 }; int countMultiples = 0, bitMask = pow(2, 3); for (int i = 1; i < bitMask; i++) { int prod = 1; for (int j = 0; j < 3; j++) { if (i & 1 << j) prod = prod * values[j]; } if (__builtin_popcount(i) % 2 == 1) countMultiples = countMultiples + n / prod; else countMultiples = countMultiples - n / prod; } return countMultiples; } int main() { int n = 13; cout<<"The number of multiples till "<<n<<" is "<<countMultiples(n)<<endl; return 0; }
Output
The number of multiples till 13 is 9
- Related Articles
- Find all factorial numbers less than or equal to n in C++
- Find maximum product of digits among numbers less than or equal to N in C++
- Print all prime numbers less than or equal to N in C++
- Print all Semi-Prime Numbers less than or equal to N in C++
- Find unique pairs such that each element is less than or equal to N in C++
- Find maximum sum array of length less than or equal to m in C++
- Linear magnification produced by a concave mirror may be:(a) less than 1 or equal to 1 (b) more than 1 or equal to 1(c) less than 1, more than 1 or equal to 1 (d) less than 1 or more than 1
- Linear magnification produced by a convex lens can be:(a) less than 1 or more than 1.(b) less than 1 or equal to 1.(c) more than 1 or equal to 1.(d) less than 1, equal to 1 or more than 1.
- Find three integers less than or equal to N such that their LCM is maximum - C++
- Find three integers less than or equal to N such that their LCM is maximum in C++
- C++ program to find unique pairs such that each element is less than or equal to N
- Multiples of 3 or 7 in C++
- Complete the following statements:The probability of an event is greater than or equal to …………. and less than or equal to ………..
- Print triplets with sum less than or equal to k in C Program
- Find Largest Special Prime which is less than or equal to a given number in C++
