 # murdho's solution

## to Armstrong Numbers in the Clojure Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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.

## Submitting Incomplete Solutions

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

### armstrong_numbers_test.clj

``````(ns armstrong-numbers-test
(:require [clojure.test :refer [deftest is testing]]
[armstrong-numbers :refer [armstrong?]]))

(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? 9476)))))

(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)))))``````
``````(ns armstrong-numbers
(:require [clojure.string :as s]))

(defn digits [n]
(map #(Integer/parseInt %)
(-> n
(str)
(s/split #""))))

(defn reverse-pow [n m]
(int (Math/pow m n)))

(defn digits-to-power-of-length [n]
(let [d (digits n)
l (count d)
f (partial reverse-pow l)]
(map f d)))

(defn armstrong? [n]
(= n (reduce + (digits-to-power-of-length n))))``````