-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem23.go
More file actions
65 lines (61 loc) · 1.28 KB
/
Copy pathproblem23.go
File metadata and controls
65 lines (61 loc) · 1.28 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package projecteuler
// checks whether a given number is abundant number or not
func isAbundantNumber(n int) bool {
sum := 0
for i := 1; i <= n/2; i++ {
if n%i == 0 {
sum += i
}
}
return sum > n
}
// generates all abundant numbers under `X`,
// and stores them in slice
func genAbundantNumUnderX(x int) []int {
cache := make([]int, 0)
for i := 2; i < x; i++ {
if isAbundantNumber(i) {
cache = append(cache, i)
}
}
return cache
}
// determines whether a given number `X`
// can be written as sum of two abundant numbers
// or not
// a cache of all abundant numbers to be passed,
// while invoking this function
func canBeWrittenAsSumOfTwoAbundantNum(x int, cache []int) bool {
check := false
for i := 0; i < len(cache); i++ {
if cache[i] > x {
break
}
for j := i; j < len(cache); j++ {
tmp := cache[i] + cache[j]
if tmp == x {
check = true
break
} else if tmp > x {
break
}
}
if check {
break
}
}
return check
}
// NonAbundantSum - Calculates sum of all positive integers
// which can't be written as sum of two abundant numbers
func NonAbundantSum() int {
const limit = 28124
sum := 0
tmp := genAbundantNumUnderX(limit)
for i := 1; i < limit; i++ {
if !canBeWrittenAsSumOfTwoAbundantNum(i, tmp) {
sum += i
}
}
return sum
}