Question: The Collatz sequence is generated by applying these 2 operations on the previous number. The rules are: When the current number is even, the next

The Collatz sequence is generated by applying these 2 operations on the previous number. The rules are:
When the current number is even, the next term is half of the current term.
When the current number is odd, the next term is 3n+1(three times the current number plus 1).
For example: 5 generates the sequence ->5,16,8,4,2,1
For this assignment, you will be writing a program that generates the Collatz sequence for a given positive integer.
First, prompt the user for a positive number greater than 1. You can assume the user will always enter integers, but not necessarily greater than 1. You will need to validate their input before you continue (i.e. entering -10 or 0 should cause your program to tell the user that their input is invalid). You should re-prompt the user to enter a value if they supply bad data. Hint: use a "while" loop to keep the user "trapped" until they supply you with "good" data.
Next, your user should continually generate the next term in Collatz sequence until you reach the number 1. The program should display whether the given number is even or odd, whether it increased from the previous number, and is it a factor of the starting number.
At the end, your program should report:
The number of steps it took to reach the number 1.
The number of even numbers in the overall sequence (including the starting number and 1).
The number of odd numbers in the overall sequence (including the starting number and 1).
The number of increases in the overall sequence (including the starting number and 1).
The number of factors of the starting number found in the overall sequence (EXCLUDING the starting number and 1).
The average of the overall sequence (including the starting number and 1).
PLEASE DO NOT USE FOR LOOP AND def return. ONLY USE WHILE loops
 The Collatz sequence is generated by applying these 2 operations on

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!