-
Notifications
You must be signed in to change notification settings - Fork 1
/
day_27_amicable_numbers.go
72 lines (63 loc) · 1.2 KB
/
day_27_amicable_numbers.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import "fmt"
func prime_factors(n int) (pfs []int) {
for n%2 == 0 {
pfs = append(pfs, 2)
n = n / 2
}
for i := 3; i*i <= n; i = i + 2 {
for n%i == 0 {
pfs = append(pfs, i)
n = n / i
}
}
if n > 2 {
pfs = append(pfs, n)
}
return
}
func power(p, i int) int {
result := 1
for j := 0; j < i; j++ {
result *= p
}
return result
}
func sum_of_proper_divisors(n int) int {
pfs := prime_factors(n)
m := make(map[int]int)
for _, prime := range pfs {
_, ok := m[prime]
if ok {
m[prime] += 1
} else {
m[prime] = 1
}
}
sum_of_all_factors := 1
for prime, exponents := range m {
sum_of_all_factors *= (power(prime, exponents+1) - 1) / (prime - 1)
}
return sum_of_all_factors - n
}
func amicable_numbers_under_10000() (amicables []int) {
for i := 3; i < 10000; i++ {
s := sum_of_proper_divisors(i)
if s == i {
continue
}
if sum_of_proper_divisors(s) == i {
amicables = append(amicables, i)
}
}
return
}
func main() {
amicables := amicable_numbers_under_10000()
fmt.Println(amicables)
sum := 0
for i := 0; i < len(amicables); i++ {
sum += amicables[i]
}
fmt.Println(sum)
}