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

n0mn0m's solution

to Collatz Conjecture in the C# Track

Published at Nov 05 2020 · 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.

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.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit CollatzConjecture.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

An unsolved problem in mathematics named after mathematician Lothar Collatz https://en.wikipedia.org/wiki/3x_%2B_1_problem

CollatzConjectureTests.cs

// This file was auto-generated based on version 1.2.1 of the canonical data.

using System;
using Xunit;

public class CollatzConjectureTests
{
    [Fact]
    public void Zero_steps_for_one()
    {
        Assert.Equal(0, CollatzConjecture.Steps(1));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Divide_if_even()
    {
        Assert.Equal(4, CollatzConjecture.Steps(16));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Even_and_odd_steps()
    {
        Assert.Equal(9, CollatzConjecture.Steps(12));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Large_number_of_even_and_odd_steps()
    {
        Assert.Equal(152, CollatzConjecture.Steps(1000000));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Zero_is_an_error()
    {
        Assert.Throws<ArgumentOutOfRangeException>(() => CollatzConjecture.Steps(0));
    }

    [Fact(Skip = "Remove this Skip property to run this test")]
    public void Negative_value_is_an_error()
    {
        Assert.Throws<ArgumentOutOfRangeException>(() => CollatzConjecture.Steps(-15));
    }
}
using System;

public static class CollatzConjecture
{
    public static int Steps(int number)
    {
        if (number < 1) throw new ArgumentOutOfRangeException();
        if (number == 1) return 0;
        if (number % 2 == 0) return 1 + Steps(number / 2);
        return 1 + Steps(number * 3 + 1);
    }
}

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?