Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(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.

image text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions

Question

1. Which position would you take?

Answered: 1 week ago

Question

How could assessment be used in an employee development program?

Answered: 1 week ago