VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-find-nth-term-divisible-by-a-or-b/

⇱ Program to find Nth term divisible by a or b - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to find Nth term divisible by a or b

Last Updated : 11 Jul, 2025

Given two integers and . The task is to find the Nth term which is divisible by either of or .

Examples : 

Input : a = 2, b = 5, N = 10
Output : 16

Input : a = 3, b = 7, N = 25
Output : 57


 


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 of or . This solution has time complexity of O(N).

Output:

16

Time complexity: O(N), as traversing over the terms.

Space complexity: O(1), as constanst space is used.


Efficient Approach: 

Intuition:

How to use Binary Search for this problem?

Determining the Binary Search Condition:

First, we need to identify the condition for our binary search, magicNumbersLessEqual(x). Given a number x, the count of numbers <= x that are divisible by A is given by ?x/A?. For example, if x = 17 and A = 3, then ?x/A? = 5. Similarly, the count of numbers <= x that are divisible by B is ?x/B?.

However, we cannot simply use ?x/A? + ?x/B? as the total count of numbers less than x that are divisible by A or B, as we might be double counting numbers that are divisible by both A and B. To account for this, we need to subtract the count of numbers that are divisible by both A and B, which are the LCM and its multiples. The count of numbers <= x that are multiples of LCM(A,B) will be ?x/LCM?. So, the final function becomes magicNumbersLessEqual(x) = ?x/A? + ?x/B? - ?x/LCM?.

Range of Binary Search:

Next, we need to determine the range of our search space. The starting point, L, is the minimum of A and B, as no number lower than that would be divisible by either. The end point, R, is N * min(A,B), as this will ensure that there are at least N magical numbers.

During each iteration, we calculate the midpoint and determine magicNumbersLessEqual(mid). If it is less than N, we set L = mid + 1 as any number less than mid cannot be the answer. If it is greater than N, we set R = mid - 1 as any number greater than mid cannot be the answer. Finally, we return the number that gives us N magical numbers <= it, which will be the Nth magical number.

The idea is to use Binary search. Here we can calculate how many numbers from 1 to are divisible by either a or b by using formula:

All the multiples of lcm(a, b) will be divisible by both and so we need to remove these terms. Now if the number of divisible terms is less than N we will increase the low position of binary search otherwise decrease high until number of divisible terms is equal to N.
Below is the implementation of the above idea : 
 


Output
16

There is a third approach that takes O(log(min(a, b))).
Worst Case Time Complexity - O(log(min(a, b)))
Auxiliary Space: O(1)


Output
16

This approach uses density of a and b, and solves for n.

The approach takes only O(log(min(a, b))) time as only simple mathematical operations(add, subtract, multiply and divide, minimum/maximum of 2 numbers -> all O(1) and gcd operation-> O(log(min(a, b)))) are used here.

NOTE ON Modulo("%"): a-=b*(a/b) is equivalent to a%=b, so you can assume that to be O(1) as well.

Comment