# SunJukebox's solution

## to Armstrong Numbers in the Bash Track

Published at Jun 06 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.

Run the tests with:

``````bats armstrong_numbers_test.sh
``````

After the first test(s) pass, continue by commenting out or removing the `[[ \$BATS_RUN_SKIPPED == true ]] || skip` annotations prepending other tests.

To run all tests, including the ones with `skip` annotations, run:

``````BATS_RUN_SKIPPED=true bats armstrong_numbers_test.sh
``````

## External utilities

`Bash` is a language to write "scripts" -- programs that can call external tools, such as `sed`, `awk`, `date` and even programs written in 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 installation of 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. Note that there are some types of problems that bash cannot solve, such as performing floating point arithmetic and manipulating dates: for those, you must call out to an external tool.

## Submitting Incomplete Solutions

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

### armstrong_numbers_test.sh

``````#!/usr/bin/env bash

# local version: 1.1.0.0

@test 'Zero is Armstrong numbers' {
# [[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash armstrong_numbers.sh 0
(( status == 0 ))
[[ \$output == "true" ]]
}

@test 'Single digits are Armstrong numbers' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash armstrong_numbers.sh 5

(( status == 0 ))
[[ \$output == "true" ]]
}

@test 'There are no two digit Armstrong numbers' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash armstrong_numbers.sh 10

(( status == 0 ))
[[ \$output == "false" ]]
}

@test 'A three digit number that is an Armstrong number' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash armstrong_numbers.sh 153

(( status == 0 ))
[[ \$output == "true" ]]
}

@test 'A three digit number that is not an Armstrong number' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash armstrong_numbers.sh 100

(( status == 0 ))
[[ \$output == "false" ]]
}

@test 'A four digit number that is an Armstrong number' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash armstrong_numbers.sh 9474

(( status == 0 ))
[[ \$output == "true" ]]
}

@test 'A four digit number that is not an Armstrong number' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash armstrong_numbers.sh 9475

(( status == 0 ))
[[ \$output == "false" ]]
}

@test 'A seven digit number that is an Armstrong number' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash armstrong_numbers.sh 9926315

(( status == 0 ))
[[ \$output == "true" ]]
}

@test 'A seven digit number that is not an Armstrong number' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash armstrong_numbers.sh 9926314

(( status == 0 ))
[[ \$output == "false" ]]
}``````
``````#!/usr/bin/env bash

main (){
input=\$1
num_digits=\${#input}
result=0

for (( i=0; i<num_digits; i++ )); do
result=\$((\$result+\${input:i:1}**num_digits))
done

if [[ \$result -eq \$input ]]; then
echo "true"
else
echo "false"
fi
}
main "\$@"``````