/
23.go
92 lines (78 loc) · 1.4 KB
/
23.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"github.com/fenglyu/projecteuler/golang/common"
)
const (
Deficient = -1
Perfect = 0
Abundant = 1
)
func NumType(n int) int {
//nums := divisor(n)
nums := common.Divisor(n)
//fmt.Println("c: ", nums)
s := 0
for _, v := range nums {
s += v
}
flag := Perfect
if s < n {
flag = Deficient
} else if s > n {
flag = Abundant
}
// fmt.Println(s, n, flag)
return flag
}
func main() {
// fmt.Println(Perfect, Deficient, Abundant)
//fmt.Printf("28 is %v\n", NumType(28))
//fmt.Printf("12 is %v\n", NumType(12))
limit := 28123
sts := make([]int, limit+1)
for i := 0; i < limit+1; i++ {
sts[i] = 0
}
// collect result
// result, rdx := make([]int, limit), 0
result := make([]int, limit+1)
for i := 0; i < limit+1; i++ {
result[i] = 0
}
abs, c := make([]int, 10000), 0
for i := 1; i <= limit; i++ {
if NumType(i) > 0 {
//fmt.Println(i)
abs[c] = i
sts[i]++
c++
}
//fmt.Println(result)
}
for i := 1; i <= limit; i++ {
var j int
for j = 0; j <= c; j++ {
if abs[j] < i {
div := i - abs[j]
if sts[div] > 0 {
result[i]++
break
}
} else {
break
}
}
// if there is no two abundants found in abs, this number cannot be written as the sum of two abundants numbers.
if j > c {
result[i]++
}
}
sum := 0
for i := 1; i <= limit; i++ {
if result[i] == 0 {
sum += i
}
}
fmt.Println(sum)
}