Avatar of paulfioravanti

paulfioravanti's solution

to Armstrong Numbers in the Elixir Track

Published at Aug 18 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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.

Source

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

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

  test "One-digit numbers are Armstrong numbers" do
    assert ArmstrongNumber.valid?(1)
    assert ArmstrongNumber.valid?(6)
    assert ArmstrongNumber.valid?(9)
  end

  @tag :pending
  test "Two-digit numbers aren't Armstrong numbers" do
    refute ArmstrongNumber.valid?(11)
    refute ArmstrongNumber.valid?(89)
    refute ArmstrongNumber.valid?(44)
  end

  @tag :pending
  test "Three-digit numbers that are the Armstrong numbers" do
    assert ArmstrongNumber.valid?(153)
    assert ArmstrongNumber.valid?(370)
  end

  @tag :pending
  test "Three-digit numbers that aren't the Armstrong numbers" do
    refute ArmstrongNumber.valid?(555)
    refute ArmstrongNumber.valid?(662)
  end

  @tag :pending
  test "Four-digit number that is the Armstrong numbers" do
    assert ArmstrongNumber.valid?(1634)
  end

  @tag :pending
  test "Four-digit number that is not the Armstrong numbers" do
    refute ArmstrongNumber.valid?(9989)
  end

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

  @tag :pending
  test "Seven-digit number that is not the Armstrong numbers" do
    refute ArmstrongNumber.valid?(2_369_989)
  end

  @tag :pending
  test "Ten-digit number that is the Armstrong numbers" do
    assert ArmstrongNumber.valid?(4_679_307_774)
  end

  @tag :pending
  test "Fourteen-digit number that is the Armstrong numbers" do
    assert ArmstrongNumber.valid?(28_116_440_335_967)
  end
end

test_helper.exs

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
defmodule ArmstrongNumber do
  @moduledoc """
  Provides a way to validate whether or not a number is an Armstrong number
  """

  @spec valid?(integer) :: boolean
  def valid?(number) do
    sum_of_powers =
      number
      |> Integer.digits()
      |> sum_powers()

    number == sum_of_powers
  end

  defp sum_powers(digits) do
    length = length(digits)
    Enum.reduce(digits, 0, &power(length, &1, &2))
  end

  defp power(length, digit, acc) do
    acc + :math.pow(digit, length)
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?