# thekeele's solution

## to Armstrong Numbers in the Elixir Track

Published at Jan 06 2020 · 1 comment
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

Execute the tests with:

``````\$ mix test
``````

### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.

For example:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

## Submitting Incomplete Solutions

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

### armstrong_number_test.exs

``````defmodule ArmstrongNumberTest do
use ExUnit.Case

# @tag :pending
test "Zero is an Armstrong number" do
assert ArmstrongNumber.valid?(0)
end

test "Single digit numbers are Armstrong numbers" do
assert ArmstrongNumber.valid?(5)
end

@tag :pending
test "There are no 2 digit Armstrong Numbers" do
refute ArmstrongNumber.valid?(10)
end

@tag :pending
test "Three digit number that is an Armstrong number" do
assert ArmstrongNumber.valid?(153)
end

@tag :pending
test "Three digit number that is not an Armstrong number" do
refute ArmstrongNumber.valid?(100)
end

@tag :pending
test "Four digit number that is an Armstrong number" do
assert ArmstrongNumber.valid?(9474)
end

@tag :pending
test "Four digit number that is not an Armstrong number" do
refute ArmstrongNumber.valid?(9475)
end

@tag :pending
test "Seven digit number that is an Armstrong number" do
assert ArmstrongNumber.valid?(9_926_315)
end

@tag :pending
test "Seven digit number that is not an Armstrong number" do
refute ArmstrongNumber.valid?(9_926_134)
end
end``````

### test_helper.exs

``````ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)``````
``````defmodule ArmstrongNumber do
@spec valid?(integer) :: boolean
def valid?(number) do
digits = Integer.digits(number)
num_digits = length(digits)
sum_of_digits =
for digit <- digits, reduce: 0 do
sum -> sum + :math.pow(digit, num_digits)
end

number == sum_of_digits
end
end``````

`:math.pow/2` returns a float so the equality check here is `5.0 == 5` for example. I went with readability over strict equality here :O