Given two integers A and N, our task is to find the Nth natural number which is not divisible by A.
Input: A = 4, N = 12
The series starting from 1 excluding the multiples of A would be 1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17 and the 12th term which is not divisible by 4 is 15.
Input: A = 3, N = 20
The series starting from 1 excluding the multiples of A would be 1, 2, 4, 5, 7, 8, 10, 11 and so on and the Nth number which is not divisible by 3 is 29.
To solve the problem mentioned above we have to observe that there is a gap after every (A – 1) integer as the multiples of A are skipped. To find that the number N lies in which set we divide N by (A – 1), and store it in a variable lets say quotient. Now multiplying A with that variable and adding the remainder to it we will get the resultant answer.
If A = 4, N = 7: quotient = 7 / 3 = 2 remainder = 7 % 3 = 1 So the answer is (A * quotient) + remainder = 4 * 2 + 1 = 9
But if the remainder is 0 it means it is the last element in the set. Lets us understand it by an example.
If A = 4, N = 6: quotient = 6 / 3 = 2 remainder = 6 % 3 = 0 So the answer is (A * quotient) - 1 = 4 * 2 - 1 = 7
Below is the implementation of the above approach:
- Find permutation of n which is divisible by 3 but not divisible by 6
- Find Nth number in a sequence which is not a multiple of a given number
- Find the Kth number which is not divisible by N
- Sum of first N natural numbers which are divisible by 2 and 7
- Sum of first N natural numbers which are divisible by X or Y
- Count natural numbers whose factorials are divisible by x but not y
- Find the first natural number whose factorial is divisible by x
- Sum of first N natural numbers which are not powers of K
- Find nth number that contains the digit k or divisible by k.
- Find Nth smallest number that is divisible by 100 exactly K times
- Split a number as sum of K numbers which are not divisible by K
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Program to find Nth term divisible by a or b
- Find the Nth term divisible by a or b or c
- Count numbers in range 1 to N which are divisible by X but not by Y
- Count of numbers in a range that does not contain the digit M and which is divisible by M.
- Sum of first K numbers which are not divisible by N
- Find N digits number which is divisible by D
- Greatest divisor which divides all natural number in range [L, R]
- Find maximum N such that the sum of square of first N natural numbers is not more than X
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.