Permutations

In his 1953 short story, The Nine Billion Names of God, Arthur C. Clarke writes of Tibetan llamas who seek to know all the names of God. They’ve been writing down the names for centuries, but upon the advent of the computer, they enlist western science to help them finish the task in days. You can complete the same task with your computer and the C programming language, but in hours instead of days.

In the story, Clarke cites the algorithm for printing out all the possible name combinations. Rather than just spit out names AAAAAAAAA through ZZZZZZZZZ, some rules are added to eliminate repetitive letters and other stipulations made by the monks.

The task of generating such output is accomplished easily in any computer language: Use nested loops to generate permutations of AAAAAAAAA through ZZZZZZZZZ. The greater the number of characters, the long it takes the program to run. If you display the results, the program runs longer. Printing takes longest.

A permutation includes all possible results of a series such as AAAAAAAAA through ZZZZZZZZZ. A combination is limited to only certain results. So in The Nine Billion Names of God, the programmers generate combinations.

In the following code, three nested for loops generate all permutations of aaa through zzz, what I like to call the TLA list, for Three Letter Acronyms:

#include <stdio.h>

int main()
{
    char tla[4] = "\0";

    for(tla[0]='a';tla[0]<='z';tla[0]++)
        for(tla[1]='a';tla[1]<='z';tla[1]++)
            for(tla[2]='a';tla[2]<='z';tla[2]++)
                printf("%s ",tla);
    putchar('\n');

    return(0);
}

The char array tla[] at Line 5 is initialized to a null string, which ensures that a null character (\0) terminates the string.

In addition to holding the output, tla[]‘s elements are also used as variables in the for loops. That makes the for statements look a bit complex.

Each for loop spins from the letter 'a' through 'z', inclusive, which is why the <= operator is used.

At Line 10, the printf() statement displays each result. Then a final newline is displayed at Line 11, then the program quits.

The output is all one line of text, from aaa through zzz, and on a modern computer the code takes only a few milliseconds to run.

You can increase the number of characters in the permutation by adding another nested for loop. That addition increases the time the program takes to run: On my system, the 3-character TLA program ran in 0.011 seconds, but the 4-character TLA program took 0.074 seconds. Adding a fifth character increased the program’s runtime to 1.941 seconds.

As a challenge, a sort-of mini-Exercise, modify the code so that it doesn’t generate a single line of text as output; add code that spits out a newline after each 20th TLA is displayed. Please try this Exercise on your own; click here to view my solution, though many solutions are possible.

Leave a Reply