;; This code is written in FlooP. To run it, first open interpreter.scm ;; in DrRacket and type (start), then type (load-quietly "wondrous.txt") ;; at the ==> interpreter prompt. (define procedure "wondrous?" (n) (block 0 begin (if (n = 0) then (quit block 0)) ((cell 0) <= 0) (mu-loop (block 1 begin (print n) (if (n = 1) then (block 2 begin (output <= yes) (print "took" (cell 0) "steps") (quit block 0))) ((cell 0) <= ((cell 0) + 1)) (if ((remainder n 2) = 0) then (block 3 begin (n <= (quotient n 2)) (quit block 1))) (n <= ((3 * n) + 1)))))) (define procedure "count-steps" (n) (block 0 begin (if (n = 0) then (quit block 0)) ((cell 0) <= 0) (mu-loop (block 1 begin (if (n = 1) then (block 2 begin (output <= (cell 0)) (quit block 0))) ((cell 0) <= ((cell 0) + 1)) (if ((remainder n 2) = 0) then (block 3 begin (n <= (quotient n 2)) (quit block 1))) (n <= ((3 * n) + 1)))))) (define procedure "most-wondrous-up-to" (limit) (block 0 begin ((cell 0) <= 1) ((cell 1) <= 0) ((cell 2) <= 1) ((cell 3) <= 0) (loop limit times (block 1 begin ((cell 1) <= (count-steps (cell 0))) (if ((cell 1) > (cell 2)) then (block 2 begin ((cell 2) <= (cell 1)) ((cell 3) <= (cell 0)) )) ((cell 0) <= ((cell 0) + 1)))) (print "most wondrous number found is" (cell 3)) (print "which took" (cell 2) "steps") (output <= (cell 3))))