Skip to content
This repository has been archived by the owner on Dec 22, 2018. It is now read-only.

Commit

Permalink
stat/combin: avoid overflow in Binomial
Browse files Browse the repository at this point in the history
Fixes #174
  • Loading branch information
vladimir-ch committed Apr 19, 2017
1 parent 58876a7 commit ea880ec
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions combin/combin.go
Expand Up @@ -35,15 +35,11 @@ func Binomial(n, k int) int {
if k > n/2 {
k = n - k
}
kfact := 1
for i := 2; i <= k; i++ {
kfact *= i
b := 1
for i := 1; i <= k; i++ {
b = (n - k + i) * b / i
}
num := 1
for i := n; i > n-k; i-- {
num *= i
}
return num / kfact
return b
}

// GeneralizedBinomial returns the generalized binomial coefficient of (n, k),
Expand Down

0 comments on commit ea880ec

Please sign in to comment.