3.17 and 3.18 Hacks
Hacks
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)
print("Histone Numbers", list_)
This is very tedious and is a less efficient way to write code
import math
num1 = 4
num2 = 9
num3 = 16
num4 = 25
print(math.sqrt(num1))
print(math.sqrt(num2))
print(math.sqrt(num3))
print(math.sqrt(num4))
The one below is a more efficient program than the one above
import math
values = [4, 9, 16, 25, 36, 49]
squareroots = [math.sqrt(number) for number in values]
print("The Original Values:\n", values)
print("The Square Root Values:\n", squareroots)
- Explain why one algorithm is more efficient than another using mathematical and/or formal reasoning. (.25)
- The first code is inefficient because the programer has to right the same code over and over again, only with using different numbers, which gets messy and annoying. The second code, however, puts all the values in a list and takes the square root all at once. So that way, all you have to do is add numbers, not functions, which is way more convenient.
- use variables, if statements, and loops to program your algorithm and upload to jupyter notebooks/ fastpages. (.25)
- I used a list and variables for both of my codes.