The problem
Write the master theorem from lesson 6 as a function. Given whole numbers a (at least 1), b (at least 2) and d (0 or more), the recurrence T(n) = a T(n/b) + nd is Θ(np (log n)k), where a power such as np means n raised to the power p. Return the pair (p, k):
kis 1 if there is a log factor, and 0 if not.pis the power ofn. When it is logb a (the power b must be raised to, to give a), round it to 3 decimal places withround(p, 3).
master(2, 2, 1) # (1, 1): n log n
master(4, 2, 1) # (2.0, 0): n²
master(3, 2, 1) # (1.585, 0)
master(2, 2, 2) # (2, 0): n²
Limits
aandbare at most 1,000, anddis at most 5.
Builds on BIGO-5 Recurrences and BIGO-6 The master theorem.
Continue on a laptop
Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.
Need a hint?0 of 3 opened
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
9 tests, not run yet
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | Merge sort: 2, 2, 1 | Not run | |
| 2 | Binary search: 1, 2, 0 | Not run | |
| 3 | Leaves decide: 4, 2, 1 | Not run | |
| 4 | Karatsuba: 3, 2, 1 | Not run | |
| 5 | Top level decides: 2, 2, 2 | Not run | |
| 6 | Top level decides: 1, 2, 1 | Not run | |
| 7 | Base 3: 9, 3, 2 | Not run | |
| 8 | Base 5: 125, 5, 3 | Not run | |
| 9 | Odd one: 7, 2, 2 | Not run |