Added statistical method to ints package.
This commit is contained in:
parent
fc18ea9ce8
commit
39cef02092
47
ints/statistics.go
Normal file
47
ints/statistics.go
Normal file
@ -0,0 +1,47 @@
|
||||
package ints
|
||||
|
||||
// Faculty returns the faculty of i.
|
||||
func Faculty(i int) int {
|
||||
if i < 2 {
|
||||
return 1
|
||||
}
|
||||
return i * Faculty(i-1)
|
||||
}
|
||||
|
||||
// Faculty64 returns the faculty of i.
|
||||
func Faculty64(i int64) int64 {
|
||||
if i < 2 {
|
||||
return 1
|
||||
}
|
||||
return i * Faculty64(i-1)
|
||||
}
|
||||
|
||||
// Permutations returns all possible permutations of the the set integers.
|
||||
func Permutations(set []int) [][]int {
|
||||
var n = len(set)
|
||||
if n == 0 {
|
||||
return nil
|
||||
}
|
||||
var res = make([][]int, 0, Faculty(n))
|
||||
var p = append([]int(nil), set...)
|
||||
|
||||
var c = make([]int, n)
|
||||
res = append(res, append([]int(nil), p...))
|
||||
i := 0
|
||||
for i < n {
|
||||
if c[i] < i {
|
||||
if i%2 == 0 {
|
||||
p[0], p[i] = p[i], p[0]
|
||||
} else {
|
||||
p[c[i]], p[i] = p[i], p[c[i]]
|
||||
}
|
||||
res = append(res, append([]int(nil), p...))
|
||||
c[i] = c[i] + 1
|
||||
i = 0
|
||||
} else {
|
||||
c[i] = 0
|
||||
i++
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
Loading…
Reference in New Issue
Block a user