--- a/Integer.st Fri Jan 16 14:55:49 2009 +0100
+++ b/Integer.st Fri Jan 16 17:22:32 2009 +0100
@@ -2147,6 +2147,7 @@
! !
+
!Integer methodsFor:'misc math'!
acker:n
@@ -2165,6 +2166,55 @@
"Modified: 18.7.1996 / 13:08:16 / cg"
!
+binomialCoefficient:kIn
+ |k acc|
+
+ "computes the binomialCoefficient C(n,k)
+ / n \ with self being n, and 0 <= k <= n.
+ \ k /
+
+ The binCo is defined as:
+ n!!
+ ----------
+ k!! (n-k)!!
+
+ but there is a faster, recursive formula:
+
+ / n \ = / n - 1 \ + / n - 1 \
+ \ k / \ k - 1 / \ k /
+
+ with:
+
+ / n \ = / n \ = 1
+ \ 0 / \ n /
+ "
+
+ kIn > self ifTrue:[^ 0].
+
+ k := kIn.
+ k > (self / 2) ifTrue:[
+ "/ symmetry
+ k := self - k.
+ ].
+
+ acc := 1.
+ 1 to:k do:[:i |
+ acc := acc * (self - k + i) / i.
+ ].
+ ^ acc
+
+ "
+ (7 binomialCoefficient:3)
+ (10 binomialCoefficient:5)
+ (100 binomialCoefficient:5)
+ (1000 binomialCoefficient:5)
+ TestCase assert: (10 binomialCoefficient:5) = (10 factorial / (5 factorial * 5 factorial))
+ TestCase assert: (100 binomialCoefficient:78) = (100 factorial / (78 factorial * (100-78) factorial))
+ TestCase assert: (1000 binomialCoefficient:5) = (1000 factorial / (5 factorial * (1000-5) factorial))
+ TestCase assert: (10000 binomialCoefficient:78) = (10000 factorial / (78 factorial * (10000-78) factorial))
+ "
+!
+
divMod:aNumber
"return an array filled with self // aNumber and
self \\ aNumber.
@@ -3534,7 +3584,7 @@
!Integer class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.202 2009-01-16 11:33:08 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.203 2009-01-16 16:22:32 cg Exp $'
! !
Integer initialize!