The Collatz Conjeture Challenge

The Collatz Conjeture Challenge

Python math project for practice

Write a Python script that asks the user to enter a number, apply the Collatz formula and display the sequence along with number of iterations. Moreover, the number input must be lower than 30, otherwise print a message of error and ask the user to try it again. This is a Python math project for practice “while” loops, problem solving skills and condition checking.

The Collatz sequence, also known as the “3n+1” sequence is a sequence of numbers defined by the following rules:

  1. Start with any positive integer n.
  2. If n is even, the next number in the sequence is n/2.
  3. If n is odd, the next number in the sequence is 3n + 1.
  4. Repeat the process with the new number, and continue iterating using the same rules.

The sequence is named after the German mathematician Lothar Collatz, who first introduced it in 1937. It is also known as the hailstone sequence due to the way the values “fall” and “rise” unpredictably.

For example, the Collatz sequence starting with the number 6:

n = 6:

6 is even, so the next number is 6/2 = 3.

3 is odd, so the next number is 33 + 1 = 10.

10 is even, so the next number is 10/2 = 5.

5 is odd, so the next number is 35 + 1 = 16.

16 is even, so the next number is 16/2 = 8.

8 is even, so the next number is 8/2 = 4.

4 is even, so the next number is 4/2 = 2.

2 is even, so the next number is 2/2 = 1.

The Collatz conjecture, an unsolved mathematical problem, posits that no matter what value of n is chosen, the sequence will always reach 1. While this conjecture remains unproven, the Collatz sequence is a fascinating area of study and has garnered significant interest in the field of mathematics.

Steps

  1. Define the function "collatz_sequence" that takes a number as input and returns the Collatz sequence along with the number of iterations.
  2. Use a “while” loop to repeatedly apply the Collatz conjecture formula until the number reaches 1.
  3. Inside the loop:
    • Check if the number is even or odd.
    • Apply the appropriate transformation (divide by 2 for even, multiply by 3 and add 1 for odd).
    • Keep track of the number of iterations
  4. If the input is not within the specified range, show an error message and ask the user to try again. Once a valid input is received, the loop breaks.
  5. Call the "collatz_sequence" function with the user’s input, and then display the resulting Collatz sequence along with the number of iterations.

Expected Output

Enter a number (lower than 30): 56
Error: Please enter a number lower than 30. Try again.
Enter a number (lower than 30): 5
The Collatz sequence for 5 is: [5, 16, 8, 4, 2, 1]
Number of iterations: 5

Check here more real Python practice projects for intermediate and advanced students.

Source Code

def collatz_sequence(n):
    sequence = [n]
    iterations = 0

    while n != 1:
        if n % 2 == 0:
            n = n // 2
        else:
            n = 3 * n + 1
        sequence.append(n)
        iterations += 1

    return sequence, iterations

while True:
    try:
        num = int(input("Enter a number (lower than 30): "))
        if num < 1 or num >= 30:
            print("Error: Please enter a number lower than 30. Try again.")
            continue
        else:
            break
    except ValueError:
        print("Error: Invalid input. Please enter a valid number.")

result, num_iterations = collatz_sequence(num)
print(f"The Collatz sequence for {num} is: {result}")
print(f"Number of iterations: {num_iterations}")

We will be happy to hear your thoughts

Leave a reply

Python and Excel Projects for practice
Register New Account
Shopping cart