From e6497178e3983e7e34ac904a1db84c6bf4d27800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20Axel=20S=C3=B8gaard?= Date: Tue, 3 Jul 2012 13:23:44 +0200 Subject: [PATCH] Moved binomial to separate number-theory file. --- bracket/bracket.rkt | 25 +++++-------------------- number-theory/number-theory.rkt | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 number-theory/number-theory.rkt diff --git a/bracket/bracket.rkt b/bracket/bracket.rkt index a4cbd9379..509635726 100644 --- a/bracket/bracket.rkt +++ b/bracket/bracket.rkt @@ -55,24 +55,7 @@ -(module number-theory racket/base - (provide binomial) - - ; binomial : natural natural -> natural - ; compute the binomial coeffecient n choose k - (define (binomial n k) - ; - ; http://lavica.fesb.hr/cgi-bin/info2html?(gmp)Binomial%20Coefficients%20Algorithm - ; TODO: Check range of n and k - (cond - [(= k 0) 1] - [(= k 1) n] - [(= k 2) (/ (* n (- n 1)) 2)] - [(> k (/ n 2)) (binomial n (- n k))] - [else (* (+ n (- k) 1) - (for/product ([i (in-range 2 (+ k 1))]) - (/ (+ n (- k) i) - i)))]))) + (module undefined racket/base (provide undefined undefined?) @@ -87,6 +70,8 @@ (define (symbolic-id? id) (or (symbol? id) (and (syntax? id) + ; Syntax symbolical identifiers are not in use. + ; Remove this? (memv (syntax-e id) '(Plus Minus Times Quotient Power =)))))) @@ -790,12 +775,12 @@ (module bracket racket - (require (submod ".." number-theory) + (require "../number-theory/number-theory.rkt" (submod ".." expression) (submod ".." undefined) (submod ".." equation-expression) "graphics.rkt") - (provide ; (all-from-out (submod ".." symbolic-application)) + (provide (rename-out [free-of Free-of] [base Base] [const Const] diff --git a/number-theory/number-theory.rkt b/number-theory/number-theory.rkt new file mode 100644 index 000000000..c40e3b02d --- /dev/null +++ b/number-theory/number-theory.rkt @@ -0,0 +1,29 @@ +#lang racket/base +(provide binomial) + +; binomial : natural natural -> natural +; compute the binomial coeffecient n choose k +(define (binomial n k) + ; + ; http://lavica.fesb.hr/cgi-bin/info2html?(gmp)Binomial%20Coefficients%20Algorithm + ; TODO: Check range of n and k + (cond + [(= k 0) 1] + [(= k 1) n] + [(= k 2) (/ (* n (- n 1)) 2)] + [(> k (/ n 2)) (binomial n (- n k))] + [else (* (+ n (- k) 1) + (for/product ([i (in-range 2 (+ k 1))]) + (/ (+ n (- k) i) + i)))])) + +(module* test #f + (require rackunit) + ; Binomial + (check-equal? (binomial 5 0) 1) + (check-equal? (binomial 5 1) 5) + (check-equal? (binomial 5 2) 10) + (check-equal? (binomial 5 3) 10) + (check-equal? (binomial 5 4) 5) + (check-equal? (binomial 5 5) 1) + (check-equal? (binomial 10 6) 210))