Published at Jan 05 2019
·
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.

Run the tests with:

```
bats armstrong_numbers_test.sh
```

After the first test(s) pass, continue by commenting out or removing the `skip`

annotations prepending other tests.

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

`Bash`

is a language to write scripts that works closely with various system utilities,
like `sed`

, `awk`

, `date`

and even other programming languages, like `Python`

.
This track does not restrict the usage of these utilities, and as long as your solution is portable
between systems and does not require installing third party applications, feel free to use them to solve the exercise.

For an extra challenge, if you would like to have a better understanding of the language,
try to re-implement the solution in pure `Bash`

, without using any external tools.

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

```
#!/usr/bin/env bats
@test 'Single digits are Armstrong numbers' {
# skip
run bash armstrong_numbers.sh 5
[ "$status" -eq 0 ]
[ "$output" = "true" ]
}
@test 'There are no two digit Armstrong numbers' {
skip
run bash armstrong_numbers.sh 10
[ "$status" -eq 1 ]
[ "$output" = "false" ]
}
@test 'A three digit number that is an Armstrong number' {
skip
run bash armstrong_numbers.sh 153
[ "$status" -eq 0 ]
[ "$output" = "true" ]
}
@test 'A three digit number that is not an Armstrong number' {
skip
run bash armstrong_numbers.sh 100
[ "$status" -eq 1 ]
[ "$output" = "false" ]
}
@test 'A four digit number that is an Armstrong number' {
skip
run bash armstrong_numbers.sh 9474
[ "$status" -eq 0 ]
[ "$output" = "true" ]
}
@test 'A four digit number that is not an Armstrong number' {
skip
run bash armstrong_numbers.sh 9475
[ "$status" -eq 1 ]
[ "$output" = "false" ]
}
@test 'A seven digit number that is an Armstrong number' {
skip
run bash armstrong_numbers.sh 9926315
[ "$status" -eq 0 ]
[ "$output" = "true" ]
}
@test 'A seven digit number that is not an Armstrong number' {
skip
run bash armstrong_numbers.sh 9926314
[ "$status" -eq 1 ]
[ "$output" = "false" ]
}
```

```
#!/bin/bash
# Check if input is an armstrong number. Print "true" or "false" and
# return with success or failure code accordingly.
validate() {
number="$1"
sum=0
digit_count=${#number}
for (( i = 0; i < digit_count; i++ )); do
sum=$(( sum + ${number:$i:1} ** digit_count))
done
if [[ "$sum" == "$number" ]]; then
echo true
else
echo false
return 1
fi
}
validate "$*" || exit 1
```

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?

## Community comments