🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of katrinleinweber

katrinleinweber's solution

to Collatz Conjecture in the R Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.

Installation

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

How to implement your solution

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.

How to run tests

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.

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.

test_collatz-conjecture.R

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.")
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?