Collatz: Famous unsolved problem in math, and asks whether repeating two simple arithmetic operations will eventually transform every positive integer into 1
Hailstone Numbers: sequence of integers generated by Collatz
Undecidable Problems: gives "yes" or "no" answer, but no algorithm can correctly answer inputs
Unsolvable Problems: No algorithm can ever be written to find the solution
Algorithm Efficiency: Measures number of steps needed to solve a problem
Heuristic Approach: Shortest and most efficient "route"
Take the two codes above and combine them so one input 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.
def collatz(i):
while i != 1:
if i % 2 > 0:
i =((3 * i) + 1)
list_.append(int(i))
else:
i = (i / 2)
list_.append(int(i))
return list_
print('Please enter a number: ', end='' + "\n")
while True:
try:
i = int(input())
list_ = [i]
break
except ValueError:
print('Invaid selection, try again: ', end='')
l = collatz(i)
print('Sequence: ')
print(*l, sep=" ")
print('Number of iterations:', len(l) - 1)
- Write 2 algorithms: One is efficent and one is innefficent, then explain why one is efficent while the other isn't.
- Explain why one algorithm is more efficient than another using mathematical and/or formal reasoning.
- use variables, if statements, and loops to program your algorithm and upload to jupyter notebooks/ fastpages.
tasks = ["a", "b", "c"]
# the tasks / variables
def complete_tasks(tasks):
for task in tasks:
# completing each task
if task == "a":
print("a")
elif task == "b":
print("b")
elif task == "c":
print("c")
# calling the function
complete_tasks(tasks)
tasks = ["a", "b", "c"]
tasks.sort ()
print (tasks)
tasks = ["shower", "brush teeth", "skincare", "sleep"]
def complete_tasks(tasks):
for task in tasks:
if task == "shower":
print("Take a shower!")
elif task == "brush teeth":
print("Go brush your teeth!")
elif task == "skincare":
print("Do your skincare!")
elif task == "sleep":
print("Go to bed!")
complete_tasks(tasks)