Question
In python: Not sure how to do a Collatz function. Remember the Collatz function? If x is odd, then collatz(x) = 3 x + 1
In python:
Not sure how to do a Collatz function.
Remember the Collatz function? If x is odd, then collatz(x) = 3 x + 1 but if x is even then collatz(x) = x/2. (remember to use // to divide by 2) Start by writing a Collatz() function that returns the value for a given input. Test this function for all numbers from 1 to 10. Next write a Collatz2() method. This method should take in a single number and repeatedly use the Collatz() function printing out the result while the number is not one. For example: If I say Collatz2(5), Then the output should be : 5 16 8 4 2 1
If I say Collatz2(12), Then the output should be : 12 6 3 10 5 16 8 4 2 1
Once you know it is working, run your method on the number n = 27 and see how long it takes to get to 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