This code will create a program that will take integer number and if the number is even, it will divide by 2, if it is odd, it will multiply by 3 and add 1 until the number reach 1.
Hailstone code:
inputStr = input (‘Input a number: ‘)
number= int(inputStr)
if number<=0:
print (‘Invalid Number’)
else:
print (number)
while number!=1:
if number%2==0:
number=number//2
print (number)
else:
number=(number*3)+1
print (number)
print (‘End’)
Extra Hailstone code:
def hailstone(number):
count=1
while number!=1:
if number%2==0:
number=number//2
count=count+1
else:
number=(number*3)+1
count=count+1
return count
def count ():
result=1
counts=1
while counts<101:
total=hailstone(counts)
counts=counts+1
if total>result:
result=total
return result
print (‘The length of the longest hailstone is: ‘,count())