🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # SergiiVlasiuk's solution

## to Armstrong Numbers in the Scala Track

Published at Aug 16 2019 · 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.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test` in the exercise directory.

For more detailed info about the Scala track see the help page.

## Submitting Incomplete Solutions

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

### ArmstrongNumbersTest.scala

``````import org.scalatest.{Matchers, FunSuite}

/** @version 1.0.0 */
class ArmstrongNumbersTest extends FunSuite with Matchers {

test("Single digit numbers are Armstrong numbers") {
ArmstrongNumbers.isArmstrongNumber(5) should be (true)
}

test("There are no 2 digit Armstrong numbers") {
pending
ArmstrongNumbers.isArmstrongNumber(10) should be (false)
}

test("Three digit number that is an Armstrong number") {
pending
ArmstrongNumbers.isArmstrongNumber(153) should be (true)
}

test("Three digit number that is not an Armstrong number") {
pending
ArmstrongNumbers.isArmstrongNumber(100) should be (false)
}

test("Four digit number that is an Armstrong number") {
pending
ArmstrongNumbers.isArmstrongNumber(9474) should be (true)
}

test("Four digit number that is not an Armstrong number") {
pending
ArmstrongNumbers.isArmstrongNumber(9475) should be (false)
}

test("Seven digit number that is an Armstrong number") {
pending
ArmstrongNumbers.isArmstrongNumber(9926315) should be (true)
}

test("Seven digit number that is not an Armstrong number") {
pending
ArmstrongNumbers.isArmstrongNumber(9926314) should be (false)
}
}``````
``````object ArmstrongNumbers {

// https://exercism.io/profiles/gerdreiss
def isArmstrongNumber(number: Int): Boolean = {
val digits = number.toString
val digitize: Char => Int = _.asDigit
val power: Int => Double = math.pow(_, digits.length)
number == digits.map(digitize andThen power).sum
}

//  // https://exercism.io/profiles/djmoorejr
//  def isArmstrongNumber( i: Int ): Boolean = {
//    val len = i.toString.length
//    i.toString.map(c => Math.pow(c.asDigit, len).toInt).sum == i
//  }

/*
def isArmstrongNumber(param: Int): Boolean = {

def size(int: Int): Int =
if (int <= 0) 0
else 1 + size(int / 10)

val power = size(param)

def calculateSum(f: (Int, Int) => Int)(param: Int): Int =
if (param > 0) f(param % 10, power) + calculateSum(f)(param / 10)
else 0

def calculatePower(base: Int, power: Int): Int =
if (power > 0) base * calculatePower(base, power - 1)
else 1

param == calculateSum(calculatePower)(param)
}
*/

}``````