Question: Complete the following function in python based on the customers request. Function: def collatzLength(seed): #Add your code here Customer Request: Background The Collatz conjecture is

Complete the following function in python based on the customers request.

Function:

def collatzLength(seed): #Add your code here

Customer Request:

Background

The Collatz conjecture is an unsolved conjecture in mathematics named after Lothar Collatz, who first proposed it in 1937. The conjecture is also known as the 3n + 1 conjecture (because of it's formula) or the hailstone sequence (because of the way that the numbers bounce up and down like hail until they finally converge to 1).

The sequence works as follows:

Take any integer n.

If n is even, divide it by 2 to get n / 2.

If n is odd multiply it by 3 and add 1 to obtain 3n + 1.

Repeat the process until the number reaches 1, then quit

The conjecture is that no matter what number you start with, you will always eventually reach 1. This has been demonstrated true for integers of approximately 10**16 in size but has never formally been proven to be true.

For example, consider starting with a value of 3

Old Value This is: So the new value is:
3 odd 3*3 + 1 = 10
10 even 10/2 = 5
5 odd 3*5 + 1 = 16
16 even 16/2 = 8
8 even 8/2 = 4
4 even 4/2 = 2
2 even 2/2 = 1
1 THE END

This has a length of 7. We must perform 7 operations before we finally reach 1.

The assignment:

In your file locate the stub of the function called collatzLength(). This function should:

take in one parameter assumed to be a positive integer

validate that it is a positive integer:

if not than you should return None

if it is a non-negative integer it should calculate the length of the sequence needed to reach a value of 1

It should return this value.

For example:

collatzLength(0) will return None

collatzLength(3) returns 7

collatzLength(8) returns 3

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!