Published at Jul 13 2018
·
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.

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

- 12
- 6
- 3
- 10
- 5
- 16
- 8
- 4
- 2
- 1

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

For installation and learning resources, refer to the exercism help page.

To run the test suite, execute the following command:

```
stack test
```

```
No .cabal file found in directory
```

You are probably running an old stack version and need to upgrade it.

```
No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...
```

Just do as it says and it will download and install the correct compiler version:

```
stack setup
```

If you want to play with your solution in GHCi, just run the command:

```
stack ghci
```

The exercism/haskell repository on GitHub is the home for all of the Haskell exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!

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.

```
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# LANGUAGE RecordWildCards #-}
import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
import CollatzConjecture (collatz)
main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs
specs :: Spec
specs = describe "collatz" $ for_ cases test
where
test Case{..} = it description assertion
where
assertion = collatz number `shouldBe` expected
data Case = Case { description :: String
, number :: Integer
, expected :: Maybe Integer
}
cases :: [Case]
cases = [ Case { description = "zero steps for one"
, number = 1
, expected = Just 0
}
, Case { description = "divide if even"
, number = 16
, expected = Just 4
}
, Case { description = "even and odd steps"
, number = 12
, expected = Just 9
}
, Case { description = "Large number of even and odd steps"
, number = 1000000
, expected = Just 152
}
, Case { description = "zero is an error"
, number = 0
, expected = Nothing
}
, Case { description = "negative value is an error"
, number = -15
, expected = Nothing
}
]
```

```
module CollatzConjecture (collatz) where
collatz :: Integer -> Maybe Integer
collatz num
| num <= 0 = Nothing
| otherwise = Just $ helper num
helper :: Integer -> Integer
helper num
| num == 1 = 0
| even num = 1 + helper (div num 2)
| otherwise = 1 + helper (3 * num + 1)
```

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.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,373 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments