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

JorgeDS's solution

to Armstrong Numbers in the Erlang Track

Published at Mar 19 2021 · 0 comments
Test suite

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


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.


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.


%% 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.



'1_zero_is_an_armstrong_number_test_'() ->
    {"Zero is an Armstrong number",

'2_single_digit_numbers_are_armstrong_numbers_test_'() ->
    {"Single digit numbers are Armstrong numbers",

'3_there_are_no_2_digit_armstrong_numbers_test_'() ->
    {"There are no 2 digit Armstrong numbers",

'4_three_digit_number_that_is_an_armstrong_number_test_'() ->
    {"Three digit number that is an Armstrong "

'5_three_digit_number_that_is_not_an_armstrong_number_test_'() ->
    {"Three digit number that is not an Armstrong "

'6_four_digit_number_that_is_an_armstrong_number_test_'() ->
    {"Four digit number that is an Armstrong "

'7_four_digit_number_that_is_not_an_armstrong_number_test_'() ->
    {"Four digit number that is not an Armstrong "

'8_seven_digit_number_that_is_an_armstrong_number_test_'() ->
    {"Seven digit number that is an Armstrong "

'9_seven_digit_number_that_is_not_an_armstrong_number_test_'() ->
    {"Seven digit number that is not an Armstrong "


is_armstrong_number(_Number) -> Number_str = integer_to_list(_Number),
                               case calc_sum_of_digits(Number_str, length(Number_str), 0) of
                                   _Number -> true;
                                   _       -> false

calc_sum_of_digits([],_, Sum) -> Sum;
calc_sum_of_digits([H|T], Number_length, Sum) -> calc_sum_of_digits(T, Number_length, trunc(math:pow((H - 48),Number_length))  + Sum) .

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?