Hacks/Assignment Take the two codes above and combine them so one imput gives the output that contains both the hailstone numbers and the number of iterations it takes i = 1. The more efficient the code, the higher your grade will be. (Algorithm Efficency) (.25)
num = []
count = 0
def collatz(i):
global count
global num
while i > 1:
if (i % 2):
# i is odd
i = 3*i + 1
count +=1
num.append(i)
else:
# i is even
i = i//2
count +=1
num.append(i)
else:
print(count)
print(num)
i = int(input('Enter i: '))
collatz(i)
Hacks/assignment Write 2 algorithms: One is efficent and one is innefficent, then explain why one is efficent while the other isn't. (.25) Explain why one algorithm is more efficient than another using mathematical and/or formal reasoning. (.25) use variables, if statements, and loops to program your algorithm and upload to jupyter notebooks/ fastpages. (.25)
num = [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9]
sum = num[0] + num[1] + num[2] + num[3] + num[4] + num[5] + num[6] + num[7] + num[8] + num[9] + num[10] + num[11] + num[12] + num[13] + num[14] + num[15] + num[16] + num[17] + num[18] +num[0] + num[1] + num[2] + num[3] + num[4] + num[5] + num[6] + num[7] + num[8] + num[9] + num[10] + num[11] + num[12] + num[13] + num[14] + num[15] + num[16] + num[17] + num[18]
sum2 = sum + sum +sum + sum + sum + sum + sum + sum + sum + sum + sum +sum + sum + sum + sum + sum + sum + sum
print(sum2)
num = [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9]
print(36*sum(num))
The bottom one is more efficient because it uses built in functions to add the numbers instead of having to add each one individually which takes a long time. Then adding the sum everytime is even more inneficient
num = [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9]
if True:
sum = 0
for i in num:
sum += num[i-1]
print(sum*36)