Published at Aug 06 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.

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.

Make sure you have read the "Guides" section of the C track on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the `test`

task.

```
make test
```

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

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.

```
#include "../src/collatz_conjecture.h"
#include "vendor/unity.h"
static void test_zero_steps_for_one(void)
{
TEST_ASSERT_EQUAL(0, steps(1));
}
static void test_divide_if_even(void)
{
TEST_ASSERT_EQUAL(4, steps(16));
}
static void test_even_and_odd_steps(void)
{
TEST_ASSERT_EQUAL(9, steps(12));
}
static void test_large_number_of_even_and_odd_steps(void)
{
TEST_ASSERT_EQUAL(152, steps(1000000));
}
static void test_zero_is_an_error(void)
{
TEST_ASSERT_EQUAL(ERROR_VALUE, steps(0));
}
static void test_negative_value_is_an_error(void)
{
TEST_ASSERT_EQUAL(ERROR_VALUE, steps(-15));
}
int main(void)
{
UnityBegin("collatz_conjecture.c");
RUN_TEST(test_zero_steps_for_one);
RUN_TEST(test_divide_if_even);
RUN_TEST(test_even_and_odd_steps);
RUN_TEST(test_large_number_of_even_and_odd_steps);
RUN_TEST(test_zero_is_an_error);
RUN_TEST(test_negative_value_is_an_error);
return UnityEnd();
}
```

```
/* ------------------------------------------------------------------------- */
/* exercism.io */
/* C Track Exercise: collatz_conjecture */
/* Contributed: Anthony J. Borla (ajborla@bigpond.com) */
/* ------------------------------------------------------------------------- */
#include "collatz_conjecture.h"
int steps(int start)
{
// Positive integer required to start conjecture steps
if (start < 1) return ERROR_VALUE;
// Rather than reuse, 'start', use 'n' as process is clearer
int n = start, steps = 0;
// Rather than reuse, 'start', use 'n' as process is clearer
while (n > 1)
{
// Apply conjecture:
// - if n is even, divide by
// - if n is odd, multiply by 3 and add 1
if (n % 2 == 0) n /= 2;
else { n *= 3; n += 1; }
// A step has just been completed
++steps;
}
// 'n' *should* be a 1, but trap anyway
return n == 1 ? steps : ERROR_VALUE;
}
```

```
/* ------------------------------------------------------------------------- */
/* exercism.io */
/* C Track Exercise: collatz_conjecture */
/* Contributed: Anthony J. Borla (ajborla@bigpond.com) */
/* ------------------------------------------------------------------------- */
#ifndef COLLATZ_CONJECTURE_H
#define COLLATZ_CONJECTURE_H
#define ERROR_VALUE -1
/* ------------------------------------------------------------------------- */
/* Given a positive integer, 'start', returns the number of steps required */
/* to reach a value of 1 when applying the Collatz Conjecture */
/* ------------------------------------------------------------------------- */
int steps(int start);
#endif
```

Integer arithmetic is so quick and simple in C !

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

## Community comments