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:
Resulting in 9 steps. So for input n = 12, the return value would be 9.
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.
An unsolved problem in mathematics named after mathematician Lothar Collatz https://en.wikipedia.org/wiki/3x_%2B_1_problem
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)))) #-xlisp-test (let ((*print-errors* t) (*print-failures* t)) (run-tests :all))
(defpackage #:collatz-conjecture (:use #:common-lisp) (:export #:collatz)) (in-package #:collatz-conjecture) ;; Using do (defun collatz (n) (when (> n 0) (do ((x n (if (evenp x) (/ x 2) (1+ (* 3 x)))) (i 0 (1+ i))) ((= x 1) i)))) ;; Using loop (defun collatz-loop (n) (when (> n 0) (loop for x = n then (if (evenp x) (/ x 2) (1+ (* 3 x))) until (= x 1) count x)))
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.