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

vlzware's solution

to Collatz Conjecture in the C Track

Published at Jul 13 2018 · 0 comments
Test suite


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.


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.

Getting Started

Make sure you have read the C page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

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

Submitting Incomplete Solutions

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"

void test_zero_steps_for_one(void)
   TEST_ASSERT_EQUAL(0, steps(1));

void test_divide_if_even(void)
   TEST_ASSERT_EQUAL(4, steps(16));

void test_even_and_odd_steps(void)
   TEST_ASSERT_EQUAL(9, steps(12));

void test_large_number_of_even_and_odd_steps(void)
   TEST_ASSERT_EQUAL(152, steps(1000000));

void test_zero_is_an_error(void)

void test_negative_value_is_an_error(void)

int main(void)


   return 0;


#include "collatz_conjecture.h"

int iseven(int n)
	return !(n & 1);

int steps(int start)
	if (start <= 0)
		return ERROR_VALUE;

	int res = 0;
	int n = start;
	while(n != 1) {
		n = (iseven(n))
			? n/2
			: 3*n + 1;
	return res;



#define ERROR_VALUE -1

int steps(int start);


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?