Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PART 3) RETURN FUNCTIONS Remember the Collatz function? If x is odd, then () = 3 + 1 but if x is even then ()

PART 3) RETURN FUNCTIONS

Remember the Collatz function? If x is odd, then () = 3 + 1 but if x is even then () = /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)

If I say Collatz2(12)

Then the output should be :

5

16

8

4

2

1

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

Intelligent Image Databases Towards Advanced Image Retrieval

Authors: Yihong Gong

1st Edition

1461375037, 978-1461375036

More Books

Students also viewed these Databases questions