# TheLostLambda's solution

## to Armstrong Numbers in the Common Lisp Track

Published at Mar 24 2020 · 0 comments
Instructions
Test suite
Solution

An Armstrong 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.

## Setup

Check out Installing Common Lisp for instructions to get started or take a look at the guides available in the track's side bar.

## Formatting

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.

### VIM

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

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 `~/.emacs` (or `~/.emacs.d/init.el`) in 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.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### armstrong-numbers-test.lisp

``````;;;
;;; armstrong-numbers v1.0.0
;;;

(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)

(defun armstrong-number-p (number)
(let ((digits (get-digits number)))
(= number (loop for d in digits summing (expt d (length digits))))))

(defun get-digits (num)
(loop for digit across (write-to-string num)
collect (digit-char-p digit)))``````