# yurivyatkin's solution

## to Collatz Conjecture in the Common Lisp Track

Published at Dec 10 2019 · 0 comments
Instructions
Test suite
Solution

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.

## Examples

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.

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

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.

### collatz-conjecture-test.lisp

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

(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 collatz (n)
(when (plusp n)
(loop :until (= n 1) :count
(setf n (if (evenp n) (floor n 2) (+ (* n 3) 1))))))``````