Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for natural numbers.
The Greek mathematician Nicomachus devised a classification scheme for natural numbers, identifying each as belonging uniquely to the categories of perfect, abundant, or deficient based on their aliquot sum. The aliquot sum is defined as the sum of the factors of a number not including the number itself. For example, the aliquot sum of 15 is (1 + 3 + 5) = 9
Implement a way to determine whether a given number is perfect. Depending on your language track, you may also need to implement a way to determine whether a given number is abundant or deficient.
In order to run the tests, issue the following command from the exercise directory:
For running the tests provided, rebar3
is used as it is the official build and
dependency management tool for erlang now. Please refer to the tracks installation
instructions on how to do that.
In order to run the tests, you can issue the following command from the exercise directory.
$ rebar3 eunit
For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.
Taken from Chapter 2 of Functional Thinking by Neal Ford. http://shop.oreilly.com/product/0636920029687.do
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
%% Generated with 'testgen v0.2.0'
%% Revision 1 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/fcbc25191f449f3791db0f2e8acf2d670ea138a0/exercises/perfect-numbers/canonical-data.json
%% This file is automatically generated from the exercises canonical data.
-module(perfect_numbers_tests).
-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").
'1_smallest_perfect_number_is_classified_correctly_test_'() ->
{"Smallest perfect number is classified "
"correctly",
?_assertEqual(perfect, perfect_numbers:classify(6))}.
'2_medium_perfect_number_is_classified_correctly_test_'() ->
{"Medium perfect number is classified "
"correctly",
?_assertEqual(perfect, perfect_numbers:classify(28))}.
'3_large_perfect_number_is_classified_correctly_test_'() ->
{"Large perfect number is classified correctly",
?_assertEqual(perfect,
perfect_numbers:classify(33550336))}.
'4_smallest_abundant_number_is_classified_correctly_test_'() ->
{"Smallest abundant number is classified "
"correctly",
?_assertEqual(abundant, perfect_numbers:classify(12))}.
'5_medium_abundant_number_is_classified_correctly_test_'() ->
{"Medium abundant number is classified "
"correctly",
?_assertEqual(abundant, perfect_numbers:classify(30))}.
'6_large_abundant_number_is_classified_correctly_test_'() ->
{"Large abundant number is classified "
"correctly",
?_assertEqual(abundant,
perfect_numbers:classify(33550335))}.
'7_smallest_prime_deficient_number_is_classified_correctly_test_'() ->
{"Smallest prime deficient number is classified "
"correctly",
?_assertEqual(deficient, perfect_numbers:classify(2))}.
'8_smallest_non_prime_deficient_number_is_classified_correctly_test_'() ->
{"Smallest non-prime deficient number "
"is classified correctly",
?_assertEqual(deficient, perfect_numbers:classify(4))}.
'9_medium_deficient_number_is_classified_correctly_test_'() ->
{"Medium deficient number is classified "
"correctly",
?_assertEqual(deficient, perfect_numbers:classify(32))}.
'10_large_deficient_number_is_classified_correctly_test_'() ->
{"Large deficient number is classified "
"correctly",
?_assertEqual(deficient,
perfect_numbers:classify(33550337))}.
'11_edge_case_no_factors_other_than_itself_is_classified_correctly_test_'() ->
{"Edge case (no factors other than itself) "
"is classified correctly",
?_assertEqual(deficient, perfect_numbers:classify(1))}.
'12_zero_is_rejected_as_it_is_not_a_positive_integer_test_'() ->
{"Zero is rejected (as it is not a positive "
"integer)",
?_assertError(_, perfect_numbers:classify(0))}.
'13_negative_integer_is_rejected_as_it_is_not_a_positive_integer_test_'() ->
{"Negative integer is rejected (as it "
"is not a positive integer)",
?_assertError(_, perfect_numbers:classify(-1))}.
-module(perfect_numbers).
-export([classify/1]).
classify(Number) when Number > 0 ->
classify(Number, lists:sum(get_factors(Number, 1, []))).
classify(Number, Sum) when Number == Sum ->
perfect;
classify(Number, Sum) when Number > Sum ->
deficient;
classify(_Number, _Sum) ->
abundant.
get_factors(Number, Count, Factors) when Count == Number ->
Factors;
get_factors(Number, Count, Factors) when (Number rem Count) == 0 ->
get_factors(Number, Count + 1, [Count | Factors]);
get_factors(Number, Count, Factors) ->
get_factors(Number, Count + 1, Factors).
A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments