ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# ikarachristos's solution

## to Armstrong Numbers in the Erlang Track

Published at Feb 02 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.

## 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]).

is_armstrong_number(Number) -> Number =:= armstrong_sum(Number).

armstrong_sum(Number) ->
Digits = digits(Number),
Length = length(Digits),
lists:foldl(fun(X, Sum) -> Sum + trunc(math:pow(X, Length)) end, 0, Digits).

digits(Number) ->
[X - \$0 || X <- integer_to_list(Number)].``````