Given two integers **N** and **K**, the task is to find the K^{th} number which is not divisible by N.

**Note:** The value of N is greater than 1, because every number is divisible by 1.

**Examples:**

Input:N = 3, K = 6

Output:8

Explanation:

Numbers which is not divisible by N = 3 – {1, 2, 4, 5, 7, 8, 10}

6th non-divisible number by 3 is 8.

Input:N = 7, K = 97

Output:113

Explanation:

Numbers which is not divisible by N = 7 – {1, 2, 4, 5, 6, ….}

97th non-divisible number by 7 is 113.

**Naive Approach:** A simple solution is to iterate over a loop to find the K^{th} non-divisible number by N. Below is the steps to find the K^{th} number:

- Intialize the count of non-divisible number and current number to 0.
- Iterate using a while loop until the count of the non divisible number is not equal to K.
- Increment the count of the non-divisible number by 1, If the current number is not divisible by N.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find ` `// the K'th non divisible ` `// number by N ` ` ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Function to find ` `// the K'th non divisible ` `// number by N ` `int` `kthNonDivisible(` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `find = 0; ` ` ` `int` `j = 0; ` ` ` ` ` `// Loop to find the K non ` ` ` `// divisible number by N ` ` ` `while` `(find != K) { ` ` ` `j++; ` ` ` `if` `(j % N != 0) ` ` ` `find++; ` ` ` `} ` ` ` `return` `j; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 3; ` ` ` `int` `K = 6; ` ` ` `cout << kthNonDivisible(N, K); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find ` `// the K'th non divisible ` `// number by N ` `class` `GFG{ ` ` ` `// Function to find ` `// the K'th non divisible ` `// number by N ` `static` `int` `kthNonDivisible(` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `find = ` `0` `; ` ` ` `int` `j = ` `0` `; ` ` ` ` ` `// Loop to find the K non ` ` ` `// divisible number by N ` ` ` `while` `(find != K) ` ` ` `{ ` ` ` `j++; ` ` ` `if` `(j % N != ` `0` `) ` ` ` `find++; ` ` ` `} ` ` ` `return` `j; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `3` `; ` ` ` `int` `K = ` `6` `; ` ` ` ` ` `System.out.print(kthNonDivisible(N, K)); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find ` `# the K'th non divisible ` `# number of N ` `import` `math ` ` ` `# Function to find the Kth ` `# not divisible by N ` `def` `kthNonDivisible(n, K): ` ` ` ` ` `find ` `=` `0` ` ` `j ` `=` `0` ` ` ` ` `# Loop to find the K non ` ` ` `# divisible number by N ` ` ` `while` `find !` `=` `K: ` ` ` `j ` `=` `j ` `+` `1` ` ` `if` `j ` `%` `N !` `=` `0` `: ` ` ` `find ` `=` `find ` `+` `1` ` ` ` ` `return` `j ` ` ` `# Driver Code ` `N ` `=` `3` `K ` `=` `6` ` ` `# Function Call ` `print` `(kthNonDivisible(N, K)) ` ` ` `# This code is contributed by ishayadav181` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the ` `// K'th non-divisible number by N ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// Function to find the K'th ` `// non divisible number by N ` `static` `int` `kthNonDivisible(` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `find = 0; ` ` ` `int` `j = 0; ` ` ` ` ` `// Loop to find the K non ` ` ` `// divisible number by N ` ` ` `while` `(find != K) ` ` ` `{ ` ` ` `j++; ` ` ` ` ` `if` `(j % N != 0) ` ` ` `find++; ` ` ` `} ` ` ` `return` `j; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 3; ` ` ` `int` `K = 6; ` ` ` ` ` `Console.Write(kthNonDivisible(N, K)); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

**Output:**

8

**Efficient Approach:** The key observation in the problem is that every number from 1 to N-1 is not divisible by N and then Similarly, N + 1 to 2*N – 1 is also not divisible by N. Keeping this in mind, the K^{th} number not divisible by N will be:

Below is the implementation of the above approach:

## C++

`// C++ implementation to find ` `// the K'th non-divisible ` `// number of N ` ` ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` `// Function to find the Kth ` `// not divisible by N ` `int` `kthNonDivisible(` `int` `N, ` `int` `K) ` `{ ` ` ` `return` `K + ` `floor` `((K - 1) / (N - 1)); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 3; ` ` ` `int` `K = 6; ` ` ` ` ` `// Function Call ` ` ` `cout << kthNonDivisible(N, K); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// K'th non-divisible number of N ` `class` `GFG{ ` ` ` `// Function to find the Kth ` `// not divisible by N ` `static` `int` `kthNonDivisible(` `int` `N, ` `int` `K) ` `{ ` ` ` `return` `(` `int` `) (K + Math.floor((K - ` `1` `) / (N - ` `1` `))); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `3` `; ` ` ` `int` `K = ` `6` `; ` ` ` ` ` `// Function Call ` ` ` `System.out.print(kthNonDivisible(N, K)); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find ` `# the K'th non-divisible ` `# number of N ` `import` `math ` ` ` `# Function to find the Kth ` `# not divisible by N ` `def` `kthNonDivisible(N, K): ` ` ` ` ` `return` `K ` `+` `math.floor((K ` `-` `1` `) ` `/` `(N ` `-` `1` `)) ` ` ` `# Driver Code ` `N ` `=` `3` `K ` `=` `6` ` ` `# Function Call ` `print` `(kthNonDivisible(N, K)) ` ` ` `# This code is contributed by ishayadav181 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the ` `// K'th non-divisible number of N ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the Kth ` `// not divisible by N ` `static` `int` `kthNonDivisible(` `int` `N, ` `int` `K) ` `{ ` ` ` `return` `(` `int` `) (K + Math.Floor((` `double` `)(K - 1) / ` ` ` `(N - 1))); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 3; ` ` ` `int` `K = 6; ` ` ` ` ` `// Function Call ` ` ` `Console.Write(kthNonDivisible(N, K)); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

8

## Recommended Posts:

- Find if a number is divisible by every number in a list
- Find the Nth natural number which is not divisible by A
- Find the number closest to n and divisible by m
- Find nth number that contains the digit k or divisible by k.
- Find N digits number which is divisible by D
- Find a N-digit number such that it is not divisible by any of its digits
- Find a N-digit number such that it is not divisible by any of its digits
- Find the first natural number whose factorial is divisible by x
- Find the maximum number of elements divisible by 3
- Find Nth smallest number that is divisible by 100 exactly K times
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Find permutation of n which is divisible by 3 but not divisible by 6
- Number of ways to split a binary number such that every part is divisible by 2
- Minimum number of swaps required to make a number divisible by 60
- Number of digits to be removed to make a number divisible by 3
- Check if a number is divisible by all prime divisors of another number
- Check if a large number is divisible by a number which is a power of 2
- Number of substrings with length divisible by the number of 1's in it
- Find the Nth term divisible by a or b or c

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.