Number Base Conversion

Algorithm
This is an algorithm for converting natural numbers from one base to another.

Let $n \in \N$ be the number to be converted to base $m$, $m \in \N, m \ge 2$.

Let $x \bmod m$ be the modular operation $m$ on $x$, that is, the remainder of $x$ on division by $m$.

Let $\left[{b_m b_{m-1} \ldots b_2 b_1 b_0}\right]_2$ be the base $m$ representation of $n$.


 * Step 1:

Take $n \bmod m$. Call this number $a_1$

If the remainder is $0$, then $b_0$ is $0$. Take $\dfrac {n} m$ and go to step $2$. Call this number $a_1$.

If the remainder is $1$, then $b_0$ is $1$. Take $\dfrac {n-1} {m}$ and go to step $2$. Call this number $a_1$.


 * $\vdots$

If the remainder is $m-1$, then $b_0$ is the letter representing $m-1$. Take $\dfrac {n-\left({m-1}\right)} {m}$ and go to step $2$. Call this number $a_1$.


 * Step 2:

If $a_1 = 0$, stop.

If $a_1 \ne 0$, take $a_1 \bmod m$.

If the remainder is $0$, then $b_1$ is $0$. Take $\dfrac {a_1} m$ and go to step $3$. Call this number $a_2$

If the remainder is $1$, then $b_1$ is $1$. Take $\dfrac {a_1-1} {m}$ and go to step $3$. Call this number $a_2$


 * $\vdots$

If the remainder is $m-1$, then $b_1$ is the letter representing $m-1$. Take $\dfrac {a_1-\left({m-1}\right)} {m}$ and go to step $3$. Call this number $a_2$.


 * $\vdots$

Continue this process until some step $z + 1$ gives $a_z = 0$, then stop.

Example
To convert $25$ from decimal to octal:

Hence the digits of the octal representation of $25$ are, from right to left, $1$ and $3$.

This is why mathematicians confuse Halloween and Christmas.