Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Step: 3

blur-text-image

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

Database Marketing The Ultimate Marketing Tool

Authors: Edward L. Nash

1st Edition

0070460639, 978-0070460638

More Books

Students also viewed these Databases questions