Answers to hailstone problems:
#-----------------------
def hailstoneNext(n):
if n % 2 == 0:
return n // 2
else:
return 3*n+1
#-----------------------
def printHailstone(n):
while n > 1: # Last "True" iteration has n==2
print(n)
n = hailstoneNext(n)
print(n) # Extra print so we print when n==1
#-----------------------
def hailstoneMax(n):
biggest = n # start with n as our biggest, will replace if we find a bigger
while n > 1:
if n > biggest:
biggest = n
n = hailstoneNext(n)
#Don't need to recheck biggest when n = 1
return biggest
# We can also use the max() function, (returns the
# larger of two numbers) to shorten the code
def hailstoneMax2(n):
biggest = n
while n > 1:
biggest = max(biggest, n)
n = hailstoneNext(n)
return biggest
#-----------------------
def hailstoneLen(n):
seqLen = 0
while n > 1:
seqLen += 1
n = hailstoneNext(n)
return seqLen + 1 #failed to count the n=1, add 1 to compensate