# stefandevai's solution

## to Perfect Numbers in the Common Lisp Track

Published at Aug 15 2019 · 0 comments
Instructions
Test suite
Solution

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

• Perfect: aliquot sum = number
• 6 is a perfect number because (1 + 2 + 3) = 6
• 28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
• Abundant: aliquot sum > number
• 12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16
• 24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36
• Deficient: aliquot sum < number
• 8 is a deficient number because (1 + 2 + 4) = 7
• Prime numbers are deficient

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.

## 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.

## Source

Taken from Chapter 2 of Functional Thinking by Neal Ford. http://shop.oreilly.com/product/0636920029687.do

## Submitting Incomplete Solutions

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

### perfect-numbers-test.lisp

``````(ql:quickload "lisp-unit")

(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 count-factors (n)
(loop :for d :from 1 :to (truncate n 2)
:when (zerop (mod n d))
:sum d))

(defun classify (n)
(when (plusp n)
(let ((n-sum (count-factors n)))
(cond ((= n-sum n) "perfect")
((> n-sum n) "abundant")
((< n-sum n) "deficient")))))``````