Given four integers **N**, **A**, **B** and **C**. The task is to print the **N ^{th}** number in the set containing the multiples of

**A**,

**B**or

**C**.

**Examples:**

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

Output:10

2, 3, 4, 5, 6, 8, 9,10, 12, 14, …

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

Output:136

**Naive approach:** Start traversing from **1** until we find the **N ^{th}** element which is divisible by either

**A**,

**B**or

**C**.

**Efficient approach:** Given a number we can find the count of the divisors of either **A**, **B** or **C**. Now, binary search can be used to find the **N ^{th}** number which is divisible by either

**A**,

**B**or

**C**.

So, if the number is num then

**count = (num/A) + (num/B) + (num/C) – (num/lcm(A, B)) – (num/lcm(C, B)) – (num/lcm(A, C)) – (num/lcm(A, B, C))**

Below is the implementation of the above approach:

## C++

`// C++ program to find nth term ` `// divisible by a, b or c ` ` ` `#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 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)/gcd(a, b))* c) / gcd(((a*b)/gcd(a, b)), c)))); ` `} ` ` ` `// Function for binary search to find the ` `// nth term divisible by a, b or c ` `int` `findNthTerm(` `int` `a, ` `int` `b, ` `int` `c, ` `long` `n) ` `{ ` ` ` `// Set low to 1 and high to LONG_MAX ` ` ` `long` `low = 1, high = LONG_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() ` `{ ` ` ` `long` `a = 2, b = 3, c = 5, n = 100; ` ` ` ` ` `cout << findNthTerm(a, b, c, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find nth term ` `// divisible by a, b or c ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return ` ` ` `// 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)))); ` ` ` `} ` ` ` ` ` `// Function for binary search to find the ` ` ` `// nth term divisible by a, b or c ` ` ` `static` `long` `findNthTerm(` `int` `a, ` `int` `b, ` `int` `c, ` `long` `n) ` ` ` `{ ` ` ` ` ` `// Set low to 1 and high to LONG_MAX ` ` ` `long` `low = ` `1` `, high = Long.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 = ` `100` `; ` ` ` ` ` `System.out.println(findNthTerm(a, b, c, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find nth term ` `# divisible by a, b or c ` `import` `sys ` ` ` `# 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 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)))); ` ` ` `# Function for binary search to find the ` `# nth term divisible by a, b or c ` `def` `findNthTerm(a, b, c, n): ` ` ` ` ` `# Set low to 1 and high to LONG_MAX ` ` ` `low ` `=` `1` `; high ` `=` `sys.maxsize; 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` `int` `(low); ` ` ` `# Driver code ` `a ` `=` `2` `; b ` `=` `3` `; c ` `=` `5` `; n ` `=` `100` `; ` ` ` `print` `(findNthTerm(a, b, c, n)); ` ` ` `# This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find nth term ` `// divisible by a, b or c ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return ` ` ` `// 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)))); ` ` ` `} ` ` ` ` ` `// Function for binary search to find the ` ` ` `// nth term divisible by a, b or c ` ` ` `static` `long` `findNthTerm(` `int` `a, ` `int` `b, ` ` ` `int` `c, ` `long` `n) ` ` ` `{ ` ` ` ` ` `// Set low to 1 and high to LONG_MAX ` ` ` `long` `low = 1, high = ` `long` `.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 = 100; ` ` ` ` ` `Console.WriteLine(findNthTerm(a, b, c, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

136

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:

- Given a number n, count all multiples of 3 and/or 5 in set {1, 2, 3, ... n}
- Kth number from the set of multiples of numbers A, B and C
- Sum of multiples of a number up to N
- Print multiples of Unit Digit of Given Number
- Maximum number of multiples in an array before any element
- Multiples of 3 or 7
- N-th multiple in sorted list of multiples of two numbers
- Queries for counts of multiples in an array
- Find Multiples of 2 or 3 or 5 less than or equal to N
- Count of common multiples of two numbers in a range
- Find the sum of all multiples of 2 and 5 below N
- Sum of all the multiples of 3 and 7 below N
- Sum of multiples of A and B less than N
- Minimum sum after subtracting multiples of k from the elements of the array
- Rearrange all elements of array which are multiples of x in decreasing order
- Sort elements of the array that occurs in between multiples of K
- Count common elements in two arrays containing multiples of N and M
- Count of Multiples of A ,B or C less than or equal to N
- Find numbers which are multiples of first array and factors of second array
- Count of multiples in an Array before every element

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.