Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for natural numbers.
The Greek mathematician Nicomachus devised a classification scheme for natural numbers, identifying each as belonging uniquely to the categories of perfect, abundant, or deficient based on their aliquot sum. The aliquot sum is defined as the sum of the factors of a number not including the number itself. For example, the aliquot sum of 15 is (1 + 3 + 5) = 9
Implement a way to determine whether a given number is perfect. Depending on your language track, you may also need to implement a way to determine whether a given number is abundant or deficient.
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.
Taken from Chapter 2 of Functional Thinking by Neal Ford. http://shop.oreilly.com/product/0636920029687.do
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
(ql:quickload "lisp-unit") #-xlisp-test (load "perfect-numbers") (defpackage #:perfect-numbers-test (:use #:common-lisp #:lisp-unit)) (in-package #:perfect-numbers-test) ;; Perfect numbers tests: (define-test smallest-perfect-number (assert-equal "perfect" (perfect-numbers:classify 6))) (define-test medium-perfect-number (assert-equal "perfect" (perfect-numbers:classify 28))) (define-test large-perfect-number (assert-equal "perfect" (perfect-numbers:classify 33550336))) ;; Abundant numbers tests: (define-test smallest-abundant-number (assert-equal "abundant" (perfect-numbers:classify 12))) (define-test medium-abundant-number (assert-equal "abundant" (perfect-numbers:classify 30))) (define-test large-abundant-number (assert-equal "abundant" (perfect-numbers:classify 33550335))) ;; Deficient numbers tests: (define-test smallest-prime-deficient-number (assert-equal "deficient" (perfect-numbers:classify 2))) (define-test smallest-non-prime-deficient-number (assert-equal "deficient" (perfect-numbers:classify 1))) (define-test medium-deficient-number (assert-equal "deficient" (perfect-numbers:classify 32))) (define-test large-deficient-number (assert-equal "deficient" (perfect-numbers:classify 33550337))) ;; Undefined values of classify tests: (define-test undefinded-0 (assert-equal NIL (perfect-numbers:classify 0))) (define-test undefined-negative (assert-equal NIL (perfect-numbers:classify -3))) #-xlisp-test (let ((*print-errors* t) (*print-failures* t)) (run-tests :all))
(defpackage #:perfect-numbers (:use #:common-lisp) (:export #:classify)) (in-package #:perfect-numbers) (defun aliquot-sum (n) (loop for x from 1 to (floor n 2) when (zerop (rem n x)) sum x)) (defun classify (n) (when (> n 0) (let ((sum (aliquot-sum n))) (cond ((= n sum) "perfect") ((< n sum) "abundant") ((> n sum) "deficient")))))
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.