On Tue, Sep 11, 2012 at 7:56 PM, Roger Hui <[email protected]> wrote:

> I am curious about how you did your simulation.  What language?  How much
> code?

I just transliterated Jordan's solution into code. The language is
Racket (a descendant of Scheme). The amount of code is gigantic
compared to J, but it is all straightforward, took only a few minutes.
to write.

Thanks to all for the interesting thread.

Cheers
P.

=====

(define (test number-of-prisoners rounds)
  (let loop ((round rounds) (days-sum 0.0) (days-min +inf.0) (days-max -inf.0))
    (if (zero? round)
      (values (/ days-sum rounds) days-min days-max)
      (let ((days (prisoners number-of-prisoners)))
        (loop (sub1 round)
              (+ days-sum days)
              (min days days-min)
              (max days days-max))))))

(define (prisoners number-of-prisoners)
  (let loop ((days 0) (count (sub1 number-of-prisoners)) (switch 'off)
(alreay-counted (hash)))
    (if (zero? count)
      days
      (let ((who (random number-of-prisoners)))
        (if (zero? who)
          (if (eq? switch 'on)
            (loop (add1 days) (sub1 count) 'off alreay-counted)
            (loop (add1 days) count 'off alreay-counted))
          (if (eq? switch 'on)
            (loop (add1 days) count 'on alreay-counted)
            (if (hash-has-key? alreay-counted who)
              (loop (add1 days) count 'off alreay-counted)
              (loop (add1 days) count 'on (hash-set alreay-counted who
#t)))))))))

;;; EOF
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to