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.
Check out Exercism Help for instructions to get started writing Common Lisp. That page will explain how to install and setup a Lisp implementation and how to run the tests.
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-tab-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) (defun next-collatz (n) "Calculate the next element in the Collatz sequence." (multiple-value-bind (quotient remainder) (floor n 2) (if (zerop remainder) quotient (1+ (* 3 n))))) (defun collatz (n) "Count the number of steps in the Collatz Conjecture given a starting value." (when (plusp n) (do ((steps 0 (1+ steps)) (m n (next-collatz m))) ((= m 1) steps))))
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.