How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the EchoLisp programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the EchoLisp 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 EchoLisp programming language
Source code in the echolisp programming language
(define (trabb-fun n)
(+ (* 5 n n n) (sqrt(abs n))))
(define (check-trabb n)
(if (number? n)
(if (<= (trabb-fun n) 400)
(printf "🌱 f(%d) = %d" n (trabb-fun n))
(printf "❌ f(%d) = %d" n (trabb-fun n)))
(error "not a number" n)))
(define (trabb (numlist null))
(while (< (length numlist) 11)
(set! numlist (append numlist
(or
(read default: (shuffle (iota 11))
prompt: (format "Please enter %d more numbers" (- 11 (length numlist))))
(error 'incomplete-list numlist))))) ;; users cancel
(for-each check-trabb (reverse (take numlist 11))))
(trabb)
;; input : (0 4 1 8 5 9 10 3 6 7 2)
🌱 f(2) = 41.41421356237309
❌ f(7) = 1717.6457513110645
❌ f(6) = 1082.4494897427833
🌱 f(3) = 136.73205080756887
❌ f(10) = 5003.162277660168
❌ f(9) = 3648
❌ f(5) = 627.2360679774998
❌ f(8) = 2562.828427124746
🌱 f(1) = 6
🌱 f(4) = 322
🌱 f(0) = 0
;; extra credit : let's find the threshold
(lib 'math)
(define (g x) (- (trabb-fun x) 400))
(root g 0 10)
→ 4.301409367213084
You may also check:How to resolve the algorithm Set step by step in the Racket programming language
You may also check:How to resolve the algorithm Search a list step by step in the Fortran programming language
You may also check:How to resolve the algorithm Sockets step by step in the Perl programming language
You may also check:How to resolve the algorithm Unicode strings step by step in the Tcl programming language
You may also check:How to resolve the algorithm Copy a string step by step in the EDSAC order code programming language