Cryptogram Puzzle

There are a variety of tools that can be employed to solve cryptograms, just like there are lots of techniques that can be used to solve puzzles like Sudoku. These include: Algebraic tehniques, number theoory, factorization, digital roots, elimination, carries, divisibility rules, modular arithmetic, the Chinese Remainder Theorem, and even simple trial and error.

I have to confess that, when I saw the puzzle, I adopted the lazy technique of simply brute forcing it and trying every possible combination of numbers!

As we know that each of the nine letters is distinct, the number of permuations is just 9 factiorial. This is a very manegable 362,880 equations to test. As the brute force approach will try every possible combination of digits, this will mean that this approach will find all possible solutions.

Many programming languages now have built in commands to generate permutations and combinations. The language I was using did not, so I went back to first principles. The code to generate permutations is not very complicated (especially if you are comfortable with recursion). For our puzzle there are nine possible letters, and nine digits. There are nine possible values the first letter can be, and once this is selected, there are eight possible for the next digit, and seven for the next … You can see how we just have to loop through: 9 × 8 × 7 × 6 × … × 2 × 1 = 9! permutations.

The number of permutations is, of course, fixed at 362,880 but the ordering they appear can be very different from row to row depending on the technique we use to generate them. There is an very clever algorithm for generating permutations, called Heap’s Algorithm. This is named after the creator Heapsort which was invented, but not named after, J.W.J. Williams the following year).

What is really clever about the Heap algorithm for generating permutations is that each subsequent permutation differs from the previous perumation by the simple switching of a pair of values! In an analogous way to Gray Code, the number of number of swaps needed to transition from each row to the next is small.

The Heap algorithm can be implemented recursively, or non-recursively, and, because each subsquestion row simply switches two values, the algorithm is perfectly efficient with amount of swapping that needs to occur.

An example of the algorithm in use is displayed on the right to show how it generates the 24 possible permutations for four numbers. The algorithm starts out at the top with 1234 and ends with 2341. If you look at each row you will see that two numbers stay the same, and two change.