The TPK algorithm is a simple
program introduced by
Donald Knuth
Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist and mathematician. He is a professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of comp ...
and
Luis Trabb Pardo to illustrate the evolution of computer
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s. In their 1977 work "The Early Development of Programming Languages", Trabb Pardo and Knuth introduced a small program that involved
arrays
An array is a systematic arrangement of similar objects, usually in rows and columns.
Things called an array include:
{{TOC right
Music
* In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
, indexing, mathematical
functions,
subroutine
In computer programming, a function (also procedure, method, subroutine, routine, or subprogram) is a callable unit of software logic that has a well-defined interface and behavior and can be invoked multiple times.
Callable units provide a ...
s,
I/O,
conditionals and
iteration
Iteration is the repetition of a process in order to generate a (possibly unbounded) sequence of outcomes. Each repetition of the process is a single iteration, and the outcome of each iteration is then the starting point of the next 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
Grimm's law, also known as the First Germanic Consonant Shift or First Germanic Sound Shift, is a set of sound laws describing the Proto-Indo-European (PIE) stop consonants as they developed in Proto-Germanic in the first millennium BC, first d ...
(which concerns the consonants 't', 'p', and 'k'), the sounds in the word "typical", and their own initials (Trabb Pardo and Knuth).
In a talk based on the paper, Knuth said:
The algorithm
Knuth describes it as follows:
[Donald Knuth, ''TPK in INTERCAL'', Chapter 7 of ''Selected Papers on Fun and Games'', 2011 (p. 41)]
In pseudocode:
ask for 11 numbers to be read into a sequence ''S''
reverse sequence ''S''
for each ''item'' in sequence ''S''
call a function to do an operation
if ''result'' overflows
alert user
else
print ''result''
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
ALGOL 60 (short for ''Algorithmic Language 1960'') is a member of the ALGOL family of computer programming languages. It followed on from ALGOL 58 which had introduced code blocks and the begin and end pairs for delimiting them, representing a ...
", noting that ALGOL 60 was a later development than the languages actually discussed in the paper:
TPK: begin integer i; real y; real array a :10
real procedure f(t); real t; value t;
f := sqrt(abs(t)) + 5 × t ↑ 3;
for i := 0 step 1 until 10 do read(a ;
for i := 10 step -1 until 0 do
begin y := f(a ;
if y > 400 then write(i, 'TOO LARGE')
else write(i, y);
end
end TPK.
As many of the early high-level languages could not handle the TPK algorithm exactly, they allow the following modifications:
* 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
with an expression equivalent to
.
With these modifications when necessary, the authors implement this algorithm in
Konrad Zuse
Konrad Ernst Otto Zuse (; ; 22 June 1910 – 18 December 1995) was a German civil engineer, List of pioneers in computer science, pioneering computer scientist, inventor and businessman. His greatest achievement was the world's first programm ...
's
Plankalkül, in
Goldstine and
von Neumann's
flow diagrams, in
Haskell Curry
Haskell Brooks Curry ( ; September 12, 1900 – September 1, 1982) was an American mathematician, logician and computer scientist. Curry is best known for his work in combinatory logic, whose initial concept is based on a paper by Moses Schönfin ...
's proposed notation, in
Short Code of
John Mauchly
John William Mauchly ( ; August 30, 1907 – January 8, 1980) was an American physicist who, along with J. Presper Eckert, designed ENIAC, the first general-purpose electronic digital computer, as well as EDVAC, BINAC and UNIVAC I, the f ...
and others, in the Intermediate Program Language of
Arthur Burks, in the notation of
Heinz Rutishauser
Heinz Rutishauser (30 January 1918 – 10 November 1970) was a Swiss people, Swiss mathematician and a pioneer of modern numerical mathematics and computer science.
Life
Rutishauser's father died when he was 13 years old and his mother died t ...
, in the language and compiler by
Corrado Böhm
Corrado Böhm (17 January 1923 – 23 October 2017) was an Italian computer scientist and Professor Emeritus at the Sapienza University of Rome, University of Rome "La Sapienza", known especially for his contributions to the theory of structured ...
in 1951–52, in
Autocode of
Alick Glennie, in the
A-2 system of
Grace Hopper
Grace Brewster Hopper (; December 9, 1906 – January 1, 1992) was an American computer scientist, mathematician, and United States Navy rear admiral. She was a pioneer of computer programming. Hopper was the first to devise the theory of mach ...
, in the
Laning and Zierler system, in the earliest proposed
Fortran (1954) of
John Backus
John Warner Backus (December 3, 1924 – March 17, 2007) was an American computer scientist. He led the team that invented and implemented FORTRAN, the first widely used high-level programming language, and was the inventor of the Backus–N ...
, in the
Autocode for
Mark 1 by
Tony Brooker, in ПП-2 of
Andrey Ershov, in BACAIC of Mandalay Grems and R. E. Porter, in Kompiler 2 of A. Kenton Elsworth and others, in ADES of E. K. Blum, the Internal Translator of
Alan Perlis
Alan Jay Perlis (April 1, 1922 – February 7, 1990) was an American computer scientist and professor at Purdue University, Carnegie Mellon University and Yale University. He is best known for his pioneering work in programming languages and was t ...
, in
Fortran of John Backus, in
ARITH-MATIC and
MATH-MATIC
MATH-MATIC is the marketing name for the AT-3 (Algebraic Translator 3) compiler, an early programming language for the UNIVAC I and UNIVAC II.
MATH-MATIC was written beginning around 1955 by a team led by Charles Katz under the direction of Gra ...
from
Grace Hopper
Grace Brewster Hopper (; December 9, 1906 – January 1, 1992) was an American computer scientist, mathematician, and United States Navy rear admiral. She was a pioneer of computer programming. Hopper was the first to devise the theory of mach ...
's lab, in the system of
Bauer and
Samelson, and (in addenda in 2003 and 2009) PACT I and TRANSCODE. They then describe what kind of arithmetic was available, and provide a subjective rating of these languages on parameters of "implementation", "readability", "control structures", "data structures", "machine independence" and "impact", besides mentioning what each was the first to do.
Implementations in more recent languages
C implementation
This shows a C implementation equivalent to the above ALGOL 60.
#include
#include
double f(double t)
int main(void)
Python implementation
This shows a Python implementation.
from math import sqrt
def f(t):
return sqrt(abs(t)) + 5 * t**3
a = loat(input()) for _ in range(11)for i, t in reversed(list(enumerate(a))):
y = f(t)
print(i, "TOO LARGE" if y > 400 else y)
Rust
Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH) ...
implementation
This shows a Rust implementation.
use std::;
fn f(t: f64) -> Option
fn main()
References
External links
Implementations in many languagesat ''
Rosetta Code
Rosetta Code is a wiki-based programming chrestomathy website with implementations of common algorithms and solutions to various computer programming, programming problems in many different programming languages. It is named for the Rosetta Stone ...
''
Implementations in several languages
{{DEFAULTSORT:Trabb Pardo-Knuth algorithm
1977 in computing
Donald Knuth
Articles with example ALGOL 60 code
Test items in computer languages
Computer programming folklore
Articles with example Python (programming language) code