BIGO-P5

Solve the recurrence

Practice 5 of 5Cost: Big-O and friends9 tests

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):

  • k is 1 if there is a log factor, and 0 if not.
  • p is the power of n. When it is logb a (the power b must be raised to, to give a), round it to 3 decimal places with round(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

  • a and b are at most 1,000, and d is 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

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

9 tests, not run yet
Test results
#TestResultReads · time
1Merge sort: 2, 2, 1Not run
2Binary search: 1, 2, 0Not run
3Leaves decide: 4, 2, 1Not run
4Karatsuba: 3, 2, 1Not run
5Top level decides: 2, 2, 2Not run
6Top level decides: 1, 2, 1Not run
7Base 3: 9, 3, 2Not run
8Base 5: 125, 5, 3Not run
9Odd one: 7, 2, 2Not run