Hailstone numbers

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)
Sequence: 2 1

Number of interations

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)
Please enter a number: 
Number of iterations: 1

Combining hailstone numbers and number of iterations

NumberIterations = 

Hacks 2

  • write one efficient algorithm and one inefficient algorithm.

  • idea: an algorithm that will have a user input the info on how the data provided