-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemplate_algorithm.go
More file actions
43 lines (39 loc) · 881 Bytes
/
Copy pathtemplate_algorithm.go
File metadata and controls
43 lines (39 loc) · 881 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
36
37
38
39
40
41
42
43
package main
// Integer power: compute a**b using binary powering algorithm
// See Donald Knuth, The Art of Computer Programming, Volume 2, Section 4.6.3
// Source: https://groups.google.com/d/msg/golang-nuts/PnLnr4bc9Wo/z9ZGv2DYxXoJ
func pow(a, b int) int {
p := 1
for b > 0 {
if b&1 != 0 {
p *= a
}
b >>= 1
a *= a
}
return p
}
func gcd(a, b int) int {
for b != 0 {
a, b = b, a%b
}
return a
}
func lcm(a, b int) int {
return a / gcd(a, b) * b
}
func allPermutations(values []string) (result [][]string) {
if len(values) == 1 {
result = append(result, values)
return
}
for i, current := range values {
others := make([]string, 0, len(values)-1)
others = append(others, values[:i]...)
others = append(others, values[i+1:]...)
for _, route := range allPermutations(others) {
result = append(result, append(route, current))
}
}
return
}