Lesson 17 and 18 Notes

Secton 17

  • Unsolvable problem: There is no code that can be written that will give an answer.
  • Undecidable problem: There is no code that can find the answer, but there is an answer, just too complex.
  • undecidability example is that there is no way to prove the collatz conjecture as true for all real numbers or if it is false.
  • Hailstone numbers is the list of numbers returned by running the Coltaz code.
  • Algorithm Efficiency measures how many steps it takes to complete an algorithm where less steps means more efficient.
  • Algorithms can make life easier

Hacks

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='')
    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)
 
i = int(input('Enter i: '))
print('Sequence: ', end='')
collatz(i)
Please enter a number: 32 16 8 4 2 1
Number of iterations: 5
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/nathancapule/vscode/CSPFastpages/CSPFastpages/_notebooks/2022-12-14-Lesson17/18.ipynb Cell 2 in <cell line: 29>()
     <a href='vscode-notebook-cell:/Users/nathancapule/vscode/CSPFastpages/CSPFastpages/_notebooks/2022-12-14-Lesson17/18.ipynb#W2sZmlsZQ%3D%3D?line=25'>26</a> print('')
     <a href='vscode-notebook-cell:/Users/nathancapule/vscode/CSPFastpages/CSPFastpages/_notebooks/2022-12-14-Lesson17/18.ipynb#W2sZmlsZQ%3D%3D?line=26'>27</a> print('Number of iterations:', len(l) - 1)
---> <a href='vscode-notebook-cell:/Users/nathancapule/vscode/CSPFastpages/CSPFastpages/_notebooks/2022-12-14-Lesson17/18.ipynb#W2sZmlsZQ%3D%3D?line=28'>29</a> i = int(input('Enter i: '))
     <a href='vscode-notebook-cell:/Users/nathancapule/vscode/CSPFastpages/CSPFastpages/_notebooks/2022-12-14-Lesson17/18.ipynb#W2sZmlsZQ%3D%3D?line=29'>30</a> print('Sequence: ', end='')
     <a href='vscode-notebook-cell:/Users/nathancapule/vscode/CSPFastpages/CSPFastpages/_notebooks/2022-12-14-Lesson17/18.ipynb#W2sZmlsZQ%3D%3D?line=30'>31</a> collatz(i)

ValueError: invalid literal for int() with base 10: ''