I was going through a solution on code chef in which we needed to remove a $1$ from a position say $x$ (in Base $B$) from a number in Base $10$ if the representation of that number in base $B$ had a digit $\geq 1$.
One way is to change the base to $B$ and check and simply do it!
This guy subtracts $B^{x}$ (where Base is $B$ in which 1 was to be removed and x is the position of $1$ (starting from $0$)) from the decimal representation if and only if after subtracting it from the number , the number is $\ge 0$ .
Now, I know $B^{x}$ represents the $1$ at position $x$ in base $10$.
But I cannot think of the logic behind it . Can you help?
Your number $n$ in base $b$ is written \begin{equation} a_0 + a_1 b + a_2 b^2 + \dots a_N b^N \end{equation} for some $N \in \mathbb{N}$ and $a_i \in \{0, \dots, b-1\}$.
So when you subtract $1$ to the $x$th digit, you actually do : \begin{equation} a_0 + a_1 x + a_2 b^2 + \dots + (a_x - 1) b^x + \dots + a_N b^N. \end{equation}
That way you can easily see that \begin{equation} (a_0 + a_1 x + a_2 b^2 + \dots a_N b^N) - b^x = a_0 + a_1 x + a_2 b^2 + \dots + (a_x - 1) b^x + \dots + a_N b^N. \end{equation}
For what to do when $a_x=0$ or $n - b^x < 0$, this depends very much on what you try to achieve, and why you're doing this computation for.