<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Apr 8, 2013 at 9:34 PM, Bradley Lucier <span dir="ltr"><<a href="mailto:lucier@math.purdue.edu" target="_blank">lucier@math.purdue.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div id=":vj">(define (iota m n)<br>
(let loop ((result '())<br>
(n (- n 1)))<br>
(if (<= m n)<br>
(loop (cons n result)<br>
(- n 1))<br>
result)))<br>
<br>
(define (partial-factorial m n)<br>
;; computes the product (m+1) * ... * (n-1) * n<br>
(if (< (- n m) 10)<br>
(apply * (iota (+ m 1) (+ n 1)))<br>
(* (partial-factorial m (quotient (+ m n) 2))<br>
(partial-factorial (quotient (+ m n) 2) n))))<br>
<br>
(define (factorial n)<br>
(partial-factorial 0 n))<br>
<br>
In Gambit, even in the interpreter, this is pretty fast:<br>
<br>
(define a (time (factorial 1000000)))<br>
(time (factorial 1000000))</div></blockquote></div><br><br></div><div class="gmail_extra">Indeed, it's impressively fast!<br>% racket fast-factorial.rkt <br>cpu time: 5760 real time: 5771 gc time: 28<br><br></div><div class="gmail_extra">
Probably this should replace the default factorial version then.<br><br>Laurent<br></div></div>