Created
December 21, 2013 07:56
-
-
Save zfogg/8066702 to your computer and use it in GitHub Desktop.
pe23 in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
const n1, n2 int = 1, 28123 | |
func main() { | |
// All abundant 'n's from n1 to n2. | |
abundant := make([]int, 0) | |
for n := n1; n <= n2; n++ { | |
if n < sumDivisors(n) { | |
abundant = append(abundant, n) | |
} | |
} | |
// A map where keys of abundant sums are set to true. | |
sp := sumsPairs(abundant) | |
// Sum of the 'i's that aren't true in the map. | |
sum := 0 | |
for i := n1; i < n2; i++ { | |
if sp[i] == false { | |
sum += i | |
} | |
} | |
println(sum) | |
} | |
func sumsPairs(xs []int) map[int]bool { | |
r := make(map[int]bool) | |
for i := len(xs)-1; i > 0; i-- { | |
for j := i-1; j >= 0; j-- { | |
r[xs[i] + xs[j]] = true | |
} | |
} | |
return r | |
} | |
func sumDivisors(n int) int { | |
s := 1 | |
for i := n/2; i > 1; i-- { | |
if n % i == 0 { | |
s += i | |
} | |
} | |
return s | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment