Exercise 9 of 30 · beginner · loops · linear · constants

Stepping by three

The code

for i in range(0, n, 3):
    work(i)

Does skipping elements make it sub-linear?

Count: work calls.

Measured counts

0500100020004000ncount
Measured
Table
ncount
83
166
3211
6422
12843
25686
512171
1024342
2048683
40961,366

Try to name the class from the shape before opening the answer.

Show the answer and derivation
Time
Θ(n)
Extra space
O(1)
Exact count
⌈n / 3⌉
  1. i takes the values 0, 3, 6, …: ⌈n/3⌉ of them.
  2. n/3 is a constant fraction of n: still Θ(n).

Common mistake: Thinking any skipping is logarithmic. Only a constant factor of the work is skipped.

Check: measured count ÷ (n) settles near 0.333 for large n.