- Related Questions & Answers
- Program to find nth term of a sequence which are divisible by a, b, c in Python
- Program to find N-th term of series a, b, b, c, c, c…in C++
- Find nth number that contains the digit k or divisible by k in C++
- Find Nth term (A matrix exponentiation example) in C++
- Find nth term of a given recurrence relation in C++
- C++ program to find nth term of the series 5, 2, 13 41,...
- Program to print pentatope numbers upto Nth term in C
- Program to find nth term in Look and Say Sequence in Python
- Find nth term of the Dragon Curve Sequence in C++
- C++ program to find Nth term of series 1, 4, 15, 72, 420…
- C++ program to find Nth term of the series 1, 5, 32, 288 …
- C++ program to find Nth term of the series 1, 8, 54, 384…
- C++ program to find Nth term of the series 3, 14, 39, 84…
- Find nth term of a given recurrence relation in Python
- C++ program to find Nth term of the series 1, 6, 18, 40, 75, ….

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we are given three numbers A, B, and N. Our task is to create a program to find Nth term divisible by A or B in C++.

The Nth Term divisible by A or B. Here, we will find the term n number term which is divisible by number A or B. For this, we will count till nth numbers that are divisible by A or B.

**Let’s take an example to understand the problem,**

A = 4, B = 3, N = 5

9

The terms the are divisible by 3 and 4 are −

3, 4, 6, 8, 9, 12, …

5th term is 9.

To find the nth term that is divisible by A or B. We can simply find numbers divisible by A or B and the nth term in the series is our answer.

#include<iostream> using namespace std; int findNTerm(int N, int A, int B) { int count = 0; int num = 1; while( count < N){ if(num%A == 0 || num%B == 0) count++; if(count == N) return num; num++; } return 0; } int main(){ int N = 12, A = 3, B = 4; cout<<N<<"th term divisible by "<<A<<" or "<<B<<" is "<<findNTerm(N, A, B)<<endl; }

12th term divisible by 3 or 4 is 24

Another approach to solving the problem could be using the Binary search to find the Nth element which is divisible by A or B. We will find the Nth term using the formula−

NTerm = maxNum/A + maxNum/B + maxNum/lcm(A,B)

And based on the value of Nterm, we will check if we need to traverse in the numbers less than maxNum or greater than maxNum.

#include <iostream> using namespace std; int findLCM(int a, int b) { int LCM = a, i = 2; while(LCM % b != 0) { LCM = a*i; i++; } return LCM; } int findNTerm(int N, int A, int B) { int start = 1, end = (N*A*B), mid; int LCM = findLCM(A, B); while (start < end) { mid = start + (end - start) / 2; if ( ((mid/A) + (mid/B) - (mid/LCM)) < N) start = mid + 1; else end = mid; } return start; } int main() { int N = 12, A = 3, B = 4; cout<<N<<"th term divisible by "<<A<<" or "<<B<<" is "<<findNTerm(N, A, B); }

12th term divisible by 3 or 4 is 24

Advertisements