Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,17 @@
"classes",
"data_structure"
]
},
{
"slug": "collatz-conjecture",
"uuid": "c74f60b3-48fe-46c5-ae52-5e4b0787ba84",
"core": false,
"unlocked_by": "leap",
"difficulty": 2,
"topics": [
"functions",
"math"
]
}
]
}
53 changes: 53 additions & 0 deletions exercises/collatz-conjecture/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)

# Basic CMake project
cmake_minimum_required(VERSION 3.1.3)

# Name the project after the exercise
project(${exercise} CXX)

# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})

# Implementation could be only a header
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
set(exercise_cpp ${file}.cpp)
else()
set(exercise_cpp "")
endif()

# Use the common Catch library?
if(EXERCISM_COMMON_CATCH)
# For Exercism track development only
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
else()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
endif()

set_target_properties(${exercise} PROPERTIES
CXX_STANDARD 14
Comment thread
arcuru marked this conversation as resolved.
Resolved
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS OFF
)

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set_target_properties(${exercise} PROPERTIES
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
)
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
endif()

# Tell MSVC not to warn us about unchecked iterators in debug builds
if(${MSVC})
set_target_properties(${exercise} PROPERTIES
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
endif()

# Run the tests on every build
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
66 changes: 66 additions & 0 deletions exercises/collatz-conjecture/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Collatz Conjecture

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:

0. 12
1. 6
2. 3
3. 10
4. 5
5. 16
6. 8
7. 4
8. 2
9. 1

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

## Getting Started

Make sure you have read the [Installing](https://exercism.io/tracks/cpp/installation) and
[Running the Tests](https://exercism.io/tracks/cpp/tests) pages for C++ on exercism.io.
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](http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd).
Create just enough structure by declaring namespaces, functions, classes,
etc., 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,
uncomment the next test by moving the following line past the next test.

```C++
#if defined(EXERCISM_RUN_ALL_TESTS)
```

This may result in compile errors as new constructs may be invoked that
you haven't yet declared or defined. Again, fix the compile errors minimally
to get a failing test, then change the code minimally to pass the test,
refactor your implementation for readability and expressiveness and then
go on to the next test.

Try to use standard C++11 facilities in preference to writing your own
low-level algorithms or facilities by hand. [CppReference](http://en.cppreference.com/)
is a wiki reference to the C++ language and standard library. If you
are new to C++, but have programmed in C, beware of
[C traps and pitfalls](http://www.slideshare.net/LegalizeAdulthood/c-traps-and-pitfalls-for-c-programmers).

## Source

An unsolved problem in mathematics named after mathematician Lothar Collatz [https://en.wikipedia.org/wiki/3x_%2B_1_problem](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.
37 changes: 37 additions & 0 deletions exercises/collatz-conjecture/collatz_conjecture_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "test/catch.hpp"
#include "collatz_conjecture.h"
#include <stdexcept>

// Collatz-conjecture exercise test case data version 1.2.1

TEST_CASE("zero_steps_for_one")
{
REQUIRE(0 == collatz_conjecture::steps(1));
}

#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("divide_if_even")
{
REQUIRE(4 == collatz_conjecture::steps(16));
}

TEST_CASE("even_and_odd_steps")
{
REQUIRE(9 == collatz_conjecture::steps(12));
}

TEST_CASE("large_number_of_even_and_odd_steps")
{
REQUIRE(152 == collatz_conjecture::steps(1000000));
}

TEST_CASE("zero_is_an_error")
{
REQUIRE_THROWS_AS(collatz_conjecture::steps(0), std::domain_error);
}

TEST_CASE("negative_value_is_an_error")
{
REQUIRE_THROWS_AS(collatz_conjecture::steps(-15), std::domain_error);
}
#endif // !EXERCISM_RUN_ALL_TESTS
12 changes: 12 additions & 0 deletions exercises/collatz-conjecture/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "collatz_conjecture.h"
#include <stdexcept>

int collatz_conjecture::steps(int value) {
if (value <= 0) throw std::domain_error("Only positive numbers are allowed!");
int step = 0;
while (value != 1) {
value = value % 2 == 0 ? value / 2 : 3 * value + 1;
++step;
}
return step;
}
10 changes: 10 additions & 0 deletions exercises/collatz-conjecture/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#if !defined(COLLATZ_CONJECTURE_H_)
#define COLLATZ_CONJECTURE_H_

namespace collatz_conjecture {

int steps(int value);

} // namespace collatz_conjecture

#endif // !COLLATZ_CONJECTURE_H_
Loading