Avatar of TheLostLambda

TheLostLambda's solution

to Armstrong Numbers in the Common Lisp Track

Published at Mar 24 2020 · 0 comments
Test suite

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.


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


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 ~/.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.


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.


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

    (armstrong-numbers:armstrong-number-p 5)))

    (armstrong-numbers:armstrong-number-p 10)))

    (armstrong-numbers:armstrong-number-p 153)))

    (armstrong-numbers:armstrong-number-p 100)))

    (armstrong-numbers:armstrong-number-p 9474)))

    (armstrong-numbers:armstrong-number-p 9475)))

    (armstrong-numbers:armstrong-number-p 9926315)))

    (armstrong-numbers:armstrong-number-p 9926314)))

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

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?