Question: Python Define a new function named collatz that will create a sequence of numbers that starts with a specified value.Initialize a list so it contains

Python

Define a new function named collatz that will create a sequence of numbers that starts with a specified value.Initialize a list so it contains only the value passed as a parameter. Then use a while loop to extend the list usingthe following process: Let n be the value currently at the end of the list. If n is even, extend the list with n , but if 2 n is odd, extend the list with 3 n + 1. An unproven conjecture from number theory is that eventually the number 1 will be appended to the list, which is when the process terminates.

Python Define a new function named collatz that will create a sequence

def collatz(data): # Fill in your code here for Part 3  return None # Change or replace this line 
if __name__ == "__main__": 
 print('Testing collatz() for 2: ' + str(collatz(2))) print('Testing collatz() for 5: ' + str(collatz(5))) print('Testing collatz() for 17: ' + str(collatz(17))) 

Examples: Function Call Return Value Collatz (2) 12, 11 Collatz (5) [5, 16, 8, 4, 2, 11 Collatz (17) [17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!