🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of epequeno

epequeno's solution

to Armstrong Numbers in the Elm Track

Published at Apr 19 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.

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The code you have to write is located inside the src/ directory of the exercise. Elm automatically installs packages dependencies the first time you run the tests so we can start by running the tests from the exercise directory with:

$ elm-test

To automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the tests suite in the file tests/Tests.elm, be sure to remove the skip <| calls from each test until you get them all passing!

Source

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

Submitting Incomplete Solutions

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

Tests.elm

module Tests exposing (tests)

import ArmstrongNumbers exposing (isArmstrongNumber)
import Expect
import Test exposing (..)


tests : Test
tests =
    describe "{exercise}"
        [ test "Zero is an Armstrong number" <|
            \() ->
                Expect.equal True
                    (isArmstrongNumber 0)
        , skip <|
            test "Single digit numbers are Armstrong numbers" <|
                \() ->
                    Expect.equal True
                        (isArmstrongNumber 5)
        , skip <|
            test "There are no 2 digit Armstrong numbers" <|
                \() ->
                    Expect.equal False
                        (isArmstrongNumber 10)
        , skip <|
            test "Three digit number that is an Armstrong number" <|
                \() ->
                    Expect.equal True
                        (isArmstrongNumber 153)
        , skip <|
            test "Three digit number that is not an Armstrong number" <|
                \() ->
                    Expect.equal False
                        (isArmstrongNumber 100)
        , skip <|
            test "Four digit number that is an Armstrong number" <|
                \() ->
                    Expect.equal True
                        (isArmstrongNumber 9474)
        , skip <|
            test "Four digit number that is not an Armstrong number" <|
                \() ->
                    Expect.equal False
                        (isArmstrongNumber 9475)
        , skip <|
            test "Seven digit number that is an Armstrong number" <|
                \() ->
                    Expect.equal True
                        (isArmstrongNumber 9926315)
        , skip <|
            test "Seven digit number that is not an Armstrong number" <|
                \() ->
                    Expect.equal False
                        (isArmstrongNumber 9926314)
        ]
module ArmstrongNumbers exposing (isArmstrongNumber)


isArmstrongNumber : Int -> Bool
isArmstrongNumber nb =
    let
        len =
            nb
                |> String.fromInt
                |> String.length
    in
    nb
        |> String.fromInt
        |> String.toList
        |> List.map String.fromChar
        |> List.map String.toInt
        |> List.map (Maybe.withDefault 0)
        |> List.map (\x -> x ^ len)
        |> List.sum
        |> (==) nb

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?