Running times are usually totals: a loop body runs once for each value of a counter, and the total work is a sum. This page gives the notation for sums and products, the formulas you will use most, and two functions — floor and ceiling — that appear whenever we halve or divide sizes.
Summation notation
For example, ∑i=14i2=1+4+9+16=30.
Formulas to remember
Sum
Closed form
Value for n=5
Growth
∑i=1ni=1+2+⋯+n
2n(n+1)
15
Θ(n2)
∑i=1ni2
6n(n+1)(2n+1)
55
Θ(n3)
∑i=1ni3
(2n(n+1))2=4n2(n+1)2
225
Θ(n4)
∑i=0n2i=1+2+4+⋯+2n
2n+1−1
63
Θ(2n)
The first one appears in every “triangular” double loop (the inner loop runs 1,2,…,n times). The last one appears when work doubles at every level, as in some recursion trees.
Product notation
Python · sums_check.py
import math# Check the closed-form formulas against direct addition for n = 1..10.ok = Truefor n in range(1, 11): s1 = sum(range(1, n + 1)) s2 = sum(i * i for i in range(1, n + 1)) s3 = sum(i ** 3 for i in range(1, n + 1)) g = sum(2 ** i for i in range(0, n + 1)) p = math.prod(range(1, n + 1)) ok &= s1 == n * (n + 1) // 2 ok &= s2 == n * (n + 1) * (2 * n + 1) // 6 ok &= s3 == (n * (n + 1) // 2) ** 2 ok &= g == 2 ** (n + 1) - 1 ok &= p == math.factorial(n) if n in (1, 5, 10): print(f"n={n:2}: sum i={s1}, sum i^2={s2}, sum i^3={s3}, sum 2^i={g}, product={p}")print("all formulas agree for n = 1..10:", ok)
Output
n= 1: sum i=1, sum i^2=1, sum i^3=1, sum 2^i=3, product=1n= 5: sum i=15, sum i^2=55, sum i^3=225, sum 2^i=63, product=120n=10: sum i=55, sum i^2=385, sum i^3=3025, sum 2^i=2047, product=3628800all formulas agree for n = 1..10: True
A program can check a formula for some values of n, but only a proof (for example by induction, later in this unit) shows it is true for everyn.
Functions
Example: f:R→R, f(x)=x3 maps 2 to 8. Running times are functions from input sizes {1,2,3,…} to numbers of steps.
Floor and ceiling
Picture the number line: floor moves left to the nearest integer, ceiling moves right. If x is already an integer, both leave it unchanged. For every real x:
x−1<⌊x⌋≤x≤⌈x⌉<x+1.
Python · floor_ceil.py
import mathfor x in [3.5, -3.5, 8, 2.01, -0.2]: f, c = math.floor(x), math.ceil(x) holds = (x - 1 < f <= x <= c < x + 1) print(f"x = {x:5}: floor = {f:3}, ceil = {c:3}, x-1 < floor <= x <= ceil < x+1: {holds}")
In algorithms, floor and ceiling appear when sizes are split: an array of n elements splits into halves of sizes ⌈n/2⌉ and ⌊n/2⌋, and a loop that halves n runs ⌊log2n⌋ times.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-211/some-pre-requisites-and-asymptotic-bounds/sums-products-and-functions/ to read this page.