# [racket] Math - factorial.rkt, binomial.rkt and memoization

 From: Pierpaolo Bernardi (olopierpa at gmail.com) Date: Tue Apr 9 09:30:00 EDT 2013 Previous message: [racket] Math - factorial.rkt, binomial.rkt and memoization Next message: [racket] Math - factorial.rkt, binomial.rkt and memoization Messages sorted by: [date] [thread] [subject] [author]

```On Tue, Apr 9, 2013 at 2:51 PM, Laurent <laurent.orseau at gmail.com> wrote:

> On Tue, Apr 9, 2013 at 2:42 PM, Matthew Flatt <mflatt at cs.utah.edu> wrote:
>
>>  (for/product ([p (in-range (+ m 1) (+ n 1))]) p)
>
>
>
> But I don't fully understand why a simple (for/product ([p (in-range 1
> (add1 n))]) p) is as fast as the iota variants.
>

I don't understand either.

FWIW here's the kind of multiplications performed by the doctored version
attached below:

> (fact 1000000)
#hash(((fíx fíx fíx) . 48577) ((big big) . 209717) ((fíx big) . 46338)
((fíx fíx) . 646789))

while the for version should be almost all big x big.

====

(define x*-counter 'not-initialized)

(define (x* . args)
(hash-update! x*-counter (map (λ (x)
(if (fixnum? x)
'fíx
'big))
args)
0)
(apply * args))

;; from included; to excluded.
(define (mult from to)
(case (- to from)
((0) 1)
((1) from)
((2) (x* from (+ from 1)))
((3) (x* from (+ from 1) (+ from 2)))
;; ...
(else
(let ((middle (quotient (+ from to) 2)))
(x* (mult from middle) (mult middle to))))))

(define (fact n)
(set! x*-counter (make-hash))
(define f (if (zero? n)
1