ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Nov 05 2018
·
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.

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.

```
(ns armstrong-numbers-test
(:require [clojure.test :refer [deftest is testing]]
[armstrong-numbers :refer [armstrong?]]))
(deftest armstrong-number-0
(testing "Zero is an Armstrong number"
(is (armstrong? 0))))
(deftest armstrong-number-5
(testing "Single digit numbers are Armstrong numbers"
(is (armstrong? 5))))
(deftest not-armstrong-number-10
(testing "There are no 2 digit Armstrong numbers"
(is (not (armstrong? 10)))))
(deftest armstrong-number-153
(testing "Three digit number that is an Armstrong number"
(is (armstrong? 153))))
(deftest not-armstrong-number-100
(testing "Three digit number that is not an Armstrong number"
(is (not (armstrong? 100)))))
(deftest armstrong-number-9474
(testing "Four digit number that is an Armstrong number"
(is (armstrong? 9474))))
(deftest not-armstrong-number-9475
(testing "Four digit number that is not an Armstrong number"
(is (not (armstrong? 9475)))))
(deftest armstrong-number-9926315
(testing "Seven digit number that is an Armstrong number"
(is (armstrong? 9926315))))
(deftest not-armstrong-number-9926314
(testing "Seven digit number that is not an Armstrong number"
(is (not (armstrong? 9926314)))))
(deftest armstrong-number-21897142587612075
(testing "Seventeen digit number that is an Armstrong number"
(is (armstrong? 21897142587612075))))
```

```
(defproject armstrong-numbers "0.1.0-SNAPSHOT"
:description "armstrong-numbers exercise"
:url "https://github.com/exercism/clojure/tree/master/exercises/armstrong-numbers"
:dependencies [[org.clojure/clojure "1.9.0"]
[org.clojure/math.numeric-tower "0.0.4"]])
```

```
(ns armstrong-numbers
(:require [clojure.math.numeric-tower :as math]))
(defn armstrong? [num]
(->> num
(str)
(map int)
(map #(- % (int \0)))
(map #(math/expt % (count (str num))))
(reduce +)
(= num)))
```

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,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments