Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This question is runned in Wolfram Mathematica Software Question 2 - Collatz Con

ID: 3739448 • Letter: T

Question

This question is runned in Wolfram Mathematica Software

Question 2 - Collatz Conjecture Quit Consider the following process to generate a sequence m?, m2, m3,.... Start with any positive integer m If m1 is even, divide it by 2, obtaining m2 -mm/2 If m1 is odd, multiply by 3 and add one, obtaining m2 3m1+1. - Repeat (with m2 in place of m? etc.) For example, starting with mi 10 we obtain the sequence 10, 5, 16, 8,4,2,1,4,2,1,.... Notice that starting with mi -1, the process produces the sequence 1, 4, 2, 1,4, 2, etc. It is a famous (and unproven) conjecture, called the Collatz conjecture (or 3n+1 conjecture), that regardless of m, the process will eventually produce the number 1 (and hence enter the cycle i,4,2, ). we will write a function to investigate this phenomenon. 1. Write, using Module, a function Collatz[m] that takes as input a positive integer m and outputs the number of times that the procedure must be repeated until we obtain 1. For example, starting with m = 10 we must repeat 6 times before obtaining 1. Ifm is not a positive integer, the function should print the message "Positive integers only!". 2. Using your function, verify that Collatz[10000]-29, Collatz[27]-111, and that Collatz[-2] and Collatz[Pi] generate the error message. Then compute Collatz[999] and Collatz[2222 3. Find the integer n in the range [1, 750] such that Collatz[n] is maximized. Report both n and Collatz[n

Explanation / Answer

1) Please check the below code in python for collatz conjecture.
In below code i am asking input from user for a positive integer.
Once i get the integer i am checking wether the number is positive or negative,
if it is negative i am throwing an error message otherwise i am running while loop to check if number is even or odd.
If the number is even i am diving it by 2 else i am multiplying the number by 3 and adding 1. I am repeating
same step until i get 1, meanwhile i am also increasing the count. Once my while loop gets over i am printing the count.

Please find the code below:

def collatz(number):
count =0
if number <0:
print("Positive Integers only")
while number != 1:
if number % 2 == 0:
number = number // 2
count+=1

elif number % 2 == 1:
number = 3 * number + 1
count += 1

return count


input_number = input("Please provide positive integer: ")
print(collatz(int(input_number)))


2) I validated my code with different values shown below:

Please provide positive integer: 10000
29

Please provide positive integer: 27
111

Please provide positive integer: -2
Positive Integers only

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote