// Package perfect is for checking if a number is perfect, abundant or deficient.
package perfect
import (
"errors"
)
// Classification is a number type representation.
type Classification byte
// Classification types.
const (
ClassificationDeficient Classification = iota
ClassificationPerfect
ClassificationAbundant
)
// ErrOnlyPositive is reported if input is < 1.
var ErrOnlyPositive error = errors.New("input must be positive")
// Classify returs the input number Classification.
func Classify(input int64) (Classification, error) {
if input < 1 {
return 0, ErrOnlyPositive
}
diff := abliquot(input) - input
if diff > 0 {
return ClassificationAbundant, nil
}
if diff < 0 {
return ClassificationDeficient, nil
}
return ClassificationPerfect, nil
}
func abliquot(in int64) int64 {
var sum int64
for i := 1; int64(i*i) < in; i++ {
if in%int64(i) == 0 {
sum += int64(i)
if int64(i) != in/int64(i) && i != 1 {
sum += in / int64(i)
}
}
}
return sum
}