diff --git a/config.json b/config.json index 7588c773..62f85141 100644 --- a/config.json +++ b/config.json @@ -176,6 +176,14 @@ "topics": null, "unlocked_by": null, "uuid": "a252e137-8a63-4f26-b119-7264f12a257a" + }, + { + "core": false, + "difficulty": 1, + "slug": "armstrong-numbers", + "topics": null, + "unlocked_by": null, + "uuid": "059141f0-f28d-4d10-a03a-7852dbfc2d2e" } ], "foregone": [], diff --git a/exercises/armstrong-numbers/README.md b/exercises/armstrong-numbers/README.md new file mode 100644 index 00000000..95aa7369 --- /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 = 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. + +## 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/armstrong-numbers-test.el b/exercises/armstrong-numbers/armstrong-numbers-test.el new file mode 100644 index 00000000..4851dbfa --- /dev/null +++ b/exercises/armstrong-numbers/armstrong-numbers-test.el @@ -0,0 +1,41 @@ +;;; armstrong-numbers-test.el --- Tests for armstrong-numbers (exercism) + +;;; Commentary: + +;;; Code: + +(load-file "armstrong-numbers.el") + +(ert-deftest armstrong-number-5 () + "Single digit numbers are Armstrong numbers" + (should (armstrong-p 5))) + +(ert-deftest not-armstrong-number-10 () + "There are no 2 digit Armstrong numbers" + (should (not (armstrong-p 10)))) + +(ert-deftest armstrong-number-153 () + "Three digit number that should an Armstrong number" + (should (armstrong-p 153))) + +(ert-deftest not-armstrong-number-100 () + "Three digit number that should an Armstrong number" + (should (not (armstrong-p 100)))) + +(ert-deftest armstrong-number-9474 () + "Four digit number that should an Armstrong number" + (should (armstrong-p 9474))) + +(ert-deftest not-armstrong-number-9475 () + "Four digit number that should not an Armstrong number" + (should (not (armstrong-p 9476)))) + +(ert-deftest armstrong-number-9926315 () + "Seven digit number that should an Armstrong number" + (should (armstrong-p 9926315))) + +(ert-deftest not-armstrong-number-9926314 () + "Seven digit number that should not an Armstrong number" + (should (not (armstrong-p 9926314)))) + +;;; armstrong-numbers-test.el ends here diff --git a/exercises/armstrong-numbers/armstrong-numbers.el b/exercises/armstrong-numbers/armstrong-numbers.el new file mode 100644 index 00000000..d41293c1 --- /dev/null +++ b/exercises/armstrong-numbers/armstrong-numbers.el @@ -0,0 +1,8 @@ +;;; armstrong-numbers.el --- armstrong-numbers Exercise (exercism) + +;;; Commentary: + +;;; Code: + +(provide 'armstrong-numbers) +;;; armstrong-numbers.el ends here diff --git a/exercises/armstrong-numbers/example.el b/exercises/armstrong-numbers/example.el new file mode 100644 index 00000000..704796b3 --- /dev/null +++ b/exercises/armstrong-numbers/example.el @@ -0,0 +1,13 @@ +;;; armstrong-numbers.el --- armstrong-numbers Exercise (exercism) + +;;; Commentary: + +;;; Code: + +(defun armstrong-p (n) + (let* ((digits (mapcar #'(lambda (d) (- d ?0)) (string-to-list (int-to-string n)))) + (p (length digits))) + (= n (apply '+ (mapcar #'(lambda (d) (expt d p)) digits))))) + +(provide 'armstrong-numbers) +;;; armstrong-numbers.el ends here