-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathaliquotsum.go
More file actions
31 lines (29 loc) · 703 Bytes
/
aliquotsum.go
File metadata and controls
31 lines (29 loc) · 703 Bytes
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
// aliquotsum.go
// description: Returns s(n)
// details:
// the aliquot sum s(n) of a positive integer n
// is the sum of all proper divisors of n,
// that is, all divisors of n other than n itself
// wikipedia: https://en.wikipedia.org/wiki/Aliquot_sum
// time complexity: O(n)
// space complexity: O(1)
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see aliquotsum_test.go
package math
// This function returns s(n) for given number
func AliquotSum(n int) (int, error) {
switch {
case n < 0:
return 0, ErrPosArgsOnly
case n == 0:
return 0, ErrNonZeroArgsOnly
default:
var sum int
for i := 1; i <= n/2; i++ {
if n%i == 0 {
sum += i
}
}
return sum, nil
}
}