Skip to content

Instantly share code, notes, and snippets.

@montanaflynn
Last active July 28, 2018 15:09
Show Gist options
  • Save montanaflynn/c8c90db7d31bd136e22d378894765360 to your computer and use it in GitHub Desktop.
Save montanaflynn/c8c90db7d31bd136e22d378894765360 to your computer and use it in GitHub Desktop.
Maximum Combination Calculator
package main
import (
"fmt"
"math/big"
)
func factorial(n *big.Int) (result *big.Int) {
result = big.NewInt(1)
var one big.Int
one.SetInt64(1)
for n.Cmp(&big.Int{}) == 1 {
result.Mul(result, n)
n.Sub(n, &one)
}
return
}
func combinations(r, n int64) *big.Int {
rBig := big.NewInt(r)
nBig := big.NewInt(n)
nSubR := new(big.Int)
div := new(big.Int)
mul := new(big.Int)
nSubR.Sub(rBig, nBig)
rFactorial := factorial(rBig)
nFactorial := factorial(nBig)
nSubRFactorial := factorial(nSubR)
mul.Mul(nFactorial, nSubRFactorial)
div.Div(rFactorial, mul)
return div
}
func main() {
fmt.Println(combinations(52, 13))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment