Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of m-c-g

m-c-g's solution

to Armstrong Numbers in the Erlang Track

Published at Apr 30 2021 · 0 comments
Instructions
Test suite
Solution

An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.

For example:

  • 9 is an Armstrong number, because 9 = 9^1 = 9
  • 10 is not an Armstrong number, because 10 != 1^2 + 0^2 = 1
  • 153 is an Armstrong number, because: 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
  • 154 is not an Armstrong number, because: 154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190

Write some code to determine whether a number is an Armstrong number.

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

Wikipedia https://en.wikipedia.org/wiki/Narcissistic_number

Submitting Incomplete Solutions

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

armstrong_numbers_tests.erl

%% Generated with 'testgen v0.2.0'
%% Revision 1 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/42dd0cea20498fd544b152c4e2c0a419bb7e266a/exercises/armstrong-numbers/canonical-data.json
%% This file is automatically generated from the exercises canonical data.

-module(armstrong_numbers_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").




'1_zero_is_an_armstrong_number_test_'() ->
    {"Zero is an Armstrong number",
     ?_assert(armstrong_numbers:is_armstrong_number(0))}.

'2_single_digit_numbers_are_armstrong_numbers_test_'() ->
    {"Single digit numbers are Armstrong numbers",
     ?_assert(armstrong_numbers:is_armstrong_number(5))}.

'3_there_are_no_2_digit_armstrong_numbers_test_'() ->
    {"There are no 2 digit Armstrong numbers",
     ?_assertNot(armstrong_numbers:is_armstrong_number(10))}.

'4_three_digit_number_that_is_an_armstrong_number_test_'() ->
    {"Three digit number that is an Armstrong "
     "number",
     ?_assert(armstrong_numbers:is_armstrong_number(153))}.

'5_three_digit_number_that_is_not_an_armstrong_number_test_'() ->
    {"Three digit number that is not an Armstrong "
     "number",
     ?_assertNot(armstrong_numbers:is_armstrong_number(100))}.

'6_four_digit_number_that_is_an_armstrong_number_test_'() ->
    {"Four digit number that is an Armstrong "
     "number",
     ?_assert(armstrong_numbers:is_armstrong_number(9474))}.

'7_four_digit_number_that_is_not_an_armstrong_number_test_'() ->
    {"Four digit number that is not an Armstrong "
     "number",
     ?_assertNot(armstrong_numbers:is_armstrong_number(9475))}.

'8_seven_digit_number_that_is_an_armstrong_number_test_'() ->
    {"Seven digit number that is an Armstrong "
     "number",
     ?_assert(armstrong_numbers:is_armstrong_number(9926315))}.

'9_seven_digit_number_that_is_not_an_armstrong_number_test_'() ->
    {"Seven digit number that is not an Armstrong "
     "number",
     ?_assertNot(armstrong_numbers:is_armstrong_number(9926314))}.
-module(armstrong_numbers).
-export([is_armstrong_number/1]).

power(_N, E) when E < 0 -> error(badarg);
power(N, E) -> power(N, E, 1).
power(_N, E, ACC) when E =:= 0 -> ACC;
power(N, E, ACC) -> N * power(N, E-1, ACC).

is_armstrong_number(N) -> 
    Digit_list = 
        [ list_to_integer([C]) 
        || C <- integer_to_list(N) ],    
    
    Len = length(Digit_list),

    lists:sum( [ power(D, Len) 
        ||  D <- Digit_list ]) =:= N.

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?