Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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.
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
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