Trabb Pardo–Knuth Algorithm
Last Updated :
25 Apr, 2023
This article is not about learning some new complex algorithm, its more about the history of programming. TPK was introduced to illustrate the evolution of computer programming languages. By the time you are finished with this article you will have learnt something about the history of programming rather than a new concept. 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. This program was written in several early programming language to show the evolution of programming languages. Just like the "Hello World!" program has the purpose of introducing beginners to programming the TPK has the same purpose and has no practical applications.
Algorithm:
input 11 numbers into a sequence A
reverse sequence A
for each item in sequence A
call a function to do an operation
if result overflows
alert user
else
print result
C++
#include <iostream>
#include <cmath>
using namespace std;
// f(x) = sqrt(|x|) + 5*x**3
double f(double x)
{
return (sqrt(fabs(x)) + 5.0 * pow(x, 3.0));
}
int main()
{
double y;
int i;
// Read in the values of the array A
double A[11] = {7.9, 7.3, 20.9, 112.0, 5.0, 3.0, 2.9, 9.0, 21.7, 31.2, 4.1};
// In reverse order, apply "f"
// to each element of A and print
for (i=10; i>=0; i--)
{
y = f(A[i]);
if (y > 400.0)
{
cout<<i<<" TOO LARGE\n";
}
else
{
cout<<i<<" " <<y<<"\n";
}
}
return 0;
}
// This code is contributed by shiv1o43g
C
// C program to implement TPK algorithm
#include <stdio.h>
#include <math.h>
// f(x) = sqrt(|x|) + 5*x**3
double f (double x)
{
return (sqrt(fabs(x)) + 5.0 * pow(x, 3.0));
}
int main (int argc, char* argv[])
{
double y;
int i;
// Read in the values of the array A
double A[11] = {7.9, 7.3, 20.9, 112.0, 5.0, 3.0, 2.9, 9.0, 21.7, 31.2, 4.1};
// In reverse order, apply "f"
// to each element of A and print
for (i=10; i>=0; i--)
{
y = f (A[i]);
if (y > 400.0)
{
printf ("%d TOO LARGE\n", i);
}
else
{
printf ("%d %f\n", i, y);
}
}
return (0);
}
Java
// Java program to implement TPK algorithm
import java.util.*;
import java.io.*;
public class TPKA {
public static void main(String... args) {
double[] input = {7.9, 7.3, 20.9, 112.0, 5.0, 3.0, 2.9, 9.0, 21.7, 31.2, 4.1};
for(int j = 10; j >= 0; j--) {
double y = f(input[j]);
if( y < 400.0) {
System.out.printf("%d %.2f\n", j, y);
} else {
System.out.printf("%d %s\n", j, "TOO LARGE");
}
}
}
private static double f(double x) {
return Math.pow(Math.abs(x), 0.5) + (5*(Math.pow(x, 3)));
}
}
Python
# Python program to implement TPK algorithm
def f(x):
return abs(x) ** 0.5 + 5 * x**3
def main():
s = [7.9, 7.3, 20.9, 112.0, 5.0, 3.0, 2.9, 9.0, 21.7, 31.2, 4.1]
s.reverse()
i = 10
for x in s:
result = f(x)
if result > 400:
print('%d %s' % (i, "TOO LARGE"))
i = i-1
else:
print('%d %f' % (i, result))
i = i-1
print('')
if __name__ == '__main__':
main()
C#
// C# program to implement TPK algorithm
using System;
public class TPKAlgorithm {
public static double F(double x) {
return Math.Sqrt(Math.Abs(x)) + 5 * Math.Pow(x, 3);
}
public static void Main() {
double[] s = { 7.9, 7.3, 20.9, 112.0, 5.0, 3.0, 2.9, 9.0, 21.7, 31.2, 4.1 };
Array.Reverse(s);
int i = 10;
foreach (double x in s) {
double result = F(x);
if (result > 400) {
Console.WriteLine($"{i} TOO LARGE");
i--;
}
else {
Console.WriteLine($"{i} {result:F6}");
i--;
}
}
Console.WriteLine();
}
}
// This code is contributed by shivregkec
JavaScript
function f(x) {
return Math.pow(Math.abs(x), 0.5) + (5*(Math.pow(x, 3)));
}
const input = [7.9, 7.3, 20.9, 112.0, 5.0, 3.0, 2.9, 9.0, 21.7, 31.2, 4.1];
for(let j = 10; j >= 0; j--) {
let y = f(input[j]);
if(y < 400.0) {
console.log(`${j} ${y.toFixed(2)}`);
} else {
console.log(`${j} TOO LARGE`);
}
}
// This code is contributed by shivhack999
Output10 346.63
9 TOO LARGE
8 TOO LARGE
7 TOO LARGE
6 123.648
5 136.732
4 TOO LARGE
3 TOO LARGE
2 TOO LARGE
1 TOO LARGE
0 TOO LARGE
References: http://cs.fit.edu/~ryan/compare/tpk-c.html
Similar Reads
Preparata Algorithm Preparata's algorithm is a recursive Divide and Conquer Algorithm where the rank of each input key is computed and the keys are outputted according to their ranks. C++ m[i, j] := M[i, j] for 1 <= i, j <= n in parallel; for r : = 1 to logn do { Step 1. In parallel set q[i, j, k] := m[i, j] + m[
14 min read
Prim's Algorithm in Python Prim's algorithm is a greedy algorithm used to find the Minimum Spanning Tree (MST) of a connected, undirected graph. The MST is a subset of the edges that connects all vertices in the graph with the minimum possible total edge weight.The algorithm starts with an empty spanning tree.The idea is to m
5 min read
Boothâs Multiplication Algorithm Booth's algorithm is a multiplication algorithm that multiplies two signed binary numbers in 2's complement notation. Booth used desk calculators that were faster at shifting than adding and created the algorithm to increase their speed. Boothâs algorithm is of interest in the study of computer arch
15 min read
How to solve RSA Algorithm Problems? RSA algorithm is an asymmetric cryptography algorithm which means, there should be two keys involve while communicating, i.e., public key and private key. There are simple steps to solve problems on the RSA Algorithm. Example-1: Step-1: Choose two prime number p and q Lets take p = 3 and q = 11 Step
8 min read
Warnsdorff's algorithm for Knightâs tour problem Problem : A knight is placed on the first block of an empty board and, moving according to the rules of chess, must visit each square exactly once. Following is an example path followed by Knight to cover all the cells. The below grid represents a chessboard with 8 x 8 cells. Numbers in cells indica
15+ min read