The TPK algorithm is a simple program introduced by Donald Knuth and Luis Trabb Pardo to illustrate the evolution of computer programming languages. In their 1977 work "The Early Development of Programming Languages", Trabb Pardo and Knuth introduced a small program that involved arrays, indexing, mathematical functions, subroutines, I/O, conditionals and iteration. They then wrote implementations of the algorithm in several early programming languages to show how such concepts were expressed.
To explain the name "TPK", the authors referred to Grimm's law (which concerns the consonants 't', 'p', and 'k'), the sounds in the word "typical", and their own initials (Trabb Pardo and Knuth).[1] In a talk based on the paper, Knuth said:[2]
You can only appreciate how deep the subject is by seeing how good people struggled with it and how the ideas emerged one at a time. In order to study this—Luis I think was the main instigator of this idea—we take one program—one algorithm—and we write it in every language. And that way from one example we can quickly psych out the flavor of that particular language. We call this the TPK program, and well, the fact that it has the initials of Trabb Pardo and Knuth is just a funny coincidence.
We introduced a simple procedure called the “TPK algorithm,” and gave the flavor of each language by expressing TPK in each particular style. […] The TPK algorithm inputs eleven numbers ; then it outputs a sequence of eleven pairs where
This simple task is obviously not much of a challenge, in any decent computer language.
In pseudocode:
ask for 11 numbers to be read into a sequence Sreverse sequence Sfor eachitemin sequence Scall a function to do an operation
ifresult overflows
alert user
elseprintresult
The algorithm reads eleven numbers from an input device, stores them in an array, and then processes them in reverse order, applying a user-defined function to each value and reporting either the value of the function or a message to the effect that the value has exceeded some threshold.
Implementations
Implementations in the original paper
In the original paper, which covered "roughly the first decade" of the development of high-level programming languages (from 1945 up to 1957), they gave the following example implementation "in a dialect of ALGOL 60", noting that ALGOL 60 was a later development than the languages actually discussed in the paper:[1]
As many of the early high-level languages could not handle the TPK algorithm exactly, they allow the following modifications:[1]
If the language supports only integer variables, then assume that all inputs and outputs are integer-valued, and that sqrt(x) means the largest integer not exceeding .
If the language does not support alphabetic output, then instead of the string 'TOO LARGE', output the number 999.
If the language does not allow any input and output, then assume that the 11 input values have been supplied by an external process somehow, and the task is to compute the 22 output values (with 999 replacing too-large values of ).
If the language does not allow programmers to define their own functions, then replace f(a[i]) with an expression equivalent to .
This shows a C implementation equivalent to the above ALGOL 60.
#include<math.h>#include<stdio.h>doublef(doublet){returnsqrt(fabs(t))+5*pow(t,3);}intmain(void){doublea[11]={0},y;for(inti=0;i<11;i++)scanf("%lf",&a[i]);for(inti=10;i>=0;i--){y=f(a[i]);if(y>400)printf("%d TOO LARGE\n",i);elseprintf("%d %.16g\n",i,y);}}
Published in Encyclopedia of Computer Science and Technology, Jack Belzer, Albert G. Holzman, and Allen Kent (eds.), Vol. 6, pp. 419-493. Dekker, New York, 1977.