Sha256: 7a3c39a3e2899946c4fd89d2c43f4bd46bb8fa8c3cec7c901d074c6eb5c006d7

Contents?: true

Size: 857 Bytes

Versions: 114

Compression:

Stored size: 857 Bytes

Contents

package perfect

import "errors"

// Classification is the category devised by Greek Mathematician Nicomachus
type Classification string

// Defined categories
const (
	ClassificationDeficient Classification = "deficient"
	ClassificationPerfect   Classification = "perfect"
	ClassificationAbundant  Classification = "abundant"
)

// Errors
var (
	ErrOnlyPositive = errors.New("Positive number is required")
)

// Classify finds the category of given natural number
func Classify(n int64) (Classification, error) {
	if n <= 0 {
		return "", ErrOnlyPositive
	}
	var sum int64
	for i := int64(1); i < n; i++ {
		if n%i == 0 {
			if sum = sum + i; sum > n {
				return ClassificationAbundant, nil
			}
		}
	}
	if sum == n {
		return ClassificationPerfect, nil
	} else if sum < n {
		return ClassificationDeficient, nil
	}
	return ClassificationAbundant, nil
}

Version data entries

114 entries across 114 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.179 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.178 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.177 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.176 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.175 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.174 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.173 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.172 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.171 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.170 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.169 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.167 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.166 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.165 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.164 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.163 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.162 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.161 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.160 tracks/go/exercises/perfect-numbers/example.go