ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

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.

See this guide for instructions on how to setup your local R environment.

In each problem folder, there is a file named `<exercise_name>.R`

containing a function that returns a `NULL`

value. Place your implementation inside the body of the function.

Inside of RStudio, simply execute the `test_<exercise_name>.R`

script. This can be conveniently done with testthat's `auto_test`

function. Because exercism code and tests are in the same folder, use this same path for both `code_path`

and `test_path`

parameters. On the command-line, you can also run `Rscript test_<exercise_name>.R`

.

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.

```
source("./collatz-conjecture.R")
library(testthat)
context("collatz conjecture")
test_that("Input of 1 results in 0 steps", {
expect_equal(collatz_step_counter(1), 0)
})
test_that("Even input with repeated even steps", {
expect_equal(collatz_step_counter(16), 4)
})
test_that("Input which results in small number of even and odd steps", {
expect_equal(collatz_step_counter(12), 9)
})
test_that("Input which results in large number of even and odd steps", {
expect_equal(collatz_step_counter(1000000), 152)
})
test_that("Input of 0 results in an error", {
expect_error(collatz_step_counter(0))
})
test_that("Negative input results in an error", {
expect_error(collatz_step_counter(-15))
})
message("All tests passed for exercise: collatz-conjecture")
```

```
collatz_step_counter <- function(num) {
# initialise counter
steps <- 0
# recurse through cases, after testing for base case
if (num == 1)
steps
else if (num %% 2 == 0)
1 + collatz_step_counter(num / 2)
else if (num %% 2 != 0)
1 + collatz_step_counter(num * 3 + 1)
else # if (num <= 0)
stop("Num must be an integer larger than 0.")
}
```

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,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments