The Collatz conjecture resists proof despite its simple rule. Take any positive integer. If it is even, divide it by 2.

If it is odd, multiply it by 3 and add 1. Repeat this process and the conjecture says you will always reach 1, no matter where you start. It is also called the hailstone sequence because the numbers bounce up and down like hailstones in a cloud before finally reaching 1.

Why it matters

The Collatz conjecture has been verified by computer for every number up to 2^68, but verification is not proof. Mathematicians have not found a general argument that covers every starting number, and no counterexample has been found.

Implementing the Collatz sequence in Python

A Collatz function takes a starting number, applies the even/odd rule, and records every value until it reaches 1.

def collatz(n):
    seq = [n]
    while n != 1:
        if n % 2 == 0:
            n = n // 2
        else:
            n = 3 * n + 1
        seq.append(n)
    return seq

The function stores the starting number in a list, then loops: divide by 2 if the current value is even, multiply and add 1 if it is odd, and append the result.

Running the sequence for a single number

The wrapper below asks for input, rejects anything that is not a positive integer, and prints the resulting sequence.

def main():
    try:
        number = int(input("Enter a positive integer: "))
        if number <= 0:
            print("Please enter a positive integer.")
            return
        seq = collatz(number)
        print("Collatz sequence:", seq)
    except ValueError:
        print("Invalid input. Please enter a valid integer.")

if __name__ == "__main__":
    main()

Verifying the conjecture for a range of numbers

To check whether every number in a range reaches 1, run the Collatz function for each value and print the result.

def test_collatz_conjecture(start, end):
    for i in range(start, end + 1):
        seq = collatz(i)
        print(f"The collatz sequence of {i} is {seq}")

test_collatz_conjecture(1, 15)

The output for 1 through 15 shows every sequence converging to 1. The longest in this range is 9, which takes 19 steps to reach 1.

What the sequence reveals

Even small starting numbers can produce long sequences with surprising peaks. The number 27, for instance, climbs to 9232 before it eventually falls back to 1. The sequence does not follow a predictable trajectory, which is why a general proof has remained out of reach.

FAQ

Common questions about the Collatz conjecture.

Question Answer
What happens if the conjecture is false? A counterexample would reach a cycle other than 4, 2, 1 or diverge toward infinity. Neither has been observed.
Why is it called the hailstone sequence? The numbers rise and fall repeatedly before reaching 1, resembling hailstones in a storm cloud.
Is there a formula for the number of steps? No. The total stopping time varies unpredictably with the starting number.
Who named it? Lothar Collatz introduced the conjecture in 1937.
Share.
Leave A Reply