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

02 Jun, 2021
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).Efficient Approach: 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 :  
C++
Java
Python3
C#
Javascript
// C++ program to find nth term// divisible by a or b #include <bits/stdc++.h>using namespace std; // Function to return// gcd of a and bint gcd(int a, int b){    if (a == 0)        return b;     return gcd(b % a, a);} // Function to calculate how many numbers// from 1 to num are divisible by a or bint divTermCount(int a, int b, int lcm, int num){    // calculate number of terms divisible by a and    // by b then, remove the terms which is are    // divisible by both a and b    return num / a + num / b - num / lcm;} // Binary search to find the nth term// divisible by a or bint findNthTerm(int a, int b, int n){    // set low to 1 and high to max(a, b)*n, here    // we have taken high as 10^18    int low = 1, high = INT_MAX, mid;    int lcm = (a * b) / gcd(a, b);     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, lcm, mid) < n)            low = mid + 1;         // if current term is greater than equal to        // n then high = mid        else            high = mid;    }     return low;} // Driver codeint main(){    int a = 2, b = 5, n = 10;    cout << findNthTerm(a, b, n) << endl;     return 0;}
// Java program to find nth term// divisible by a or bclass GFG{// Function to return// gcd of a and bstatic int gcd(int a, int b){    if (a == 0)        return b;     return gcd(b % a, a);} // Function to calculate how many numbers// from 1 to num are divisible by a or bstatic int divTermCount(int a, int b,                        int lcm, int num){    // calculate number of terms    // divisible by a and by b then,    // remove the terms which is are    // divisible by both a and b    return num / a + num / b - num / lcm;} // Binary search to find the// nth term divisible by a or bstatic int findNthTerm(int a, int b, int n){    // set low to 1 and high to max(a, b)*n,     // here we have taken high as 10^18    int low = 1, high = Integer.MAX_VALUE, mid;    int lcm = (a * b) / gcd(a, b);     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, lcm, mid) < n)            low = mid + 1;         // if current term is greater        // than equal to n then high = mid        else            high = mid;    }     return low;} // Driver codepublic static void main (String[] args){    int a = 2, b = 5, n = 10;    System.out.println(findNthTerm(a, b, n));}} // This code is contributed by Smitha
# Python 3 program to find nth term# divisible by a or bimport sys # Function to return gcd of a and bdef gcd(a, b):    if a == 0:        return b    return gcd(b % a, a) # Function to calculate how many numbers# from 1 to num are divisible by a or bdef divTermCount(a, b, lcm, num):     # calculate number of terms divisible    # by a and by b then, remove the terms    # which are divisible by both a and b    return num // a + num // b - num // lcm # Binary search to find the nth term# divisible by a or bdef findNthTerm(a, b, n):     # set low to 1 and high to max(a, b)*n,    # here we have taken high as 10^18    low = 1; high = sys.maxsize    lcm = (a * b) // gcd(a, b)    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, lcm, mid) < n:            low = mid + 1         # if current term is greater        # than equal to n then high = mid        else:            high = mid    return low # Driver codea = 2; b = 5; n = 10print(findNthTerm(a, b, n)) # This code is contributed by Shrikant13
// C# program to find nth term// divisible by a or busing System; class GFG{// Function to return gcd of a and bstatic int gcd(int a, int b){    if (a == 0)        return b;     return gcd(b % a, a);} // Function to calculate how many numbers// from 1 to num are divisible by a or bstatic int divTermCount(int a, int b,                        int lcm, int num){    // calculate number of terms    // divisible by a and by b then,    // remove the terms which is are    // divisible by both a and b    return num / a + num / b - num / lcm;} // Binary search to find the// nth term divisible by a or bstatic int findNthTerm(int a, int b, int n){    // set low to 1 and high to max(a, b)*n,    // here we have taken high as 10^18    int low = 1, high = int.MaxValue, mid;    int lcm = (a * b) / gcd(a, b);     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, lcm, mid) < n)            low = mid + 1;         // if current term is greater        // than equal to n then high = mid        else            high = mid;    }     return low;} // Driver codestatic public void Main (){    int a = 2, b = 5, n = 10;    Console.WriteLine(findNthTerm(a, b, n));}} // This code is contributed by Sach_Code
<script> // JavaScript program to find nth term// divisible by a or b // Function to return// gcd of a and bfunction gcd(a , b){    if (a == 0)        return b;     return gcd(b % a, a);} // Function to calculate how many numbers// from 1 to num are divisible by a or bfunction divTermCount(a , b, lcm , num){    // calculate number of terms    // divisible by a and by b then,    // remove the terms which is are    // divisible by both a and b    return parseInt(num / a) +    parseInt(num / b) - parseInt(num / lcm);} // Binary search to find the// nth term divisible by a or bfunction findNthTerm(a , b , n){    // set low to 1 and high to max(a, b)*n,     // here we have taken high as 10^18    var low = 1, high = Number.MAX_VALUE, mid;    var lcm = parseInt((a * b) / gcd(a, b));     while (low < high)    {        mid = low + parseInt((high - low) / 2);         // if the current term is less        // than n then we need to increase        // low to mid + 1        if (divTermCount(a, b, lcm, mid) < n)            low = mid + 1;         // if current term is greater        // than equal to n then high = mid        else            high = mid;    }     return low;} // Driver codevar a = 2, b = 5, n = 10;document.write(findNthTerm(a, b, n));  // This code is contributed by Amit Katiyar </script>
16
 
Smitha Dinesh Semwal
Sach_Code
shrikanth13
amit143katiyar
Binary Search
divisibility
GCD-LCM
Searching
Searching
Binary Search
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Search an element in a sorted and rotated array
Program to find largest element in an array
k largest(or smallest) elements in an array
Given an array of size n and a number k, find all elements that appear more than n/k times
Median of two sorted arrays of different sizes
Find the index of an array element in Java
Two Pointers Technique
Count number of occurrences (or frequency) in a sorted array
Most frequent element in an array
Best First Search (Informed Search)