My close personal friend and colleague Encsé posted a fun little programming exercise and invited us to send him solutions in our favorite Turing-complete tool, especially if it happens to be mod_rewrite or C++ templates. Unfortunately, I don’t think there is a single sane person on the planet who would consider C++ their favorite anything, and I like Encsé better than to let him needlessly associate with any additional crazy people. Obviously, this meant that I had to delve into the murky depths of template madness myself.
Here is a copy of the problem, in case Encsé decides to delete his post once he finds out it has been tainted with C++ — which would be an entirely sensible (and, indeed, healthy) reaction:
Find a number consisting of 9 digits in which each of the digits from 1 to 9 appears only once. This number must also satisfy these divisibility requirements:
- The number should be divisible by 9.
- If the rightmost digit is removed, the remaining number should be divisible by 8.
- If the rightmost digit of the new number is removed, the remaining number should be divisible by 7.
- And so on, until there’s only one digit (which will necessarily be divisible by 1).
OK, so I implemented a solution using C++ template metaprogramming. It was actually easier than I thought, and the experience was surprisingly interesting. Which is not to say I’d like to do it again. I imagine I would feel this exact same way after performing an autopsy.
