How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the Swift programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the Swift programming language

Table of Contents

Problem Statement

The TPK algorithm is an early example of a programming chrestomathy. It was used in Donald Knuth and Luis Trabb Pardo's Stanford tech report The Early Development of Programming Languages. The report traces the early history of work in developing computer languages in the 1940s and 1950s, giving several translations of the algorithm. From the wikipedia entry: The task is to implement the algorithm:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the Swift programming language

Source code in the swift programming language

import Foundation

print("Enter 11 numbers for the Trabb─Pardo─Knuth algorithm:")

let f: (Double) -> Double = { sqrt(fabs($0)) + 5 * pow($0, 3) }

(1...11)
    .generate()
    .map { i -> Double in
        print("\(i): ", terminator: "")
        guard let s = readLine(), let n = Double(s) else { return 0 }
        return n
    }
    .reverse()
    .forEach {
        let result = f($0)
        print("f(\($0))", result > 400.0 ? "OVERFLOW" : result, separator: "\t")
    }


  

You may also check:How to resolve the algorithm Execute a Markov algorithm step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Polynomial regression step by step in the D programming language
You may also check:How to resolve the algorithm Stack step by step in the Forth programming language
You may also check:How to resolve the algorithm File size step by step in the Maple programming language
You may also check:How to resolve the algorithm Function frequency step by step in the Common Lisp programming language