Repository: exercism/clojure
Pull request #209 (2017-12-30 06:02:51)

add armstrong-numbers


Patch:
diff --git a/config.json b/config.json
index b442c99..e312281 100644
--- a/config.json
+++ b/config.json
@@ -9,6 +9,14 @@
       "unlocked_by": null,
       "uuid": "1378910d-9bec-4217-bd40-07a8967fa3ad"
     },
+    {
+      "core": false,
+      "difficulty": 1,
+      "slug": "armstrong-numbers",
+      "topics": null,
+      "unlocked_by": null,
+      "uuid": "a4bdbdfd-1db1-425c-a243-dd032dc7b93a"
+    },
     {
       "core": false,
       "difficulty": 1,
diff --git a/exercises/armstrong-numbers/README.md b/exercises/armstrong-numbers/README.md
new file mode 100644
index 0000000..5323751
--- /dev/null
+++ b/exercises/armstrong-numbers/README.md
@@ -0,0 +1,19 @@
+## Armstrong numbers
+
+An [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_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 = 2`
+- 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.
+
+## Source
+
+See more at [wikipedia](https://en.wikipedia.org/wiki/Narcissistic_number)
+
+## Submitting Incomplete Solutions
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/exercises/armstrong-numbers/project.clj b/exercises/armstrong-numbers/project.clj
new file mode 100644
index 0000000..4ec8c55
--- /dev/null
+++ b/exercises/armstrong-numbers/project.clj
@@ -0,0 +1,4 @@
+(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"]])
diff --git a/exercises/armstrong-numbers/src/armstrong_numbers.clj b/exercises/armstrong-numbers/src/armstrong_numbers.clj
new file mode 100644
index 0000000..6da2f86
--- /dev/null
+++ b/exercises/armstrong-numbers/src/armstrong_numbers.clj
@@ -0,0 +1,3 @@
+(ns armstrong-numbers)
+
+(defn armstrong? [n])
diff --git a/exercises/armstrong-numbers/src/example.clj b/exercises/armstrong-numbers/src/example.clj
new file mode 100644
index 0000000..ed83dc5
--- /dev/null
+++ b/exercises/armstrong-numbers/src/example.clj
@@ -0,0 +1,6 @@
+(ns armstrong-numbers)
+
+(defn armstrong? [n]
+  (let [digits (map (comp read-string str) (str n))
+        l      (count digits)]
+    (== n (reduce + (map #(Math/pow % l) digits)))))


Test patch:
diff --git a/exercises/armstrong-numbers/test/armstrong_numbers_test.clj b/exercises/armstrong-numbers/test/armstrong_numbers_test.clj
new file mode 100644
index 0000000..3d346e9
--- /dev/null
+++ b/exercises/armstrong-numbers/test/armstrong_numbers_test.clj
@@ -0,0 +1,35 @@
+(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 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)))))