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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the Wren 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 Wren programming language

Source code in the wren programming language

import "io" for Stdin, Stdout
import "/fmt" for Fmt

var f = Fn.new { |x| x.abs.sqrt + 5*x*x*x }

var s = List.filled(11, 0)
System.print("Please enter 11 numbers:")
var count = 0
while (count < 11) {
    Fmt.write("  Number $-2d : ", count + 1)
    Stdout.flush()
    var number = Num.fromString(Stdin.readLine())
    if (!number) {
        System.print("Not a valid number, try again.")
    } else {
        s[count] = number
        count = count + 1
    }
}
s = s[-1..0]
System.print("\nResults:")
for (item in s) {
    var fi = f.call(item)
    if (fi <= 400) {
        Fmt.print("  f($6.3f) = $7.3f", item, fi)
    } else {
        Fmt.print("  f($6.3f) = overflow", item)
    }
}

  

You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Java programming language
You may also check:How to resolve the algorithm Lychrel numbers step by step in the Fortran programming language
You may also check:How to resolve the algorithm Loops/Nested step by step in the Maple programming language
You may also check:How to resolve the algorithm Colour bars/Display step by step in the Ada programming language
You may also check:How to resolve the algorithm Determine if a string is collapsible step by step in the Quackery programming language