TPK algorithm

Last updated

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.

Contents

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.

The algorithm

Knuth describes it as follows: [3]

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]

TPK:beginintegeri;realy;realarraya[0:10];realproceduref(t);realt;valuet;f:=sqrt(abs(t))+5×t3;fori:=0step1until10doread(a[i]);fori:=10step-1until0dobeginy:=f(a[i]);ify>400thenwrite(i,'TOO LARGE')elsewrite(i,y);endendTPK.

As many of the early high-level languages could not handle the TPK algorithm exactly, they allow the following modifications: [1]

With these modifications when necessary, the authors implement this algorithm in Konrad Zuse's Plankalkül, in Goldstine and von Neumann's flow diagrams, in Haskell Curry's proposed notation, in Short Code of John Mauchly and others, in the Intermediate Program Language of Arthur Burks, in the notation of Heinz Rutishauser, in the language and compiler by Corrado Böhm in 1951–52, in Autocode of Alick Glennie, in the A-2 system of Grace Hopper, in the Laning and Zierler system, in the earliest proposed Fortran (1954) of John Backus, 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, in Fortran of John Backus, in ARITH-MATIC and MATH-MATIC from Grace Hopper'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. [1]

Implementations in more recent languages

C implementation

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);}}

Python implementation

This shows a Python implementation.

frommathimportsqrtdeff(t):returnsqrt(abs(t))+5*t**3a=[float(input())for_inrange(11)]fori,tinreversed(list(enumerate(a))):y=f(t)print(i,"TOO LARGE"ify>400elsey)

Rust implementation

This shows a Rust implementation.

usestd::{io,iter::zip};fnf(t: f64)-> f64{t.abs().sqrt()+5.0*t.powi(3)}fnmain(){letmuta=[0f64;11];for(t,input)inzip(&muta,io::stdin().lines()){*t=input.unwrap().parse().unwrap();}a.iter().enumerate().rev().for_each(|(i,&t)|matchf(t){yify>400.0=>println!("{i} TOO LARGE"),y=>println!("{i} {y}"),});}

Related Research Articles

<span class="mw-page-title-main">ALGOL</span> Family of programming languages

ALGOL is a family of imperative computer programming languages originally developed in 1958. ALGOL heavily influenced many other languages and was the standard method for algorithm description used by the Association for Computing Machinery (ACM) in textbooks and academic sources for more than thirty years.

<span class="mw-page-title-main">Discrete Fourier transform</span> Type of Fourier transform in discrete mathematics

In mathematics, the discrete Fourier transform (DFT) converts a finite sequence of equally-spaced samples of a function into a same-length sequence of equally-spaced samples of the discrete-time Fourier transform (DTFT), which is a complex-valued function of frequency. The interval at which the DTFT is sampled is the reciprocal of the duration of the input sequence. An inverse DFT (IDFT) is a Fourier series, using the DTFT samples as coefficients of complex sinusoids at the corresponding DTFT frequencies. It has the same sample-values as the original input sequence. The DFT is therefore said to be a frequency domain representation of the original input sequence. If the original sequence spans all the non-zero values of a function, its DTFT is continuous, and the DFT provides discrete samples of one cycle. If the original sequence is one cycle of a periodic function, the DFT provides all the non-zero values of one DTFT cycle.

<span class="mw-page-title-main">Hash function</span> Mapping arbitrary data to fixed-size values

A hash function is any function that can be used to map data of arbitrary size to fixed-size values, though there are some hash functions that support variable length output. The values returned by a hash function are called hash values, hash codes, hash digests, digests, or simply hashes. The values are usually used to index a fixed-size table called a hash table. Use of a hash function to index a hash table is called hashing or scatter storage addressing.

In quantum computing, Grover's algorithm, also known as the quantum search algorithm, is a quantum algorithm for unstructured search that finds with high probability the unique input to a black box function that produces a particular output value, using just evaluations of the function, where is the size of the function's domain. It was devised by Lov Grover in 1996.

In arithmetic and computer programming, the extended Euclidean algorithm is an extension to the Euclidean algorithm, and computes, in addition to the greatest common divisor (gcd) of integers a and b, also the coefficients of Bézout's identity, which are integers x and y such that

In machine learning, the perceptron is an algorithm for supervised learning of binary classifiers. A binary classifier is a function which can decide whether or not an input, represented by a vector of numbers, belongs to some specific class. It is a type of linear classifier, i.e. a classification algorithm that makes its predictions based on a linear predictor function combining a set of weights with the feature vector.

In numerical analysis, the Kahan summation algorithm, also known as compensated summation, significantly reduces the numerical error in the total obtained by adding a sequence of finite-precision floating-point numbers, compared to the obvious approach. This is done by keeping a separate running compensation, in effect extending the precision of the sum by the precision of the compensation variable.

The AKS primality test is a deterministic primality-proving algorithm created and published by Manindra Agrawal, Neeraj Kayal, and Nitin Saxena, computer scientists at the Indian Institute of Technology Kanpur, on August 6, 2002, in an article titled "PRIMES is in P". The algorithm was the first one which is able to determine in polynomial time, whether a given number is prime or composite and this without relying on mathematical conjectures such as the generalized Riemann hypothesis. The proof is also notable for not relying on the field of analysis. In 2006 the authors received both the Gödel Prize and Fulkerson Prize for their work.

ALGOL 60 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 key advance in the rise of structured programming. ALGOL 60 was one of the first languages implementing function definitions. ALGOL 60 function definitions could be nested within one another, with lexical scope. It gave rise to many other languages, including CPL, PL/I, Simula, BCPL, B, Pascal, and C. Practically every computer of the era had a systems programming language based on ALGOL 60 concepts.

In computer programming, a statement is a syntactic unit of an imperative programming language that expresses some action to be carried out. A program written in such a language is formed by a sequence of one or more statements. A statement may have internal components.

In number theory, Dixon's factorization method is a general-purpose integer factorization algorithm; it is the prototypical factor base method. Unlike for other factor base methods, its run-time bound comes with a rigorous proof that does not rely on conjectures about the smoothness properties of the values taken by a polynomial.

ALGO is an algebraic programming language developed for the Bendix G-15 computer.

Autocode is the name of a family of "simplified coding systems", later called programming languages, devised in the 1950s and 1960s for a series of digital computers at the Universities of Manchester, Cambridge and London. Autocode was a generic term; the autocodes for different machines were not necessarily closely related as are, for example, the different versions of the single language Fortran.

ALTRAN is a programming language for the formal manipulation of rational functions of several variables with integer coefficients. It was developed at Bell Labs in 1960s. ALTRAN is a FORTRAN version of ALPAK rational algebra package, and “can be thought of as a variant of FORTRAN with the addition of an extra declaration, the ‘algebraic’ type declaration.”

Shanks' square forms factorization is a method for integer factorization devised by Daniel Shanks as an improvement on Fermat's factorization method.

In computing, a procedural parameter is a parameter of a procedure that is itself a procedure.

<span class="mw-page-title-main">Recursion (computer science)</span> Use of functions that call themselves

In computer science, recursion is a method of solving a computational problem where the solution depends on solutions to smaller instances of the same problem. Recursion solves such recursive problems by using functions that call themselves from within their own code. The approach can be applied to many types of problems, and recursion is one of the central ideas of computer science.

The power of recursion evidently lies in the possibility of defining an infinite set of objects by a finite statement. In the same manner, an infinite number of computations can be described by a finite recursive program, even if this program contains no explicit repetitions.

In computer science, the longest increasing subsequence problem aims to find a subsequence of a given sequence in which the subsequence's elements are sorted in an ascending order and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous or unique. The longest increasing subsequences are studied in the context of various disciplines related to mathematics, including algorithmics, random matrix theory, representation theory, and physics. The longest increasing subsequence problem is solvable in time where denotes the length of the input sequence.

XPL0 is a computer programming language that is essentially a cross between Pascal and C. It looks somewhat like Pascal but works more like C. It was created in 1976 by Peter J. R. Boyle who wanted a high-level language for his microcomputer and wanted something more sophisticated than BASIC, which was the dominant language for personal computers at the time.

<span class="mw-page-title-main">Fast inverse square root</span> Root-finding algorithm

Fast inverse square root, sometimes referred to as Fast InvSqrt or by the hexadecimal constant 0x5F3759DF, is an algorithm that estimates , the reciprocal of the square root of a 32-bit floating-point number in IEEE 754 floating-point format. The algorithm is best known for its implementation in 1999 in Quake III Arena, a first-person shooter video game heavily based on 3D graphics. With subsequent hardware advancements, especially the x86 SSE instruction rsqrtss, this algorithm is not generally the best choice for modern computers, though it remains an interesting historical example.

References

  1. 1 2 3 4 Luis Trabb Pardo and Donald E. Knuth, "The Early Development of Programming Languages".
    • First published August 1976 in typewritten draft form, as Stanford CS Report STAN-CS-76-562
    • 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.
    • Reprinted ( doi:10.1016/B978-0-12-491650-0.50019-8) in A History of Computing in the Twentieth Century, N. Metropolis, J. Howlett, and G.-C. Rota (eds.), New York, Academic Press, 1980. ISBN   0-12-491650-3
    • Reprinted with amendments as Chapter 1 of Selected Papers on Computer Languages, Donald Knuth, Stanford, CA, CSLI, 2003. ISBN   1-57586-382-0)
  2. "A Dozen Precursors of Fortran", lecture by Donald Knuth, 2003-12-03 at the Computer History Museum: Abstract, video
  3. Donald Knuth, TPK in INTERCAL, Chapter 7 of Selected Papers on Fun and Games, 2011 (p. 41)