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

Published at Mar 15 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.

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

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.
-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) ->
Digits = get_digits(Number, []),
NumDigits = length(Digits),
lists:foldl(fun(X, Sum) -> round(math:pow(X, NumDigits)) + Sum end, 0, Digits) =:= Number.
get_digits(Number, Digits) when Number < 10 ->
[Number | Digits];
get_digits(Number, Digits) ->
get_digits(Number div 10, [Number rem 10 | Digits]).
```

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?

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

## Community comments