[LUAU] doing 'e'
Jim Thompson
jim at netgate.com
Wed Nov 30 08:56:48 PST 2005
Jim Thompson wrote:
> or lisp
>
> (setq service_list (mapcar #'string-downcase TempServiceList))
Lets see a perl/python/... equivalent to this lisp exercise for
obtaining 'e':
* (defun uniform ()
"the uniform random variable on the interval (0,1)"
(random 1.0 ))
UNIFORM
* (defun wait-time (&optional ignore-me)
"the number of uniform random variates when their sum first exceeds 1"
(do ((count 0 (1+ count))
(total 0 (+ total (uniform))))
((> total 1) count)))
WAIT-TIME
;; I put the optional parameter in wait-time to support this use:
* (mapcar #'wait-time '(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1))
(2 2 2 2 2 2 2 3 3 4 3 2 4 3 2)
;; The wait-times are smallish integers. Let's try to see what their
average is.
* (defun avg-wait-time (trials)
(let ((sum 0))
(dotimes (i trials) (incf sum (wait-time)))
(/ sum trials 1.0)))
AVG-WAIT-TIME
;; Can you guess the limiting value of the average wait time?
* (mapcar #'avg-wait-time '(1 10 100 1000 10000 100000 1000000))
(2.0 2.9 2.71 2.726 2.7118 2.71774 2.719146)
* (mapcar #'avg-wait-time '(1 10 100 1000 10000 100000 1000000 10000000))
(2.0 3.0 2.77 2.71 2.7093 2.72287 2.717509 2.7183373)
* (mapcar #'avg-wait-time '(1 10 100 1000 10000 100000 1000000 10000000000))
(2.0 2.9 2.72 2.721 2.7164 2.71576 2.71782 2.7182744)
Euler's number, 'e', is approximately 2.7182818284590452354, and this is
a highly-inefficient way to extract it, but
the function does tend toward 'e'.)
Jim
More information about the LUAU
mailing list