-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombination.go
More file actions
30 lines (23 loc) · 769 Bytes
/
combination.go
File metadata and controls
30 lines (23 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package gocap
// CombinationCalculator implementation
type CombinationCalculator struct{}
// Calculate the combination for the given inputs
func (c *CombinationCalculator) Calculate(operation *Operation) int {
if operation.N == 0 && operation.R == 0 {
return 0
}
if operation.Repetition {
return c.withRepetition(operation.N, operation.R)
}
return c.withoutRepetition(operation.N, operation.R)
}
func (c *CombinationCalculator) withoutRepetition(n int, r int) int {
return fact(n) / (fact(r) * fact(n-r))
}
func (c *CombinationCalculator) withRepetition(n int, r int) int {
return fact(r+n-1) / (fact(r) * fact(n-1))
}
// NewCombinationCalculator constructor
func NewCombinationCalculator() *CombinationCalculator {
return &CombinationCalculator{}
}