-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation.go
More file actions
35 lines (27 loc) · 797 Bytes
/
permutation.go
File metadata and controls
35 lines (27 loc) · 797 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
31
32
33
34
35
package gocap
import (
"math"
)
// PermutationCalculator implementation
type PermutationCalculator struct{}
// Calculate the permutation for the given inputs
func (c *PermutationCalculator) 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 *PermutationCalculator) withoutRepetition(n int, r int) int {
return fact(n) / fact(n-r)
}
func (c *PermutationCalculator) withRepetition(n int, r int) int {
result := math.Pow(float64(n), float64(r))
return int(result)
}
// NewPermutationCalculator constructor
func NewPermutationCalculator() *PermutationCalculator {
return &PermutationCalculator{}
}