Saturday, February 15, 2014

A neat little result.

While going over some homework as a TA in computer science 2 at the University of Iceland, I noticed something I did not know.

We already know that (assuming integer division) that
\[\left( \frac{x}{{10}^i} \right) \mod 10 \]

gives us the \((n-i)\)-th digit of a number (\(n =\left\lfloor{\log_{10}(x)} \right\rfloor \), and  \( i = 0 \) gives you the last digit) when counting in a decimal system. But going over the homework batch where the students were supposed to implement radix sort, I noticed that

\[ \frac{x \mod {10}^{i+1}}{{10}^i} \]

gives you the \(i\)-th digit (where 0 gives you the first)! Pretty cool!

I'm not going to prove this, as I have far to little experience with integer division and the modulus operation, but it seems to work, judging by that my students queues all turn out sorted.

No comments:

Post a Comment