-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
perfectnumber.go
38 lines (35 loc) · 1.15 KB
/
perfectnumber.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
// perfectnumber.go
// description: provides the function IsPerfectNumber and related utilities
// details:
// provides the functions
// - IsPerfectNumber which checks if the input is a perfect number,
// - SumOfProperDivisors which returns the sum of proper divisors of the input.
// A number is called perfect, if it is a sum of its proper divisors,
// cf. https://en.wikipedia.org/wiki/Perfect_number,
// https://mathworld.wolfram.com/PerfectNumber.html
// time complexity: O(sqrt(n))
// space complexity: O(1)
// https://oeis.org/A000396
// author(s) [Piotr Idzik](https://github.com/vil02)
// see perfectnumber_test.go
package math
// Returns the sum of proper divisors of inNumber.
func SumOfProperDivisors(inNumber uint) uint {
var res = uint(0)
if inNumber > 1 {
res = uint(1)
}
for curDivisor := uint(2); curDivisor*curDivisor <= inNumber; curDivisor++ {
if inNumber%curDivisor == 0 {
res += curDivisor
if curDivisor*curDivisor != inNumber {
res += inNumber / curDivisor
}
}
}
return res
}
// Checks if inNumber is a perfect number
func IsPerfectNumber(inNumber uint) bool {
return inNumber > 0 && SumOfProperDivisors(inNumber) == inNumber
}