# Count of Multiples of A ,B or C less than or equal to N

Given four integers **N**, **A**, **B** and **C**. The task is to find the count of integers from the range **[1, N]** which are divisible by either **A**, **B** or **C**.

**Examples:**

Input:A = 2, B = 3, C = 5, N = 10

Output:8

2, 3, 4, 5, 6, 8, 9 and 10 are the only number from the

range [1, 10] which are divisible by wither 2, 3 or 5.

Input:A = 7, B = 3, C = 5, N = 100

Output:55

**Approach:** An efficient approach is to use the concept of set theory. As we have to find numbers that are divisible by a or b or c.

So. the count of numbers divisible either by A, B or C is **(num/A) + (num/B) + (num/C) – (num/lcm(A, B)) – (num/lcm(A, B)) – (num/lcm(A, C)) + – (num/lcm(A, B, C))**

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the ` `// gcd of a and b ` `long` `gcd(` `long` `a, ` `long` `b) ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// Function to return the count of integers ` `// from the range [1, num] which are ` `// divisible by either a, b or c ` `long` `divTermCount(` `long` `a, ` `long` `b, ` `long` `c, ` `long` `num) ` `{ ` ` ` `// Calculate the number of terms divisible by a, b ` ` ` `// and c then remove the terms which are divisible ` ` ` `// by both (a, b) or (b, c) or (c, a) and then ` ` ` `// add the numbers which are divisible by a, b and c ` ` ` `return` `((num / a) + (num / b) + (num / c) ` ` ` `- (num / ((a * b) / gcd(a, b))) ` ` ` `- (num / ((c * b) / gcd(c, b))) ` ` ` `- (num / ((a * c) / gcd(a, c))) ` ` ` `+ (num / ((a * b * c) / gcd(gcd(a, b), c)))); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `long` `a = 7, b = 3, c = 5, n = 100; ` ` ` ` ` `cout << divTermCount(a, b, c, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the ` `// gcd of a and b ` `static` `long` `gcd(` `long` `a, ` `long` `b) ` `{ ` ` ` `if` `(a == ` `0` `) ` ` ` `return` `b; ` ` ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// Function to return the count of integers ` `// from the range [1, num] which are ` `// divisible by either a, b or c ` `static` `long` `divTermCount(` `long` `a, ` `long` `b, ` ` ` `long` `c, ` `long` `num) ` `{ ` ` ` `// Calculate the number of terms divisible by a, b ` ` ` `// and c then remove the terms which are divisible ` ` ` `// by both (a, b) or (b, c) or (c, a) and then ` ` ` `// add the numbers which are divisible by a, b and c ` ` ` `return` `((num / a) + (num / b) + (num / c) - ` ` ` `(num / ((a * b) / gcd(a, b))) - ` ` ` `(num / ((c * b) / gcd(c, b))) - ` ` ` `(num / ((a * c) / gcd(a, c))) + ` ` ` `(num / ((a * b * c) / gcd(gcd(a, b), c)))); ` `} ` ` ` `// Driver code ` `static` `public` `void` `main (String []arr) ` `{ ` ` ` `long` `a = ` `7` `, b = ` `3` `, c = ` `5` `, n = ` `100` `; ` ` ` ` ` `System.out.println(divTermCount(a, b, c, n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the ` `# gcd of a and b ` `def` `gcd(a, b) : ` ` ` ` ` `if` `(a ` `=` `=` `0` `) : ` ` ` `return` `b; ` ` ` ` ` `return` `gcd(b ` `%` `a, a); ` ` ` `# Function to return the count of integers ` `# from the range [1, num] which are ` `# divisible by either a, b or c ` `def` `divTermCount(a, b, c, num) : ` ` ` ` ` `# Calculate the number of terms divisible by a, b ` ` ` `# and c then remove the terms which are divisible ` ` ` `# by both (a, b) or (b, c) or (c, a) and then ` ` ` `# add the numbers which are divisible by a, b and c ` ` ` `return` `((num ` `/` `/` `a) ` `+` `(num ` `/` `/` `b) ` `+` `(num ` `/` `/` `c) ` `-` ` ` `(num ` `/` `/` `((a ` `*` `b) ` `/` `/` `gcd(a, b))) ` `-` ` ` `(num ` `/` `/` `((c ` `*` `b) ` `/` `/` `gcd(c, b))) ` `-` ` ` `(num ` `/` `/` `((a ` `*` `c) ` `/` `/` `gcd(a, c))) ` `+` ` ` `(num ` `/` `/` `((a ` `*` `b ` `*` `c) ` `/` `/` `gcd(gcd(a, b), c)))); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `7` `; b ` `=` `3` `; c ` `=` `5` `; n ` `=` `100` `; ` ` ` ` ` `print` `(divTermCount(a, b, c, n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation for above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the ` `// gcd of a and b ` `static` `long` `gcd(` `long` `a, ` `long` `b) ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// Function to return the count of integers ` `// from the range [1, num] which are ` `// divisible by either a, b or c ` `static` `long` `divTermCount(` `long` `a, ` `long` `b, ` ` ` `long` `c, ` `long` `num) ` `{ ` ` ` `// Calculate the number of terms divisible by a, b ` ` ` `// and c then remove the terms which are divisible ` ` ` `// by both (a, b) or (b, c) or (c, a) and then ` ` ` `// add the numbers which are divisible by a, b and c ` ` ` `return` `((num / a) + (num / b) + (num / c) - ` ` ` `(num / ((a * b) / gcd(a, b))) - ` ` ` `(num / ((c * b) / gcd(c, b))) - ` ` ` `(num / ((a * c) / gcd(a, c))) + ` ` ` `(num / ((a * b * c) / gcd(gcd(a, b), c)))); ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main (String []arr) ` `{ ` ` ` `long` `a = 7, b = 3, c = 5, n = 100; ` ` ` ` ` `Console.WriteLine(divTermCount(a, b, c, n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

55

## Recommended Posts:

- Find Multiples of 2 or 3 or 5 less than or equal to N
- Given a number n, count all multiples of 3 and/or 5 in set {1, 2, 3, ... n}
- Count common elements in two arrays containing multiples of N and M
- Count of common multiples of two numbers in a range
- Count numbers whose XOR with N is equal to OR with N
- Count numbers whose difference with N is equal to XOR with N
- Count non-negative triplets with sum equal to N
- Count pairs from two arrays having sum equal to K
- Count pairs with set bits sum equal to K
- Count of quadruplets from range [L, R] having GCD equal to K
- Count the numbers < N which have equal number of divisors as K
- Count numbers (smaller than or equal to N) with given digit sum
- Count number of integers less than or equal to N which has exactly 9 divisors
- Count the number of subsequences of length k having equal LCM and HCF
- Count different numbers that can be generated such that there digits sum is 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 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.