Lesson 3.17 and 3.18 - Algorithm Efficiency and Undecidable Problems
Notes, questions, and hacks
Notes
- Collatz: Asks whether repeating two arithmetic operations will eventually transform every positive integer into 1
- the Collatz conjecture is one of the most famous unsolved problems in math
- Hailstone numbers: Sequence of integers generated by Collatz conjecture
- Iteration: Action or process of iterating or repeating
- Procedure in which repitition of a sequence of operations yields results successively closer to a desired result
- Undecidable problems: Problem that hsould give a yes or no answer, but no algorithm exists that can answer correctly on all inputs yet
- Unsolvable problems: Problem which no algorithm can be written to find the solution
- Algorithm efficiency: aspect of algorithmic programming that measures amount of steps needed to solve a problem
i = int(input("Enter a number"))
print("Number:", i)
def collatz(i):
while i > 1:
print(i, end=' ')
if (i % 2):
i = 3*i + 1
list_.append(i)
else:
i = i//2
list_.append(i)
print(1, end='')
list_ = [i]
l = list(i)
print('Number of iterations:', len(l) - 1)
print('Sequence: ')
collatz(i)
num1 = 10
num2 = 20
num3 = 30
print(num1 + num2 + num3)
nums = [10, 20, 30]
print(sum(nums))
The first one is an inefficient way to add the sums of variables, because it individually assigns and adds each variable. The second one is more efficient because it uses a list to assign values, and finds the sum with a sum() function.
tasks = ["wake up", "go to school", "go home", "do homework", "go to sleep"]
def dailyRoutine(tasks):
for task in tasks:
if task == "wake up":
print("Good morning!")
elif task == "go to school":
print("I hate school.")
elif task == "go home":
print("Let's take a nap.")
elif task == "do homework":
print("Do your homework... stay on task!")
else:
print("Time to sleep.")
dailyRoutine(tasks)