Skip to content

Instantly share code, notes, and snippets.

@jittuu
Created July 7, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jittuu/dbd993774793d7f6d14f to your computer and use it in GitHub Desktop.
Save jittuu/dbd993774793d7f6d14f to your computer and use it in GitHub Desktop.
Binomial coefficient
package main
import "fmt"
func main() {
//nCr
n := 7
r := 2
c := BinCoeff(n, r)
fmt.Println(c)
}
// BinCoeff (in combinatorics) gives the number of ways, disregarding order,
// that k objects can be chosen from among n objects; more formally, the number of
// k-element subsets (or k-combinations) of an n-element set
// ref: http://www.mathwords.com/c/combination_formula.htm
func BinCoeff(n int, k int) int64 {
if k == 0 || k == n {
return 1
} else {
return BinCoeff(n-1, k-1) + BinCoeff(n-1, k)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment