Count of Multiples of A ,B or C less than or equal to N
Last Updated :
25 Apr, 2023
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 either 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.
- Let n(a): count of numbers divisible by a.
- Let n(b): count of numbers divisible by b.
- Let n(c): count of numbers divisible by c.
- n(a ? b): count of numbers divisible by a and b.
- n(a ? c): count of numbers divisible by a and c.
- n(b ? c): count of numbers divisible by b and c.
- n(a ? b ? c): count of numbers divisible by a and b and c.
According to set theory,
n(a ? b ? c) = n(a) + n(b) + n(c) – n(a ? b) – n(b ? c) – n(a ? c) + n(a ? b ? 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++
#include <bits/stdc++.h>
using namespace std;
long gcd( long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
long divTermCount( long a, long b, long c, long num)
{
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))));
}
int main()
{
long a = 7, b = 3, c = 5, n = 100;
cout << divTermCount(a, b, c, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static long gcd( long a, long b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static long divTermCount( long a, long b,
long c, long num)
{
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))));
}
static public void main (String []arr)
{
long a = 7 , b = 3 , c = 5 , n = 100 ;
System.out.println(divTermCount(a, b, c, n));
}
}
|
Python3
def gcd(a, b) :
if (a = = 0 ) :
return b;
return gcd(b % a, a);
def lcm (x, y):
return (x * y) / / gcd (x, y)
def divTermCount(a, b, c, num) :
return (num / / a + num / / b + num / / c -
num / / lcm(a, b) -
num / / lcm(c, b) -
num / / lcm(a, c) +
num / / (lcm(lcm(a, b), c)))
if __name__ = = "__main__" :
a = 7 ; b = 3 ; c = 5 ; n = 100 ;
print (divTermCount(a, b, c, n));
|
C#
using System;
class GFG
{
static long gcd( long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long divTermCount( long a, long b,
long c, long num)
{
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))));
}
static public void Main (String []arr)
{
long a = 7, b = 3, c = 5, n = 100;
Console.WriteLine(divTermCount(a, b, c, n));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function divTermCount(a, b, c, num)
{
return Math.ceil(((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)))));
}
n = 13;
var a = 7, b = 3, c = 5, n = 100;
document.write(divTermCount(a, b, c, n));
</script>
|
Time Complexity: O(log(min(a, b))), where a and b are the parameters of gcd
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...