Lab3

Lab3

The Problem
There is an interesting little algorithm called the hailstone algorithm (also called the Collatz
sequence). It is fairly straightforward. For any positive integer (input):
1. If the number is even, divide it in half
2. If the number is odd, multiply it by 3 then add 1
3. If the number reaches 1, stop; otherwise go to step 1

CODE:

inputStr = raw_input(“Input a number: “)
number = int(inputStr)

if number < 0:
print (“You entered a negative number.”)
else:
print (‘Sequence for’), number, (‘=>’), number,
while number > 1:
if number%2==0:
number = number / 2
else:
number = number*3+1
print number,
print
print
print (‘End’)