Sha256: 970ba22b0f7473caa969953cc6d3c513b1d3d97c0e313699fcc30492a0265865

Contents?: true

Size: 883 Bytes

Versions: 252

Compression:

Stored size: 883 Bytes

Contents

package perfect

import "errors"

const testVersion = 1

// 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 uint64) (Classification, error) {
	if n == 0 {
		return "", ErrOnlyPositive
	}
	var sum uint64
	for i := uint64(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

252 entries across 252 versions & 1 rubygems

Version Path
trackler-2.2.1.56 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.55 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.54 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.53 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.52 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.51 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.50 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.49 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.48 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.47 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.46 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.45 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.44 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.43 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.42 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.41 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.40 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.39 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.38 tracks/go/exercises/perfect-numbers/example.go
trackler-2.2.1.37 tracks/go/exercises/perfect-numbers/example.go