Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Use bigint Binomial for Choose function #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions utils/math/mathutils.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package zxcvbnmath

import "math"
import (
"math"
"math/big"
)

/*
NChoseK http://blog.plover.com/math/choose.html
I am surprised that I have to define these. . . Maybe i just didn't look hard enough for a lib.
*/

// NChoseK returns the binomial co-efficient taking and returning float64
// It is simply a type adjusting wrapper for big.Binomial()
func NChoseK(n, k float64) float64 {
if k > n {
return 0
} else if k == 0 {
return 1
}
coef := new(big.Int).Binomial(int64(n), int64(k))

var r float64 = 1

for d := float64(1); d <= k; d++ {
r *= n
r /= d
n--
}
f := new(big.Float).SetInt(coef)
r, _ := f.Float64()

return r
}
Expand Down