-
Notifications
You must be signed in to change notification settings - Fork 3
/
problem23.go
45 lines (40 loc) · 1.13 KB
/
problem23.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
// Copyright 2015 Peter Mrekaj. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.txt file.
package euler
// Problem23 is solution for finding the the sum of all the positive
// integers which cannot be written as the sum of two abundant numbers.
func Problem23() int {
const limit = 28123
var abd []int
// Were starting from 12 'cause it is the smallest abundant number.
// The upper limit is 28123 'cause it can be shown that all greater
// integers can be written as the sum of two abundant numbers.
for i := 12; i <= limit; i++ {
if isAbundant(i) {
abd = append(abd, i)
}
}
sums := make(map[int]bool)
for i, a := range abd {
for j := i; j < len(abd); j++ {
s := a + abd[j]
if s <= limit {
sums[s] = true
}
}
}
sum := 0
for i := 1; i <= limit; i++ {
if !sums[i] {
sum += i
}
}
return sum
}
// isAbundant returns true if n is a abundant number.
// For n = 0 returns false, for n < 0 the result is not specified.
// A number is called abundant if sum of its divisors exceeds n.
func isAbundant(n int) bool {
return PropDivSum(n) > n
}