Complexity and efficiency · 6 / 15 Exercise Medium

Growth table

Objective

Create a Python program that counts the steps of four kinds of algorithm for several input sizes and shows how each one grows: the basic idea of complexity.

Instructions

  • Read one line with several integers n (each between 1 and 200), separated by spaces.
  • For each n write three functions that count steps with a counter: one with a simple loop (n steps), one with two nested loops (n × n steps) and one that divides n by 2 while it is greater than 1 (how many times it can be halved).
  • Show one line per n with the format n=N: logarithmic a, linear b, n log n c, quadratic d. Notice how the quadratic term shoots up.
  • Here a is the number of halvings and c is n multiplied by that number.
Input
1 8 16
Output
n=1: logarithmic 0, linear 1, n log n 0, quadratic 1
n=8: logarithmic 3, linear 8, n log n 24, quadratic 64
n=16: logarithmic 4, linear 16, n log n 64, quadratic 256

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise