danbaron
31-07-2011, 11:06
On my machine, Racket calculates 100,000 factorial (100000!) in approximately 2 minutes.
It has 456,574 digits.
' code --------------------------------------------------------------------------------------------------
#lang racket
(define fact-value 0)
(define (num-digits n)
(add1 (order-of-magnitude n)))
(define (time-function f n)
(let ((t1 (current-milliseconds)))
(set! fact-value (f n))
(let ((t2 (current-milliseconds)))
(let ((tt (/ (- t2 t1) 1000.0)))
tt))))
(define (fact n)
(define prod 1)
(define (loop m)
(cond
((> m n) prod)
(else
(set! prod (* prod m))
(loop (+ m 1)))))
(loop 1))
' REPL interactions -------------------------------------------------------------------------------------
Welcome to DrRacket, version 5.1 [3m].
Language: racket.
> (fact 1)
1
> (fact 2)
2
> (fact 3)
6
> (fact 4)
24
> (fact 5)
120
> (time-function fact 100000)
120.089
> (num-digits fact-value)
456574
>
It has 456,574 digits.
' code --------------------------------------------------------------------------------------------------
#lang racket
(define fact-value 0)
(define (num-digits n)
(add1 (order-of-magnitude n)))
(define (time-function f n)
(let ((t1 (current-milliseconds)))
(set! fact-value (f n))
(let ((t2 (current-milliseconds)))
(let ((tt (/ (- t2 t1) 1000.0)))
tt))))
(define (fact n)
(define prod 1)
(define (loop m)
(cond
((> m n) prod)
(else
(set! prod (* prod m))
(loop (+ m 1)))))
(loop 1))
' REPL interactions -------------------------------------------------------------------------------------
Welcome to DrRacket, version 5.1 [3m].
Language: racket.
> (fact 1)
1
> (fact 2)
2
> (fact 3)
6
> (fact 4)
24
> (fact 5)
120
> (time-function fact 100000)
120.089
> (num-digits fact-value)
456574
>