Created
July 7, 2015 13:58
-
-
Save jittuu/dbd993774793d7f6d14f to your computer and use it in GitHub Desktop.
Binomial coefficient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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