-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdivisors.go
More file actions
50 lines (46 loc) · 1.16 KB
/
Copy pathdivisors.go
File metadata and controls
50 lines (46 loc) · 1.16 KB
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
44
45
46
47
48
49
50
package main
import "fmt"
func factorize(n int) map[int]int {
factorization := make(map[int]int, 1)
for ; n % 2 == 0; n /= 2 { factorization[2]++ }
for i := 3; i * i <= n; i += 2 {
for ; n % i == 0; n /= i { factorization[i]++ }
}
if n > 2 { factorization[n] = 1 }
return factorization
}
func node(factorization, indices map[int]int,
processed map[int]bool,
powers []int,
n int) {
for factor, maxPower := range factorization {
if powers[indices[factor]] < maxPower {
nextN := n * factor
fmt.Printf("\t%d--%d\n", n, nextN)
if (! processed[nextN]) {
nextNPowers := make([]int, len(powers))
copy(nextNPowers, powers)
nextNPowers[indices[factor]]++
node(factorization, indices, processed, nextNPowers, nextN)
}
}
}
processed[n] = true
}
func main() {
var n int
fmt.Scanf("%d", &n)
fmt.Printf("graph {\n")
fmt.Printf("\t1\n")
factorization := factorize(n)
indices := make(map[int]int, len(factorization))
i := 0
for k, _ := range factorization {
indices[k] = i
i++
}
processed := make(map[int]bool)
powers := make([]int, len(factorization))
node(factorization, indices, processed, powers, 1)
fmt.Printf("}\n")
}