-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproblem21.go
28 lines (25 loc) · 870 Bytes
/
problem21.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
// 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
// Problem21 is solution for finding the sum of all the amicable numbers under 10000.
func Problem21() int {
sum := 0
for n := 2; n < 10e3; n++ { // We are starting by 2 'cause 1 has no divisors at all.
if isAmicable(n) {
sum += n
}
}
return sum
}
// isAmicable decide if a is a amicable number.
// The number is amicable if next statement is true:
// Let d(n) be defined as the sum of proper divisors of
// n (numbers less than n which divide evenly into n).
// If d(a) = b and d(b) = a, where a != b, then a and b
// are an amicable pair and each of a and b are called
// amicable numbers.
func isAmicable(a int) bool {
b := PropDivSum(a)
return a != b && a == PropDivSum(b)
}