Skip to content

Instantly share code, notes, and snippets.

@soegaard
Created April 14, 2013 20:51
Show Gist options
  • Save soegaard/5384160 to your computer and use it in GitHub Desktop.
Save soegaard/5384160 to your computer and use it in GitHub Desktop.
Efficient factorial
#lang racket
(define (fact n)
(mult 1 n 1))
(define (mult m n k)
; m*(m+k)*(m+2k)*...
; last factor <= n
(if (< (- n m) k)
m
(* (mult m n (* 2 k))
(mult (+ m k) n (* 2 k)))))
(time (begin (fact 1000000) (void)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment