Update
This commit is contained in:
42
go/perfect-numbers/README.md
Normal file
42
go/perfect-numbers/README.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# Perfect Numbers
|
||||
|
||||
The Greek mathematician Nicomachus devised a classification scheme for natural numbers.
|
||||
|
||||
The Greek mathematican Nicomachus devised a classification scheme for
|
||||
natural numbers, identifying each as belonging uniquely to the
|
||||
categories of _abundant_, _perfect_, or _deficient_. A perfect number
|
||||
equals the sum of its positive divisors — the pairs of numbers whose
|
||||
product yields the target number, excluding the number itself.
|
||||
|
||||
- Perfect: Sum of factors = number
|
||||
- Abundant: Sum of factors > number
|
||||
- Deficient: Sum of factors < number
|
||||
|
||||
The Aliquot sum is defined as the sum of the factors of a number not
|
||||
including the number itself.
|
||||
|
||||
## Examples
|
||||
|
||||
- 6 is a perfect number because its divisors are 1, 2, 3 and 6 = 1 + 2 +
|
||||
3.
|
||||
- 28 is perfect number because 28 = 1 + 2 + 4 + 7 + 14.
|
||||
- Prime numbers 7, 13, etc are Deficient by the Nicomachus
|
||||
classification.
|
||||
|
||||
To run the tests simply run the command `go test` in the exercise directory.
|
||||
|
||||
If the test suite contains benchmarks, you can run these with the `-bench`
|
||||
flag:
|
||||
|
||||
go test -bench .
|
||||
|
||||
For more detailed info about the Go track see the [help
|
||||
page](http://exercism.io/languages/go).
|
||||
|
||||
## Source
|
||||
|
||||
Taken from Chapter 2 of Functional Thinking by Neal Ford. [http://shop.oreilly.com/product/0636920029687.do](http://shop.oreilly.com/product/0636920029687.do)
|
||||
|
||||
## Submitting Incomplete Problems
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
|
40
go/perfect-numbers/perfect_numbers.go
Normal file
40
go/perfect-numbers/perfect_numbers.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package perfect
|
||||
|
||||
import "errors"
|
||||
|
||||
type Classification int
|
||||
|
||||
const (
|
||||
ClassificationPerfect = iota
|
||||
ClassificationDeficient
|
||||
ClassificationAbundant
|
||||
)
|
||||
|
||||
const testVersion = 1
|
||||
|
||||
var ErrOnlyPositive error
|
||||
|
||||
func Classify(inp uint64) (Classification, error) {
|
||||
ErrOnlyPositive = errors.New("Only Positive Allowed")
|
||||
if inp <= 0 {
|
||||
return 0, ErrOnlyPositive
|
||||
}
|
||||
factors := GetFactorSum(inp)
|
||||
if factors > inp {
|
||||
return ClassificationAbundant, nil
|
||||
}
|
||||
if factors < inp {
|
||||
return ClassificationDeficient, nil
|
||||
}
|
||||
return ClassificationPerfect, nil
|
||||
}
|
||||
|
||||
func GetFactorSum(inp uint64) uint64 {
|
||||
var ret uint64
|
||||
for i := uint64(1); i < inp; i++ {
|
||||
if inp%i == 0 {
|
||||
ret += i
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
41
go/perfect-numbers/perfect_numbers_test.go
Normal file
41
go/perfect-numbers/perfect_numbers_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package perfect
|
||||
|
||||
import "testing"
|
||||
|
||||
var _ error = ErrOnlyPositive
|
||||
|
||||
func TestGivesPositiveRequiredError(t *testing.T) {
|
||||
if _, err := Classify(0); err != ErrOnlyPositive {
|
||||
t.Errorf("Expected error %q but got %q", ErrOnlyPositive, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifiesCorrectly(t *testing.T) {
|
||||
cases := []struct {
|
||||
input uint64
|
||||
expected Classification
|
||||
}{
|
||||
{1, ClassificationDeficient},
|
||||
{13, ClassificationDeficient},
|
||||
{12, ClassificationAbundant},
|
||||
{6, ClassificationPerfect},
|
||||
{28, ClassificationPerfect},
|
||||
{496, ClassificationPerfect},
|
||||
{8128, ClassificationPerfect},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if cat, err := Classify(c.input); err != nil {
|
||||
t.Errorf("%d: Expected no error but got %s", c.input, err)
|
||||
} else if cat != c.expected {
|
||||
t.Errorf("%d: Expected %q, got %q", c.input, c.expected, cat)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const targetTestVersion = 1
|
||||
|
||||
func TestTestVersion(t *testing.T) {
|
||||
if testVersion != targetTestVersion {
|
||||
t.Errorf("Found testVersion = %v, want %v.", testVersion, targetTestVersion)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user