-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem-21.go
More file actions
58 lines (47 loc) · 896 Bytes
/
problem-21.go
File metadata and controls
58 lines (47 loc) · 896 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import "fmt"
func main() {
list := make(map[int]int)
listResult := make(map[int]bool)
result := 0
fmt.Print("Amicable number : ")
for i := 1; i < 10000; i++ {
for j := 1; j < 10000; j++ {
if i == j {
continue
}
if _, ok := listResult[j]; ok {
continue
}
if getDivisor(list, i) == j && getDivisor(list, j) == i {
result += i + j
fmt.Printf("[%d, %d], ", i, j)
listResult[i] = true
}
}
}
fmt.Println("\nSum of amicable numbers :", result)
}
func getDivisor(list map[int]int, num int) int {
total := 1
temp := 0
if val, ok := list[num]; ok {
return val
}
for i := 2; i < num; i++ {
if num%i == 0 && i*i == num {
total += i
break
}
if num%i == 0 && num/i == temp {
break
}
if num%i == 0 {
secondDivisor := num / i
total += i + secondDivisor
temp = i
}
}
list[num] = total
return total
}