Given four integers **A**, **N**, **L** and **R**, the task is to find the **N** th number in a sequence of consecutive integers from **L** to **R** which is not a multiple of **A**. It is given that the sequence contain at least **N** numbers which are not divisible by **A** and the integer **A** is always greater than 1.**Examples:**

Input:A = 2, N = 3, L = 1, R = 10Output:5Explanation:

The sequence is1, 2,3, 4,5, 6,7, 8,9and 10. Here 5 is the third number which is not a multiple of 2 in the sequence.Input:A = 3, N = 6, L = 4, R = 20Output:11Explanation :

11 is the 6th number which is not a multiple of 3 in the sequence.

**Naive Approach:** The naive approach is to iterate over the range **[L, R]** in a loop to find the **N ^{th}** number. The steps are:

- Intialize the count of non-multiple number and current number to 0.
- Iterate over the range
**[L, R]**until the count of the non-multiple number is not equal to**N**. - Increment the count of the non-multiple number by 1, If the
**current number is not divisible by A**.

Below is the implementation of the above approach:

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
`// Function to find Nth number not a ` `// multiple of A in the range [L, R] ` `void` `count_no (` `int` `A, ` `int` `N, ` `int` `L, ` `int` `R) `
`{ ` ` ` ` ` `// To store the count `
` ` `int` `count = 0; `
` ` `int` `i = 0; `
` ` `// To check all the nos in range `
` ` `for` `(i = L; i < R + 1; i++) `
` ` `{ `
` ` `if` `(i % A != 0) `
` ` `count += 1; `
` ` ` ` `if` `(count == N) `
` ` `break` `; `
` ` `} `
` ` `cout << i; `
`} ` `// Driver code ` `int` `main() `
`{ ` ` ` ` ` `// Given values of A, N, L, R `
` ` `int` `A = 3, N = 6, L = 4, R = 20; `
` ` ` ` `// Function Call `
` ` `count_no (A, N, L, R); `
` ` `return` `0; `
`} ` `// This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

`// Java program for the above approach ` `import` `java.util.*;`
`import` `java.io.*;`
`class` `GFG{`
` ` `// Function to find Nth number not a ` `// multiple of A in the range [L, R] ` `static` `void` `count_no (` `int` `A, ` `int` `N, `
` ` `int` `L, ` `int` `R)`
`{ ` ` ` `// To store the count `
` ` `int` `count = ` `0` `; `
` ` `int` `i = ` `0` `; `
` ` `// To check all the nos in range `
` ` `for` `(i = L; i < R + ` `1` `; i++) `
` ` `{ `
` ` `if` `(i % A != ` `0` `) `
` ` `count += ` `1` `; `
` ` ` ` `if` `(count == N) `
` ` `break` `; `
` ` `} `
` ` `System.out.println(i); `
`} ` `// Driver code ` `public` `static` `void` `main(String[] args) `
`{` ` ` ` ` `// Given values of A, N, L, R `
` ` `int` `A = ` `3` `, N = ` `6` `, L = ` `4` `, R = ` `20` `; `
` ` `// Function call `
` ` `count_no (A, N, L, R); `
`}` `}` `// This code is contributed by sanjoy_62` |

*chevron_right*

*filter_none*

`# Python3 program for the above approach ` `# Function to find Nth number not a ` `# multiple of A in the range [L, R] ` `def` `count_no (A, N, L, R): `
` ` `# To store the count `
` ` `count ` `=` `0`
` ` `# To check all the nos in range `
` ` `for` `i ` `in` `range` `( L, R ` `+` `1` `): `
` ` `if` `( i ` `%` `A !` `=` `0` `): `
` ` `count ` `+` `=` `1`
` ` `if` `( count ` `=` `=` `N ): `
` ` `break`
` ` `print` `( i ) `
` ` `# Given values of A, N, L, R ` `A, N, L, R ` `=` `3` `, ` `6` `, ` `4` `, ` `20`
`# Function Call ` `count_no (A, N, L, R) ` |

*chevron_right*

*filter_none*

`// C# program for the above approach ` `using` `System;`
`class` `GFG{`
` ` `// Function to find Nth number not a ` `// multiple of A in the range [L, R] ` `static` `void` `count_no (` `int` `A, ` `int` `N, `
` ` `int` `L, ` `int` `R)`
`{ ` ` ` ` ` `// To store the count `
` ` `int` `count = 0; `
` ` `int` `i = 0; `
` ` `// To check all the nos in range `
` ` `for` `(i = L; i < R + 1; i++) `
` ` `{ `
` ` `if` `(i % A != 0) `
` ` `count += 1; `
` ` ` ` `if` `(count == N) `
` ` `break` `; `
` ` `} `
` ` `Console.WriteLine(i); `
`} ` `// Driver code ` `public` `static` `void` `Main() `
`{` ` ` ` ` `// Given values of A, N, L, R `
` ` `int` `A = 3, N = 6, L = 4, R = 20; `
` ` `// Function call `
` ` `count_no (A, N, L, R); `
`}` `}` `// This code is contributed by sanjoy_62` |

*chevron_right*

*filter_none*

**Output:**

11

**Time Complexity:** *O(R – L)* **Auxiliary Space:** *O(1)*

**Efficient Approach:**

The key observation is that there are **A – 1** numbers that are not divisible by **A** in the range **[1, A – 1]**. Similarly, there are **A – 1** numbers not divisible by **A** in range** [A + 1, 2 * A – 1], [2 * A + 1, 3 * A – 1]** and so on.

With the help of this observation, the **N ^{th}** number which is not divisible by

**A**will be:

To find the value in the range [ **L, R** ], we need to shift the origin from ‘0’ to ‘L – 1’, thus we can say that the N^{th} number which is not divisible by A in the range will be :

However there is an edge case, when the value of **( L – 1 ) + N + floor( ( N – 1 ) / ( A – 1 ) )** itself turns out to be multiple of a **‘A’**, in that case Nth number will be :

Below is the implementation of the above approach:

`# Python3 program for the above approach` `import` `math`
`# Function to find Nth number` `# not a multiple of A in range [L, R]` `def` `countNo (A, N, L, R):`
` ` `# Calculate the Nth no`
` ` `ans ` `=` `L ` `-` `1` `+` `N \`
` ` `+` `math.floor( ( N ` `-` `1` `) ` `/` `( A ` `-` `1` `) )`
` ` ` ` `# Check for the edge case`
` ` `if` `ans ` `%` `A ` `=` `=` `0` `:`
` ` `ans ` `=` `ans ` `+` `1` `;`
` ` `print` `(ans)`
`# Input parameters ` `A, N, L, R ` `=` `5` `, ` `10` `, ` `4` `, ` `20`
`# Function Call` `countNo(A, N, L, R)` |

*chevron_right*

*filter_none*

**Output:**

16

**Time Complexity:** *O(1)* **Auxiliary Space:** *O(1)*

## Recommended Posts:

- Find the Nth natural number which is not divisible by A
- Find First element in AP which is multiple of given prime
- Find nth term of the Dragon Curve Sequence
- Program to find Nth term of the Van Eck's Sequence
- Nth number whose sum of digit is multiple of 10
- Check if there exists any sub-sequence in a string which is not palindrome
- Mth bit in Nth binary string from a sequence generated by the given operations
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Find if the given number is present in the infinite sequence or not
- Find minimum length sub-array which has given sub-sequence in it
- Find original sequence from Array containing the sequence merged many times in order
- Find the multiple of x which is closest to a^b
- Smallest multiple of 3 which consists of three given non-zero digits
- Finding nth term of any Polynomial Sequence
- Count the occurrence of Nth term in first N terms of Van Eck's sequence
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum
- Find the smallest positive number which can not be represented by given digits
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Smallest N digit number which is a multiple of 5

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.

**Article Tags :**