Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of meatbot

meatbot's solution

to Perfect Numbers in the Erlang Track

Published at Mar 29 2021 · 0 comments
Instructions
Test suite
Solution

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

  • Perfect: aliquot sum = number
    • 6 is a perfect number because (1 + 2 + 3) = 6
    • 28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
  • Abundant: aliquot sum > number
    • 12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16
    • 24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36
  • Deficient: aliquot sum < number
    • 8 is a deficient number because (1 + 2 + 4) = 7
    • Prime numbers are deficient

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.

Running tests

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

Questions?

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.

Source

Taken from Chapter 2 of Functional Thinking by Neal Ford. http://shop.oreilly.com/product/0636920029687.do

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

perfect_numbers_tests.erl

%% 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 ->
  case lists:sum(factors_of_number(Number)) of
    Sum when Sum == Number -> perfect;
    Sum when Sum > Number -> abundant;
    Sum when Sum < Number -> deficient
  end.

factors_of_number(Number) -> factors_of_number(Number, Number - 1, []).

factors_of_number(_Number, 0, Factors) ->
  Factors;
factors_of_number(Number, Factor, Factors) when Number rem Factor == 0 ->
  factors_of_number(Number, Factor - 1, [Factor | Factors]);
factors_of_number(Number, Factor, Factors) ->
  factors_of_number(Number, Factor - 1, Factors).

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?