In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of November 2nd
(this exercise will still be available for submission after that deadline, but without counting towards your grade)
[to understand the context of this problem, you should read the class #04 exercise sheet]


In this problem you should submit a function as described. Inside the function do not print anything that was not asked!

[IP034] Collatz Sequence

The Collatz sequence, also known as the \( 3n + 1 \) sequence, is a mathematical sequence defined for positive integers. The sequence starts with any positive integer n and follows these rules:

Despite its simplicity, the Collatz conjecture, which posits that this sequence will always eventually reach 1 for any positive integer n, remains unproven. This intriguing problem invites exploration of the patterns and behaviors of numbers as they traverse through the sequence.

The Problem

Write a function collatz(n) that takes a positive integer n and generates the Collatz sequence starting from n. The function should return a string representation of the entire sequence following the format depicted on the example output.

Constraints

The following limits are guaranteed in all the test cases that will be given to your program:

1 ≤ n ≤ 1000       The starting integer for the Collatz sequence

Example Function Calls Example Output
print(collatz(1))
print(collatz(3))
print(collatz(12))
1
3, 10, 5, 16, 8, 4, 2, 1
12, 6, 3, 10, 5, 16, 8, 4, 2, 1

Introduction to Programming (CC1024)
DCC/FCUP - University of Porto