diff --git a/math/aliquot_test.go b/math/aliquot_test.go index efeda425c..45c45981e 100644 --- a/math/aliquot_test.go +++ b/math/aliquot_test.go @@ -21,6 +21,8 @@ func TestAliquotSum(t *testing.T) { {"n = 10", 10, 8, nil}, {"n = 11", 11, 1, nil}, {"n = 1", 1, 0, nil}, + {"n = 28", 28, 28, nil}, + {"n = 36", 36, 55, nil}, {"n = -1", -1, 0, math.ErrPosArgsOnly}, {"n = 0", 0, 0, math.ErrNonZeroArgsOnly}, } diff --git a/math/aliquotsum.go b/math/aliquotsum.go index cc6bd326a..aff6f4878 100644 --- a/math/aliquotsum.go +++ b/math/aliquotsum.go @@ -5,14 +5,14 @@ // 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) +// time complexity: O(sqrt(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 +// AliquotSum returns the sum of all proper divisors of n. func AliquotSum(n int) (int, error) { switch { case n < 0: @@ -20,10 +20,19 @@ func AliquotSum(n int) (int, error) { case n == 0: return 0, ErrNonZeroArgsOnly default: - var sum int - for i := 1; i <= n/2; i++ { - if n%i == 0 { - sum += i + if n == 1 { + return 0, nil + } + + sum := 1 + for divisor := 2; divisor <= n/divisor; divisor++ { + if n%divisor == 0 { + sum += divisor + + pairedDivisor := n / divisor + if pairedDivisor != divisor { + sum += pairedDivisor + } } } return sum, nil