Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

More Books

Students also viewed these Databases questions