Avatar of williampw

williampw's solution

to Collatz Conjecture in the Common Lisp Track

Published at Jul 29 2019 · 0 comments
Test suite

The Collatz Conjecture or 3x+1 problem can be summarized as follows:

Take any positive integer n. If n is even, divide n by 2 to get n / 2. If n is odd, multiply n by 3 and add 1 to get 3n + 1. Repeat the process indefinitely. The conjecture states that no matter which number you start with, you will always reach 1 eventually.

Given a number n, return the number of steps required to reach 1.


Starting with n = 12, the steps would be as follows:

  1. 12
  2. 6
  3. 3
  4. 10
  5. 5
  6. 16
  7. 8
  8. 4
  9. 2
  10. 1

Resulting in 9 steps. So for input n = 12, the return value would be 9.


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.


An unsolved problem in mathematics named after mathematician Lothar Collatz https://en.wikipedia.org/wiki/3x_%2B_1_problem

Submitting Incomplete Solutions

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 "collatz-conjecture")

(defpackage #:collatz-conjecture-test
  (:use #:common-lisp #:lisp-unit))

(in-package #:collatz-conjecture-test)

(define-test steps-for-1
  (assert-equal 0 (collatz-conjecture:collatz 1)))

(define-test steps-for-16
  (assert-equal 4 (collatz-conjecture:collatz 16)))

(define-test steps-for-12
  (assert-equal 9 (collatz-conjecture:collatz 12)))

(define-test steps-for-1000000
  (assert-equal 152 (collatz-conjecture:collatz 1000000)))

(define-test steps-for-0
  (assert-equal NIL (collatz-conjecture:collatz 0)))

(define-test steps-for-negative
  (assert-equal NIL (collatz-conjecture:collatz (- 0 15))))

(let ((*print-errors* t)
      (*print-failures* t))
  (run-tests :all))
(defpackage #:collatz-conjecture
  (:use #:common-lisp)
  (:export #:collatz))

(in-package #:collatz-conjecture)

(defun collatz (n)
  (when (plusp n)
    (cond ((= 1 n) 0)
          ((evenp n) (1+ (collatz (/ n 2))))
          (t (1+ (collatz (1+ (* 3 n)) ))))))

Community comments

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

williampw's Reflection

Does anyone know a good library to define memoized functions? I feel like a defun-memoize macro must have been written a thousand times already.