Skip to content

Commit

Permalink
Merge pull request exercism#84 from cpaulbond/master
Browse files Browse the repository at this point in the history
Added armstrong numbers exercises.
  • Loading branch information
benreyn authored May 16, 2018
2 parents dcfe99e + 76e366d commit f6512d1
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down
19 changes: 19 additions & 0 deletions exercises/armstrong-numbers/README.md
Original file line number Diff line number Diff line change
@@ -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.
41 changes: 41 additions & 0 deletions exercises/armstrong-numbers/armstrong-numbers-test.el
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions exercises/armstrong-numbers/armstrong-numbers.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
;;; armstrong-numbers.el --- armstrong-numbers Exercise (exercism)

;;; Commentary:

;;; Code:

(provide 'armstrong-numbers)
;;; armstrong-numbers.el ends here
13 changes: 13 additions & 0 deletions exercises/armstrong-numbers/example.el
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f6512d1

Please sign in to comment.