Although this was a programming question I want the mathematical intuition behind it. So we were given two numbers $n$ and $k$. We were asked to find out $k{-th}$ number **not divisible** by $n$.
For example, $n=3$ and $k=7$. If we see numbers $1$ to $12$. $$1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12$$ Then $3,6,9$ are divisible by $3$ so we remove it. So we are left with $1, 2, 4, 5, 7, 8, 10, 11$. Clearly the $7th$ number not divisible by $3$ is $10$. So answer is $10$. It turns out the answer is: $$k+⌊\tfrac{k-1}{n-1}⌋$$I want the mathematical intuition behind this formula.
==== old answer ===
Assume the answer is $M$.
Then there are $k$ numbers less than or equal to $M$ that are not divisible by $n$.
So $k = f(M)$ where $f(M)$ is the function that tells you how many number less than or equal to $M$ are not divisible by $n$.
So if we can "undo" $f(M)$ to get $f^{-1}(k) = M$ we can solve for $M$.
Now there is a unique number $m$ so that $M = m*n + r$ where $0\le r < n$. That's just division.
In other words, $m*n \le M < (m+1)n$. But as $n$ doesn't divide $M$ we have.
$m*n < M < (m+1)n$.
And the numbers $n, 2n, 3n,.....,mn$ are all divisible by $n$. And all the rest of the numbers are not.
So there are $M- m = k$ numbers that are not divisible $n$ that are equal or less than $M$.
So what is $m$?
So $mn < M < (m+1)n$ so $m < \frac Mn < m+1$ so $m = \lfloor \frac Mn \rfloor$.
And we have $M - \lfloor \frac Mn \rfloor = k$
So now we must solve for $M$ in terms of $n$ and $k$.
Let's go.....
$M - \lfloor \frac Mn \rfloor = k$
$\lfloor \frac Mn \rfloor = M-k$
$M-k < \frac Mn < M-k +1$ (We know $n\not \mid M$ so we have strict inequalities)
$Mn-kn < M < Mn -kn + n$
$-kn < M-Mn < -kn +n$
$kn- n < Mn-M < kn$
$(k-1)\frac n{n-1} < M < k\frac n{n-1}$
Now $\frac n{n-1} < 1$ so there is (apparently) exactly one integer, $M$ so that $(k-1)\frac n{n-1} < M$ and that $k\frac n{n-1} > M$.
$k\frac n{n-1} = k\frac {(n-1)+1}{n-1} = k(1 + \frac 1{n-1})= k + \frac k{n-1}$
And $(k-1)\frac n{n-1} = (k-1)(1 + \frac 1{n-1}) = k-1 + \frac {k-1}{n-1}=k + \frac{k-n}{n-1}$.
So we have the only integer $M$ so that $k + \frac {k-n}{n-1} < M < k + \frac {k}{n-1}$.
If $\frac k{n-1}$ is an integer then $k+\frac k{n-1}$ is too big.
But if $h \le k-1$ and $\frac h{n-1}$ is an integer then $M \ge k + \frac h{n-1}$.
So $M$ is the $k + $ the largest integer less than or equal to $\frac {k-1}{n-1}$
So $M = k + \lfloor \frac {k-1}{n-1} \rfloor$.
=====
Actually that last bit was pretty confusing.
Go back to $(k-1)\frac n{n-1} < M < k\frac n{n-1}$
Now lets jump ahead and consider $k + \lfloor \frac {k-1}{n-1}\rfloor$
Let $h = \lfloor \frac {k-1}{n-1}\rfloor$.
So $h\le \frac {k-1}{n-1}< \frac {k}{n-1}$
And $\frac {k-1}{n-1} - 1=\frac {k-n}{n-1} < h$.
So $k + \frac {k-n}{n-1}< k + \lfloor \frac {k-1}{n-1}\rfloor <k+\frac {k}{n-1}$
But also $(k-1)\frac n{n-1} < M < k\frac n{n-1}$.
But there is at most one integer in that range. So as both $M$ and $k + \lfloor \frac {k-1}{n-1}\rfloor$ are both integers, they must be equal.