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 = 9^1 = 9
10 != 1^2 + 0^2 = 1
153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190
Write some code to determine whether a number is an Armstrong number.
Execute the tests with:
$ mix test
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.
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.
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
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
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.
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.
Sign up Learn More
Community comments
:math.pow/2
returns a float so the equality check here is5.0 == 5
for example. I went with readability over strict equality here :O