Nth number in a set of multiples of A , B or C
Given four integers N, A, B, and C. The task is to print the Nth 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 Nth 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 Nth number 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++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
long gcd_ab, gcd_bc, gcd_ac, lcm_abc, lcm_ab, lcm_bc, lcm_ac;
void preCal( long a, long b, long c) {
gcd_ab = gcd(a, b);
gcd_bc = gcd(c, b);
gcd_ac = gcd(a, c);
lcm_ab = ((a * b) / gcd_ab);
lcm_bc = ((c * b) / gcd_bc);
lcm_ac = ((a * c) / gcd_ac);
lcm_abc = (lcm_ab * c) / gcd(lcm_ab, c);
}
long divTermCount( long a, long b, long c, long num)
{
return ((num / a) + (num / b) + (num / c)
- (num / lcm_ab)
- (num / lcm_bc)
- (num / lcm_ac)
+ (num / lcm_abc));
}
int findNthTerm( int a, int b, int c, long n)
{
preCal(a, b, c);
long low = 1, high, mid;
high = max(a, max(b, c)) * n;
low = gcd(a, gcd_bc) * n;
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()
{
long a = 2, b = 3, c = 5, n = 100;
cout << findNthTerm(a, b, c, n);
return 0;
}
|
Java
class GFG
{
static long gcd_ab, gcd_bc, gcd_ac, lcm_abc, lcm_ab, lcm_bc, lcm_ac;
static long gcd( long a, long b)
{
if (a == 0 )
{
return b;
}
return gcd(b % a, a);
}
static void preCal( long a, long b, long c) {
gcd_ab = gcd(a, b);
gcd_bc = gcd(c, b);
gcd_ac = gcd(a, c);
lcm_ab = ((a * b) / gcd_ab);
lcm_bc = ((c * b) / gcd_bc);
lcm_ac = ((a * c) / gcd_ac);
lcm_abc = (lcm_ab * c) / gcd(lcm_ab, c);
}
static long divTermCount( long a, long b,
long c, long num)
{
return ((num / a) + (num / b) + (num / c)
- (num / lcm_ab)
- (num / lcm_bc)
- (num / lcm_ac)
+ (num / lcm_abc));
}
static long findNthTerm( int a, int b, int c, long n)
{
preCal(a, b, c);
long low = 1 , high, mid;
high = a > b ? (a > c ? a : c) : (b > c ? b : c);
high = high * n;
low = gcd(a, gcd_bc) * n;
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 = 100 ;
System.out.println(findNthTerm(a, b, c, n));
}
}
|
Python3
import sys
def gcd(a, b):
if (a = = 0 ):
return b;
return gcd(b % a, a);
gcd_ab = 1 ; gcd_bc = 1 ; gcd_ac = 1 ; gcd_abc = 1 ;
def preCal(a, b, c):
gcd_ab = gcd(a, b);
gcd_bc = gcd(c, b);
gcd_ac = gcd(a, c);
gcd_abc = gcd(((a * b) / gcd_ab), c);
def divTermCount(a, b, c, num):
return ((num / a) + (num / b) + (num / c)
- (num / ((a * b) / gcd_ab))
- (num / ((c * b) / gcd_bc))
- (num / ((a * c) / gcd_ac))
+ (num / ((((a * b) / gcd_ab) * c) / gcd_abc)));
def findNthTerm(a, b, c, n):
preCal(a, b, c);
mid = 0 ;
high = max (a, max (b, c)) * n;
low = gcd_abc * n;
while (low < high):
mid = low + (high - low) / 2 ;
if (divTermCount(a, b, c, mid) < n):
low = mid + 1 ;
else :
high = mid;
return int (low);
a = 2 ; b = 3 ; c = 5 ; n = 100 ;
print (findNthTerm(a, b, c, n));
|
C#
using System;
class GFG
{
static long gcd_ab, gcd_bc, gcd_ac, lcm_abc, lcm_ab, lcm_bc, lcm_ac;
static long gcd( long a, long b)
{
if (a == 0)
{
return b;
}
return gcd(b % a, a);
}
static void preCal( long a, long b, long c) {
gcd_ab = gcd(a, b);
gcd_bc = gcd(c, b);
gcd_ac = gcd(a, c);
lcm_ab = ((a * b) / gcd_ab);
lcm_bc = ((c * b) / gcd_bc);
lcm_ac = ((a * c) / gcd_ac);
lcm_abc = (lcm_ab * c) / gcd(lcm_ab, c);
}
static long divTermCount( long a, long b,
long c, long num)
{
return ((num / a) + (num / b) + (num / c)
- (num / lcm_ab)
- (num / lcm_bc)
- (num / lcm_ac)
+ (num / lcm_abc));
}
static long findNthTerm( int a, int b,
int c, long n)
{
preCal(a, b, c);
long low = 1, high, mid;
high = a > b ? (a > c ? a : c) : (b > c ? b : c);
high = high * n;
low = gcd(a, gcd_bc) * n;
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 = 100;
Console.WriteLine(findNthTerm(a, b, c, n));
}
}
|
Javascript
<script>
function gcd( a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
var gcd_ab, gcd_bc, gcd_ac, lcm_abc, lcm_ab, lcm_bc, lcm_ac;
function preCal(a, b, c) {
gcd_ab = gcd(a, b);
gcd_bc = gcd(c, b);
gcd_ac = gcd(a, c);
lcm_ab = ((a * b) / gcd_ab);
lcm_bc = ((c * b) / gcd_bc);
lcm_ac = ((a * c) / gcd_ac);
lcm_abc = (lcm_ab * c) / gcd(lcm_ab, c);
}
function divTermCount( a, b, c, num)
{
return parseInt((num / a) + (num / b) + (num / c)
- (num / lcm_ab)
- (num / lcm_bc)
- (num / lcm_ac)
+ (num / lcm_abc));
}
function findNthTerm( a, b, c, n)
{
preCal(a, b, c);
var low = 1, high, mid;
high = a > b ? (a > c ? a : c) : (b > c ? b : c);
high = high * n;
low = gcd(a, gcd_bc) * n;
while (low < high) {
mid = low + (high - low) / 2;
if (divTermCount(a, b, c, mid) < n)
low = mid + 1;
else
high = mid;
}
return low;
}
var a = 2, b = 3, c = 5, n = 100;
document.write(parseInt(findNthTerm(a, b, c, n)));
</script>
|
Time Complexity: O(logn + log(min(a, b))
Auxiliary Space: O(1)
Another Approach:
- Initialize three variables a, b, and c to the values of A, B, and C, respectively.
- Initialize a counter variable count to 0.
- Initialize a variable num to 0.\
- While count is less than N, do the following:
- Find the minimum value among a, b, and c, and assign it to num.
- If num is equal to a, increment a by A.
- If num is equal to b, increment b by B.
- If num is equal to c, increment c by C.
- Increment the count by 1.
- Return num.
C++
#include<bits/stdc++.h>
using namespace std;
int findNthMultiple( int A, int B, int C, int N){
int a = A, b = B, c = C, count = 0, num = 0;
while (count < N){
num = (a < b) ? (a < c ? a : c) : (b < c ? b : c);
if (num == a) a += A;
if (num == b) b += B;
if (num == c) c += C;
count++;
}
return num;
}
int main(){
int A = 2, B = 3, C = 5, N = 7;
cout << "The " << N << "th multiple of " << A << ", " << B << " or " << C << " is: " << findNthMultiple(A, B, C, N);
return 0;
}
|
C
#include<stdio.h>
int findNthMultiple( int A, int B, int C, int N){
int a = A, b = B, c = C, count = 0, num = 0;
while (count < N){
num = (a < b) ? (a < c ? a : c) : (b < c ? b : c);
if (num == a) a += A;
if (num == b) b += B;
if (num == c) c += C;
count++;
}
return num;
}
int main(){
int A = 2, B = 3, C = 5, N = 7;
printf ( "The %dth multiple of %d, %d or %d is: %d" , N, A, B, C, findNthMultiple(A, B, C, N));
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int findNthMultiple( int A, int B, int C, int N){
int a = A, b = B, c = C, count = 0 , num = 0 ;
while (count < N){
num = (a < b) ? (a < c ? a : c) : (b < c ? b : c);
if (num == a) a += A;
if (num == b) b += B;
if (num == c) c += C;
count++;
}
return num;
}
public static void main(String[] args) {
int A = 2 , B = 3 , C = 5 , N = 7 ;
System.out.println( "The " + N + "th multiple of " + A + ", " + B + " or " + C + " is: " + findNthMultiple(A, B, C, N));
}
}
|
Python3
def findNthMultiple(A, B, C, N):
a = A
b = B
c = C
count = 0
num = 0
while count < N:
num = min (a, b, c)
if num = = a:
a + = A
if num = = b:
b + = B
if num = = c:
c + = C
count + = 1
return num
A = 2
B = 3
C = 5
N = 7
print (f "The {N}th multiple of {A}, {B}, or {C} is: {findNthMultiple(A, B, C, N)}" )
|
C#
using System;
class MainClass {
static int FindNthMultiple( int A, int B, int C, int N)
{
int a = A, b = B, c = C, count = 0, num = 0;
while (count < N) {
num = (a < b) ? (a < c ? a : c)
: (b < c ? b : c);
if (num == a)
a += A;
if (num == b)
b += B;
if (num == c)
c += C;
count++;
}
return num;
}
static void Main()
{
int A = 2, B = 3, C = 5, N = 7;
Console.WriteLine(
"The {0}th multiple of {1}, {2} or {3} is: {4}" ,
N, A, B, C, FindNthMultiple(A, B, C, N));
}
}
|
Javascript
function findNthMultiple(A, B, C, N) {
let a = A,
b = B,
c = C,
count = 0,
num = 0;
while (count < N) {
num = Math.min(a, b, c);
if (num == a) a += A;
if (num == b) b += B;
if (num == c) c += C;
count++;
}
return num;
}
let A = 2,
B = 3,
C = 5,
N = 7;
console.log( "The " + N + "th multiple of " + A + ", " + B + " or " + C + " is: " + findNthMultiple(A, B, C, N));
|
Output
The 7th multiple of 2, 3 or 5 is: 9
Time complexity: The time complexity of this algorithm is O(N), because we have to iterate through the loop N times.
Auxiliary Space: The space complexity of this algorithm is O(1), because we are not using any additional data structures to store the variables.
Last Updated :
24 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...