Find the Nth term divisible by a or b or c
Given four integers a, b, c, and N. The task is to find the Nth term which is divisible by either of a, b or c.
Examples:
Input: a = 2, b = 3, c = 5, N = 10
Output: 14
Sequence is 2, 3, 4, 5, 6, 8, 9, 10, 12, 14, 15, 16…
Input: a = 3, b = 5, c = 7, N = 10
Output: 18
Naive Approach: A simple approach is to traverse over all the terms starting from 1 until we find the desired Nth term which is divisible by either a, b or c. This solution has time complexity of O(N).
Efficient Approach: The idea is to use Binary search. Here we can calculate how many numbers from 1 to num are divisible by either a, b or c by using the formula: (num / a) + (num / b) + (num / c) – (num / lcm(a, b)) – (num / lcm(b, c)) – (num / lcm(a, c)) + (num / lcm(a, b, c))
The above formula is derived using set theory
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int lcm( int a, int b)
{
return (a * b) / gcd(a, b);
}
int divTermCount( int a, int b, int c, int num)
{
return ((num / a) + (num / b) + (num / c)
- (num / lcm(a, b))
- (num / lcm(b, c))
- (num / lcm(a, c))
+ (num / lcm(a, lcm(b, c))));
}
int findNthTerm( int a, int b, int c, int n)
{
int low = 1, high = INT_MAX, mid;
while (low < high) {
mid = low + (high - low) / 2;
if (divTermCount(a, b, c, mid) < n)
low = mid + 1;
else
high = mid;
}
return low;
}
int main()
{
int a = 2, b = 3, c = 5, n = 10;
cout << findNthTerm(a, b, c, n);
return 0;
}
|
Java
class GFG
{
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static int lcm( int a, int b)
{
return (a * b) / gcd(a, b);
}
static int divTermCount( int a, int b, int c, int num)
{
return ((num / a) + (num / b) + (num / c)
- (num / lcm(a, b))
- (num / lcm(b, c))
- (num / lcm(a, c))
+ (num / lcm(a, lcm(b, c))));
}
static int findNthTerm( int a, int b, int c, int n)
{
int low = 1 , high = Integer.MAX_VALUE, mid;
while (low < high) {
mid = low + (high - low) / 2 ;
if (divTermCount(a, b, c, mid) < n)
low = mid + 1 ;
else
high = mid;
}
return low;
}
public static void main(String[] args)
{
int a = 2 , b = 3 , c = 5 , n = 10 ;
System.out.println(findNthTerm(a, b, c, n));
}
}
|
Python
def gcd(a, b):
if (a = = 0 ):
return b
return gcd(b % a, a)
def lcm(a, b):
return ((a * b) / / gcd(a, b))
def divTermCount(a, b, c, num):
return ((num / / a) + (num / / b) + (num / / c)
- (num / / lcm(a, b))
- (num / / lcm(b, c))
- (num / / lcm(a, c))
+ (num / / lcm(a, lcm(b, c))))
def findNthTerm(a, b, c, n):
low = 1
high = 10 * * 9
mid = 0
while (low < high):
mid = low + (high - low) / / 2
if (divTermCount(a, b, c, mid) < n):
low = mid + 1
else :
high = mid
return low
a = 2
b = 3
c = 5
n = 10
print (findNthTerm(a, b, c, n))
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm( int a, int b)
{
return (a * b) / gcd(a, b);
}
static int divTermCount( int a, int b, int c, int num)
{
return ((num / a) + (num / b) + (num / c)
- (num / lcm(a, b))
- (num / lcm(b, c))
- (num / lcm(a, c))
+ (num / lcm(a, lcm(b, c))));
}
static int findNthTerm( int a, int b, int c, int n)
{
int low = 1, high = int .MaxValue, mid;
while (low < high) {
mid = low + (high - low) / 2;
if (divTermCount(a, b, c, mid) < n)
low = mid + 1;
else
high = mid;
}
return low;
}
public static void Main(String[] args)
{
int a = 2, b = 3, c = 5, n = 10;
Console.WriteLine(findNthTerm(a, b, c, n));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function lcm(a, b)
{
return parseInt((a * b) / gcd(a, b));
}
function divTermCount(a, b, c, num)
{
return (parseInt(num / a) + parseInt(num / b) +
parseInt(num / c)
- parseInt(num / lcm(a, b))
- parseInt(num / lcm(b, c))
- parseInt(num / lcm(a, c))
+ parseInt(num / lcm(a, lcm(b, c))));
}
function findNthTerm(a, b, c, n)
{
let low = 1, high = Number.MAX_VALUE, mid;
while (low < high) {
mid = low + parseInt((high - low) / 2);
if (divTermCount(a, b, c, mid) < n)
low = mid + 1;
else
high = mid;
}
return low;
}
let a = 2, b = 3, c = 5, n = 10;
document.write(findNthTerm(a, b, c, n));
</script>
|
Time Complexity: O(log n * log(min(a, b))), where a and b are two parameters for GCD.
Auxiliary Space: O(log(min(a, b)))
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...