forked from exercism/emacs-lisp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request exercism#84 from cpaulbond/master
Added armstrong numbers exercises.
- Loading branch information
Showing
5 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |