1. (7 points) Consider a sequence of integers defined as follows: The first term is any positive integer. • For each ter
Posted: Fri Jul 08, 2022 6:39 am
1. (7 points) Consider a sequence of integers defined as follows: The first term is any positive integer. • For each term n in the sequence, the next term is computed like this: If n is even, the next term is n/2. If n is odd, the next term is 3n + 1. • Stop once the sequence reaches 1. Here are a few examples of this sequence for different values of the first term: 8, 4, 2, 1 12, 6, 3, 10, 5, 16, 8, 4, 2, 1 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 Note that all three of these eventually do reach 1. In fact, it is believed (but not known) that the sequence will always reach 1, regardless of the first term chosen. This is known as the Collatz conjecture. Despite its apparent simplicity, it has so far cluded all attempts at a proof. Mathematician Jeffrey Lagarias at the University of Michigan has claimed that "this is an extraordinarily difficult problem, completely out of reach of present day mathematics." We might not be able to prove the Collatz conjecture here, but we can experiment computa- tionally with it! Write a program named collatz.py that allows the user to enter any positive integer. The program should then compute and list all the terms of the sequence until it reaches 1. At the end, show how many terms it took to get to 1 (including 1 itself). Include input validation to ensure that the user's input is positive. If it's not positive, show an error message and allow the user to re-enter the input as many times as necessary.