Unit 3.17-3.18 Group Lesson Hacks
- Hailstone numbers
- Number of interations
- Combining hailstone numbers and number of iterations
- Hacks 2
def collatz(i):
while i > 1:
print(i, end=' ')
if (i % 2):
# i is odd
i = 3*i + 1
else:
# i is even
i = i//2
print(1, end='')
i = int(input('Enter i: '))
print('Sequence: ', end='')
collatz(i)
def collatz(i):
while i != 1:
if i % 2 > 0:
i =((3 * i) + 1)
list_.append(i)
else:
i = (i / 2)
list_.append(i)
return list_
print('Please enter a number: ', end='')
while True:
try:
i = int(input())
list_ = [i]
break
except ValueError:
print('Invaid selection, try again: ', end='')
l = collatz(i)
print('')
print('Number of iterations:', len(l) - 1)
NumberIterations =