Question
In this lab you will write a function named collatz() that has one parameter named number. If the number is even, then collatz() should print
In this lab you will write a function named collatz() that has one parameter named number. If the number is even, then collatz() should print number // 2 and return this value. If the number is odd, then collatz() should print and return 3 * number + 1.
Then write a program that lets the user type in an integer and that keeps calling collatz() on that number until the function returns the value 1.
Amazingly enough, this sequence actually works for any positive integersooner or later, using this sequence, youll arrive at 1! Even mathematicians arent sure why. Your program is exploring whats called the Collatz sequence, sometimes called the simplest impossible math problem.
Remember to convert the return value from input() to an integer with the int() function; otherwise, it will be a string value.
The program must:
have at least 1 function
have at least 1 decision structure
Hint: An integer number is even if number % 2 == 0, and its odd if number % 2 == 1.
Note: Input validation is not required, so assume the user will enter answers that make sense. What will your program do given unexpected values?
The output of this program could look something like this:
Welcome to the Collatz Sequence calculator.
Please enter a positive number: 3 10 5 16 8 4 2 1
|
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started