Published at Nov 22 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.

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

```
gem install minitest
```

If you would like color output, you can `require 'minitest/pride'`

in
the test file, or note the alternative instruction, below, for running
the test file.

Run the tests from the exercise directory using the following command:

```
ruby armstrong_numbers_test.rb
```

To include color from the command line:

```
ruby -r minitest/pride armstrong_numbers_test.rb
```

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.

```
require 'minitest/autorun'
require_relative 'armstrong_numbers'
# Common test data version: 1.1.0 b3c2522
class ArmstrongNumbersTest < Minitest::Test
def test_zero_is_an_armstrong_number
# skip
assert ArmstrongNumbers.include?(0)
end
def test_single_digit_numbers_are_armstrong_numbers
skip
assert ArmstrongNumbers.include?(5)
end
def test_there_are_no_2_digit_armstrong_numbers
skip
refute ArmstrongNumbers.include?(10)
end
def test_three_digit_number_that_is_an_armstrong_number
skip
assert ArmstrongNumbers.include?(153)
end
def test_three_digit_number_that_is_not_an_armstrong_number
skip
refute ArmstrongNumbers.include?(100)
end
def test_four_digit_number_that_is_an_armstrong_number
skip
assert ArmstrongNumbers.include?(9_474)
end
def test_four_digit_number_that_is_not_an_armstrong_number
skip
refute ArmstrongNumbers.include?(9_475)
end
def test_seven_digit_number_that_is_an_armstrong_number
skip
assert ArmstrongNumbers.include?(9_926_315)
end
def test_seven_digit_number_that_is_not_an_armstrong_number
skip
refute ArmstrongNumbers.include?(9_926_314)
end
end
```

```
class ArmstrongNumbers
def self.include?(number)
digits = number.digits
digits.sum { |digit| digit ** digits.size } == number
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.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,104 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments