## Trabb Pardo Knuth Algorithm

### April 27, 2012

We’ll give two solutions. Scheme uses lists, not arrays, as its basic aggregate data type, so our first version of the program uses lists:

`(define (tpk len)`

(define (f x) (+ (sqrt (abs x)) (* 5 x x x)))

(let loop ((len len) (nums '()))

(if (positive? len)

(loop (- len 1) (cons (read) nums))

(for-each

(lambda (x)

(let ((result (f x)))

(display (if (< 400 result) "TOO LARGE" result))

(newline)))

nums))))

Here’s an example:

`> (tpk 11)`

TOO LARGE

322.0

136.732050807569

41.4142135623731

6.0

0.0

-4.0

-38.5857864376269

-133.267949192431

-318.0

-622.7639320225

Although we used lists, the Trabb Pardo Knuth algorithm is intended to be used with arrays. Here is the fully-imperative array version in Scheme:

`(define (read-number len)`

(let ((vec (make-vector len 0)))

(do ((i 0 (+ i 1)) (num (read) (read)))

((or (= i len)(eof-object? num)) vec)

(vector-set! vec i num))))

`(define (vector-reverse vec)`

(do ((lo 0 (+ lo 1))

(hi (- (vector-length vec) 1) (- hi 1)))

((<= hi lo) vec)

(let ((t (vector-ref vec lo)))

(vector-set! vec lo (vector-ref vec hi))

(vector-set! vec hi t))))

`(define (f num) (+ (sqrt (abs num)) (* 5 num num num)))`

`(define (tpk len)`

(let ((vec (vector-reverse (read-number len))))

(do ((i 0 (+ i 1))) ((= i len))

(let ((result (f (vector-ref vec i))))

(display (if (< 400 result) "TOO LARGE" result))

(newline)))))

Here, `read-number`

returns a vector of `len`

numbers, `vector-reverse`

reverses the vector by repeatedly swapping from the left and right, and `tpk`

encodes the algorithm. Output from `(tpk 11)`

is the same as for the prior version.

You can run the program at http://programmingpraxis.codepad.org/8OFS3rH7. It is also available at http://ideone.com/gOKJ7, where you can actually see input and output of the program.

[…] today’s Programming Praxis exercise, our goal is to implement a simple algorithm that could serve as an […]

My Haskell solution (see http://bonsaicode.wordpress.com/2012/04/27/programming-praxis-trabb-pardo-knuth-algorithm/ for a version with comments):

Implementation in C. The algorithm doesn’t check if overflow occurred when reading input from the user

Here’s a fairly literal implementation in Python 2.7:

Note: “input()” is generally not safe, because it actually executes the user input.

[…] latest Programming Praxis Exercise is interesting. Back in 1973, Luis Trabb Pardo and Don Knuth published an algorithm that was meant […]

Almost the same as Mike’s, here as Python3. My personal preference is with fewer intermediate variables in this case.

‘input’ in Python3 is the same as ‘raw_input’ in Python2, i.e. it doesn’t eval the input, but just returns it as a string. This means I cast to float before passing to my function ‘sqrt’. I broaden the scope of error catching from OverflowError to all ValueErrors, which includes failure to convert the entered string into a float.

Remco, I think a Haskell solution truer to the spirit of the exercise would probably use something like GHC’s exception system to handle numeric overflow. Yours forbids it, rather than actually dealing with it.

Forth version, just don’t enter a double precision number.

Execution

my implementation in Python:

def tbk():

s = raw_input("Enter 11 numbers separated by space: ")

s = s.split(‘ ‘)

s.reverse()

for i in s:

try:

result = pow(9.9, int(i))

except OverflowError:

print "Overflow error for {}".format(i)

else:

print result

Benjamin, see the link “Howto: posting source code” in the blog titlebar for how to post source code in your comments. Personally I found the third of the three described methods simplest.

I saw that one after asking here. Should have search around before asking. Haha! Thanks anyway!

In Scala:

`

Seq.fill(11){Console.readInt}.reverse.map(i => Try(sqrt(i)).foreach {

case Success(root) => println(root)

case Failure(_) => println(“Could not compute sqrt”)

}

`