Start learning free

LESSON 133 OF 347 · LOOPS & CONDITIONALS

Collatz Conjecture in C#: Printing the Sequence with a while Loop

Practice Read the full explanation

The Collatz conjecture in mathematics asks whether repeating two simple arithmetic operations will eventually transform every positive integer into one. It concerns sequences of integers in which each term is obtained from the previous term as follows:
if the previous term is even, the next term is one half of the previous term.
If the previous term is odd, the next term is 3 times the previous term plus 1.
The conjecture is that these sequences always reach 1, no matter which positive integer is chosen to start the sequence.

You are given an integer n and you should print to the console all the values that n takes throughout the Collatz Conjecture.

Example:

Input: n = 3

Output:
3
10
5
16
8
4
2
1

Explanation:
n = 3  => n is odd  => n becomes 3 * 3 + 1 = 10
n = 10 => n is even => n becomes 10 / 2    = 5
n = 5  => n is odd  => n becomes 5 * 3 + 1 = 16
n = 16 => n is even => n becomes 16 / 2    = 8
n = 8  => n is even => n becomes 8 / 2     = 4
n = 4  => n is even => n becomes 4 / 2     = 2
n = 2  => n is even => n becomes 2 / 1     => 1
n = 1 => stop
Hints:

Look at the example and notice that we apply a transformation over and over until we reach a final point where we stop. Does that ring a bell?

We need to repeat the transformation code as long as one condition holds (n is greater than 1). How do you write that in code?

We will use a while loop that executes as long as n > 1 . What should we do inside this loop?

If n is even, we should apply a transformation. Otherwise, we should apply the other transformation. How do we handle these different scenarios?

We can do this using an if - else statement.

Video Solution:

STEP 1 OF 5
    Ctrl + Enter to check

    C# · LOOPS & CONDITIONALS

    Collatz Conjecture in C#, explained

    Problem overview

    The method PrintSeries(int n) prints every value that n takes in the Collatz sequence, starting with n itself and ending at 1. The rules to get the next value are:

    • if the current value is even, the next one is half of it;
    • if it is odd, the next one is three times the value plus one.

    The Collatz conjecture says that, whichever positive integer you start from, this process always reaches 1. Nobody has proven it, but this lesson doesn't need the proof: it only has to run the process and print each value.

    For n = 3 the output is 3, 10, 5, 16, 8, 4, 2, 1, one number per line. Starting at 3 (odd) gives 3 * 3 + 1 = 10. Then 10 is even, giving 5, and so on until the sequence reaches 1.

    Approach: repeat a rule until a condition holds

    The number of steps is not known in advance, so a for loop with a fixed count doesn't fit. A while loop does: it repeats its body as long as a condition is true. Here the condition is n > 1, because the sequence is finished when it reaches 1.

    Inside the loop, the current value is replaced by the next one, using an if/else for the two rules, and then the new value is printed. The starting value is printed once, before the loop, because the loop only prints values it has just produced.

    Step by step

    1. Print n before the loop. The series begins with the input value, which no loop pass will produce.
    2. Open a while (n > 1) loop. The parameter n is an ordinary local variable, so the loop can assign new values to it directly.
    3. Handle the even case. When n % 2 == 0, set n = n / 2. Integer division drops any remainder, which is harmless here because n is even and the division is exact.
    4. Handle the odd case. In the else branch, set n = 3 * n + 1.
    5. Print the new value, after the if/else but still inside the loop, so it runs for both cases.

    The solution in C#

    class Solution {
        public void PrintSeries(int n) {
            Console.WriteLine(n);
            while (n > 1) {
                if (n % 2 == 0) {
                    n = n / 2;
                } else {
                    n = 3 * n + 1;
                }
                Console.WriteLine(n);
            }
        }
    }
    

    Starting at 6, the method prints:

    6
    3
    10
    5
    16
    8
    4
    2
    1
    

    C# details worth knowing:

    • Parameters can be reassigned. Arguments are passed by value, so changing n inside the method changes only the method's own copy. The caller's variable stays the same.
    • Integer division truncates. With two int operands, / produces an int. For example 7 / 2 is 3. Here the dividend is always even, so nothing is lost.
    • Return type void. The method prints, and returns nothing, so it has no return statement.
    • Overflow wraps. If 3 * n + 1 exceeds the maximum int value (2,147,483,647), C# wraps around silently to a negative number by default. For a very large starting value this could end the loop early, since a negative number fails n > 1. The lesson's tests use small values, where this does not happen.

    Complexity

    The running time depends on how many terms the sequence has before it reaches 1, which is not a simple function of n. In practice it is short for small numbers: 27, for example, takes 111 steps. Extra space is O(1), since the method keeps just the current value.

    It helps to see why the loop must stop at 1 and not go on. From 1 the odd rule gives 4, then 2, then 1 again, a cycle that would repeat forever. Stopping the moment the value reaches 1 is what makes the loop finite.

    Edge cases and common mistakes

    • n = 1. The method prints 1 and the loop never runs. That is correct: the sequence is already at its end.
    • Printing in the wrong place. If the print is inside only the else branch, the even values are missing. If it comes before the update instead of after it, the output repeats a value and misses the final 1.
    • Using n >= 1. Since 1 is odd, it becomes 4, then 2, then 1, and the loop never ends.
    • Forgetting to update n. Writing n / 2 without assigning the result leaves n unchanged, and the program loops forever.
    • Doing the division with a decimal type. Using double for n would print values like 5 and 16 the same way, but it invites rounding questions and makes the % test less natural. The sequence only needs whole numbers, so int is the right type.

    Testing

    • n = 3 prints 3, 10, 5, 16, 8, 4, 2, 1.
    • n = 1 prints just 1.
    • n = 2 prints 2, 1.
    • n = 6 prints 6, 3, 10, 5, 16, 8, 4, 2, 1.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free