An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.
9 = 9^1 = 9
10 != 1^2 + 0^2 = 1
153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190
Write some code to determine whether a number is an Armstrong number.
While Common Lisp doesn't care about indentation and layout of code, nor whether you use spaces or tabs, this is an important consideration for submissions to exercism.io. Excercism.io's code widget cannot handle mixing of tab and space characters well so using only spaces is recommended to make the code more readable to the human reviewers. Please review your editors settings on how to accomplish this. Below are instructions for popular editors for Common Lisp.
Use the following commands to ensure VIM uses only spaces for indentation:
:set tabstop=2 :set shiftwidth=2 :set expandtab
(or as a oneliner
:set tabstop=2 shiftwidth=2 expandtab). This can
be added to your
~/.vimrc file to use it all the time.
Emacs is very well suited for editing Common Lisp and has many powerful add-on packages available. The only thing that one needs to do with a stock emacs to make it work well with exercism.io is to evaluate the following code:
(setq-default indent-tabs-mode nil)
This can be placed in your
order to have it set whenever Emacs is launched.
One suggested add-on for Emacs and Common Lisp is SLIME which offers tight integration with the REPL; making iterative coding and testing very easy.
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
;;; ;;; armstrong-numbers v1.0.0 ;;; (ql:quickload "lisp-unit") #-xlisp-test (load "armstrong-numbers") (defpackage #:armstrong-numbers-test (:use #:common-lisp #:lisp-unit)) (in-package #:armstrong-numbers-test) (define-test single-digit-numbers-are-armstrong-numbers (assert-equal T (armstrong-numbers:armstrong-number-p 5))) (define-test there-are-no-2-digit-armstrong-numbers (assert-equal 'NIL (armstrong-numbers:armstrong-number-p 10))) (define-test three-digit-number-that-is-an-armstrong-number (assert-equal T (armstrong-numbers:armstrong-number-p 153))) (define-test three-digit-number-that-is-not-an-armstrong-number (assert-equal 'NIL (armstrong-numbers:armstrong-number-p 100))) (define-test four-digit-number-that-is-an-armstrong-number (assert-equal T (armstrong-numbers:armstrong-number-p 9474))) (define-test four-digit-number-that-is-not-an-armstrong-number (assert-equal 'NIL (armstrong-numbers:armstrong-number-p 9475))) (define-test seven-digit-number-that-is-an-armstrong-number (assert-equal T (armstrong-numbers:armstrong-number-p 9926315))) (define-test seven-digit-number-that-is-not-an-armstrong-number (assert-equal 'NIL (armstrong-numbers:armstrong-number-p 9926314))) #-xlisp-test (let ((*print-errors* t) (*print-failures* t)) (run-tests :all))
(in-package #:cl-user) (defpackage #:armstrong-numbers (:use #:cl) (:export #:armstrong-number-p)) (in-package #:armstrong-numbers) ;;;153 is an Armstrong number, because: 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 (defun integer-to-list(number) (if (and (numberp number) (> number 0) (rationalp number)) (map 'list #'digit-char-p (prin1-to-string number)) nil)) (defun power(number) "gets length of number" (length (write-to-string number))) (defun digit-power-sum(number) "sums powered digits of number" (loop for i in (integer-to-list number) sum (expt i (power number)))) (defun armstrong-number-p (number) (cond ((null number) nil) ((eql number (digit-power-sum number))))) (defun armstrong-number-list(limit) (loop for i from 1 to limit if (armstrong-number-p i) collect i))
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.