Question
(Using Python 3.7) Please help me understand why my code isn't working, I'm in an infinite loop. I think it's because of indentation but I
(Using Python 3.7) Please help me understand why my code isn't working, I'm in an infinite loop. I think it's because of indentation but I can't figure it out. My code is below the instructions.
print("This code takes input from user, computes the Collatz sequence, then states number of iterations to reach value 1") # First, take input from user and verify correct input n = int(input("Please input a positive integer: "))
if n
if type(n) == "int": print("Error. Please input a whole number") exit()
# Now with correct input confirmed, begin Collatz sequence
i = 0 while n != 1: if (n % 2) != 0: n = n / 2 print(n) i += 1 else: n = (3 * n) + 2 print(n) i += 1
print(i)
a) The Collatz Conjecture: The Collatz conjecture, also known as the 3n+1 conjecture (and other names), deals with the following operation to produce a sequence of numbers. Given a number, n, if n is even then the next number is n divided by 2. If n is odd, then the next number is 3n+1. The Collatz conjecture is that this sequence of numbers always eventually reaches 1. As simple as this seems, it is unproven (and considered extremely hard to prove) by mathematicians. As an example of a sequence, if you start with the number 6, then the terms of the sequence will be: 6, 3, 10, 5, 16, 8, 4, 2, 1. Write a program that takes in a positive integer from a user, and computes the Collatz sequence, printing out all the numbers in the sequence, followed by a line stating how many iterations it took to reach the value 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