geom/ints/statistics.go

48 lines
824 B
Go
Raw Normal View History

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
}