Given four integers **a**, **b**, **c** and **N**. The task is to find the **N ^{th}** 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 **N ^{th}** 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++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return ` `// gcd of a and b ` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// Function to return the lcm of a and b ` `int` `lcm(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `(a * b) / gcd(a, b); ` `} ` ` ` `// Function to return the count of numbers ` `// from 1 to num which are divisible by a, b or c ` `int` `divTermCount(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `num) ` `{ ` ` ` ` ` `// Calculate number of terms divisible by a and ` ` ` `// by b and by c then, remove the terms which is are ` ` ` `// divisible by both a and b, both b and c, both ` ` ` `// c and a and then add which are divisible by a and ` ` ` `// b and c ` ` ` `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)))); ` `} ` ` ` `// Function to find the nth term ` `// divisible by a, b or c ` `// by using binary search ` `int` `findNthTerm(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `n) ` `{ ` ` ` ` ` `// Set low to 1 and high to max (a, b, c) * n ` ` ` `int` `low = 1, high = INT_MAX, mid; ` ` ` ` ` `while` `(low < high) { ` ` ` `mid = low + (high - low) / 2; ` ` ` ` ` `// If the current term is less than ` ` ` `// n then we need to increase low ` ` ` `// to mid + 1 ` ` ` `if` `(divTermCount(a, b, c, mid) < n) ` ` ` `low = mid + 1; ` ` ` ` ` `// If current term is greater than equal to ` ` ` `// n then high = mid ` ` ` `else` ` ` `high = mid; ` ` ` `} ` ` ` ` ` `return` `low; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 2, b = 3, c = 5, n = 10; ` ` ` ` ` `cout << findNthTerm(a, b, c, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return ` ` ` `// gcd of a and b ` ` ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `if` `(a == ` `0` `) ` ` ` `return` `b; ` ` ` ` ` `return` `gcd(b % a, a); ` ` ` `} ` ` ` ` ` `// Function to return the lcm of a and b ` ` ` `static` `int` `lcm(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `return` `(a * b) / gcd(a, b); ` ` ` `} ` ` ` ` ` `// Function to return the count of numbers ` ` ` `// from 1 to num which are divisible by a, b or c ` ` ` `static` `int` `divTermCount(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `num) ` ` ` `{ ` ` ` ` ` `// Calculate number of terms divisible by a and ` ` ` `// by b and by c then, remove the terms which is are ` ` ` `// divisible by both a and b, both b and c, both ` ` ` `// c and a and then add which are divisible by a and ` ` ` `// b and c ` ` ` `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)))); ` ` ` `} ` ` ` ` ` `// Function to find the nth term ` ` ` `// divisible by a, b or c ` ` ` `// by using binary search ` ` ` `static` `int` `findNthTerm(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `n) ` ` ` `{ ` ` ` ` ` `// Set low to 1 and high to max (a, b, c) * n ` ` ` `int` `low = ` `1` `, high = Integer.MAX_VALUE, mid; ` ` ` ` ` `while` `(low < high) { ` ` ` `mid = low + (high - low) / ` `2` `; ` ` ` ` ` `// If the current term is less than ` ` ` `// n then we need to increase low ` ` ` `// to mid + 1 ` ` ` `if` `(divTermCount(a, b, c, mid) < n) ` ` ` `low = mid + ` `1` `; ` ` ` ` ` `// If current term is greater than equal to ` ` ` `// n then high = mid ` ` ` `else` ` ` `high = mid; ` ` ` `} ` ` ` ` ` `return` `low; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `a = ` `2` `, b = ` `3` `, c = ` `5` `, n = ` `10` `; ` ` ` ` ` `System.out.println(findNthTerm(a, b, c, n)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// Rajnis09 ` |

*chevron_right*

*filter_none*

## Python

`# Python3 implementation of the approach ` ` ` `# Function to return ` `# gcd of a and b ` `def` `gcd(a, b): ` ` ` ` ` `if` `(a ` `=` `=` `0` `): ` ` ` `return` `b ` ` ` ` ` `return` `gcd(b ` `%` `a, a) ` ` ` `# Function to return the lcm of a and b ` `def` `lcm(a, b): ` ` ` ` ` `return` `((a ` `*` `b) ` `/` `/` `gcd(a, b)) ` ` ` ` ` `# Function to return the count of numbers ` `# from 1 to num which are divisible by a, b or c ` `def` `divTermCount(a, b, c, num): ` ` ` ` ` `# Calculate number of terms divisible by a and ` ` ` `# by b and by c then, remove the terms which is are ` ` ` `# divisible by both a and b, both b and c, both ` ` ` `# c and a and then add which are divisible by a and ` ` ` `# b and c ` ` ` `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)))) ` ` ` ` ` `# Function to find the nth term ` `# divisible by a, b or c ` `# by using binary search ` `def` `findNthTerm(a, b, c, n): ` ` ` ` ` ` ` `# Set low to 1 and high to max (a, b, c) * n ` ` ` `low ` `=` `1` ` ` `high ` `=` `10` `*` `*` `9` ` ` `mid` `=` `0` ` ` ` ` `while` `(low < high): ` ` ` `mid ` `=` `low ` `+` `(high ` `-` `low) ` `/` `/` `2` ` ` ` ` `# If the current term is less than ` ` ` `# n then we need to increase low ` ` ` `# to mid + 1 ` ` ` `if` `(divTermCount(a, b, c, mid) < n): ` ` ` `low ` `=` `mid ` `+` `1` ` ` ` ` `# If current term is greater than equal to ` ` ` `# n then high = mid ` ` ` `else` `: ` ` ` `high ` `=` `mid ` ` ` ` ` ` ` `return` `low ` ` ` `# Driver code ` `a ` `=` `2` `b ` `=` `3` `c ` `=` `5` `n ` `=` `10` ` ` `print` `(findNthTerm(a, b, c, n)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return ` ` ` `// gcd of a and b ` ` ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` ` ` `return` `gcd(b % a, a); ` ` ` `} ` ` ` ` ` `// Function to return the lcm of a and b ` ` ` `static` `int` `lcm(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `return` `(a * b) / gcd(a, b); ` ` ` `} ` ` ` ` ` `// Function to return the count of numbers ` ` ` `// from 1 to num which are divisible by a, b or c ` ` ` `static` `int` `divTermCount(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `num) ` ` ` `{ ` ` ` ` ` `// Calculate number of terms divisible by a and ` ` ` `// by b and by c then, remove the terms which is are ` ` ` `// divisible by both a and b, both b and c, both ` ` ` `// c and a and then add which are divisible by a and ` ` ` `// b and c ` ` ` `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)))); ` ` ` `} ` ` ` ` ` `// Function to find the nth term ` ` ` `// divisible by a, b or c ` ` ` `// by using binary search ` ` ` `static` `int` `findNthTerm(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `n) ` ` ` `{ ` ` ` ` ` `// Set low to 1 and high to max (a, b, c) * n ` ` ` `int` `low = 1, high = ` `int` `.MaxValue, mid; ` ` ` ` ` `while` `(low < high) { ` ` ` `mid = low + (high - low) / 2; ` ` ` ` ` `// If the current term is less than ` ` ` `// n then we need to increase low ` ` ` `// to mid + 1 ` ` ` `if` `(divTermCount(a, b, c, mid) < n) ` ` ` `low = mid + 1; ` ` ` ` ` `// If current term is greater than equal to ` ` ` `// n then high = mid ` ` ` `else` ` ` `high = mid; ` ` ` `} ` ` ` ` ` `return` `low; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `a = 2, b = 3, c = 5, n = 10; ` ` ` ` ` `Console.WriteLine(findNthTerm(a, b, c, n)); ` ` ` ` ` `} ` `} ` ` ` `/* This code is contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

**Output:**

14

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.

## Recommended Posts:

- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- First term from given Nth term of the equation F(N) = (2 * F(N - 1)) % 10^9 + 7
- Nth term of given recurrence relation having each term equal to the product of previous K terms
- Program to find Nth term divisible by a or b
- Count sequences of length K having each term divisible by its preceding term
- Find nth term of the Dragon Curve Sequence
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Program to find the Nth term of series -1, 2, 11, 26, 47......
- Program to find Nth term of series 9, 23, 45, 75, 113...
- Program to find Nth term in the series 0, 2, 1, 3, 1, 5, 2, 7, 3,…
- Program to find the Nth term of the series 3, 20, 63, 144, 230, ……
- Program to find the Nth term of series 5, 10, 17, 26, 37, 50, 65, 82, ...
- Program to find the Nth term of series 0, 4, 14, 30, 51, 80, 114, 154, 200, ...
- Find Nth term of the series 3, 14, 39, 84...
- Program to find the Nth term of the series 0, 14, 40, 78, 124, ...
- Program to find the Nth term of the series 0, 5, 14, 27, 44, ........
- Find Nth term of the series 1, 8, 54, 384...

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.