MCS-211 · Block 1 Unit 2

Some Pre-requisites and Asymptotic Bounds

Section 1 of 8

Block 1 · Unit 2Some Pre-requisites and Asymptotic Bounds7 min · Section 1 of 8

2.1Sums, Products and Functions

Covers IGNOU MCS-211 §2.2.1, 2.2.2

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\sum_{i=1}^{4} i^2 = 1 + 4 + 9 + 16 = 30.

Formulas to remember

SumClosed formValue for n=5n = 5Growth
∑i=1ni=1+2+⋯+n\sum_{i=1}^{n} i = 1 + 2 + \dots + nn(n+1)2\dfrac{n(n+1)}{2}15Θ(n2)\Theta(n^2)
∑i=1ni2\sum_{i=1}^{n} i^2n(n+1)(2n+1)6\dfrac{n(n+1)(2n+1)}{6}55Θ(n3)\Theta(n^3)
∑i=1ni3\sum_{i=1}^{n} i^3(n(n+1)2)2=n2(n+1)24\left(\dfrac{n(n+1)}{2}\right)^2 = \dfrac{n^2(n+1)^2}{4}225Θ(n4)\Theta(n^4)
∑i=0n2i=1+2+4+⋯+2n\sum_{i=0}^{n} 2^i = 1 + 2 + 4 + \dots + 2^n2n+1−12^{n+1} - 163Θ(2n)\Theta(2^n)

The first one appears in every “triangular” double loop (the inner loop runs 1,2,…,n1, 2, \dots, 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 = True
for 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=1
n= 5: sum i=15, sum i^2=55, sum i^3=225, sum 2^i=63, product=120
n=10: sum i=55, sum i^2=385, sum i^3=3025, sum 2^i=2047, product=3628800
all formulas agree for n = 1..10: True

A program can check a formula for some values of nn, but only a proof (for example by induction, later in this unit) shows it is true for every nn.

Functions

Example: f:R→Rf: \mathbb{R} \to \mathbb{R}, f(x)=x3f(x) = x^3 maps 2 to 8. Running times are functions from input sizes {1,2,3,… }\{1, 2, 3, \dots\} to numbers of steps.

Floor and ceiling

Picture the number line: floor moves left to the nearest integer, ceiling moves right. If xx is already an integer, both leave it unchanged. For every real xx: x−1<⌊x⌋≤x≤⌈x⌉<x+1.x - 1 < \lfloor x \rfloor \le x \le \lceil x \rceil < x + 1 .

Python · floor_ceil.py
import math

for 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}")
Output
x =   3.5: floor =   3, ceil =   4, x-1 < floor <= x <= ceil < x+1: True
x =  -3.5: floor =  -4, ceil =  -3, x-1 < floor <= x <= ceil < x+1: True
x =     8: floor =   8, ceil =   8, x-1 < floor <= x <= ceil < x+1: True
x =  2.01: floor =   2, ceil =   3, x-1 < floor <= x <= ceil < x+1: True
x =  -0.2: floor =  -1, ceil =   0, x-1 < floor <= x <= ceil < x+1: True

In algorithms, floor and ceiling appear when sizes are split: an array of nn elements splits into halves of sizes ⌈n/2⌉\lceil n/2 \rceil and ⌊n/2⌋\lfloor n/2 \rfloor, and a loop that halves nn runs ⌊log⁡2n⌋\lfloor \log_2 n \rfloor times.

Key takeaways

  • ∑i=1ni=n(n+1)2\sum_{i=1}^{n} i = \frac{n(n+1)}{2}, ∑i2=n(n+1)(2n+1)6\sum i^2 = \frac{n(n+1)(2n+1)}{6}, ∑i3=(n(n+1)2)2\sum i^3 = \left(\frac{n(n+1)}{2}\right)^2, ∑i=0n2i=2n+1−1\sum_{i=0}^{n} 2^i = 2^{n+1} - 1.
  • ∏i=1ni=n!\prod_{i=1}^{n} i = n!.
  • A function assigns exactly one output to each input; running times are functions of nn.
  • Floor rounds down (towards −∞-\infty), ceiling rounds up; ⌈−3.5⌉=−3\lceil -3.5 \rceil = -3.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is ⌈−3.5⌉\lceil -3.5 \rceil?

Show answer

Answer: B. −3-3

The least integer that is ≥ −3.5 is −3. The value −2 is also ≥ −3.5 but it is not the least.

Question 2 · multiple choice

What is ∑i=120i\sum_{i=1}^{20} i?

Show answer

Answer: C. 210

n(n+1)/2 = 20 × 21 / 2 = 210. 420 forgets to divide by 2.

Question 3 · multiple choice

What is 1+2+4+⋯+291 + 2 + 4 + \dots + 2^9?

Show answer

Answer: D. 1023

Σ from i = 0 to 9 of 2ⁱ = 2¹⁰ − 1 = 1023. 1024 forgets the ”− 1”.

Question 4 · multiple choice

Which value is ⌊log⁡2100⌋\lfloor \log_2 100 \rfloor?

Show answer

Answer: A. 6

2⁶ = 64 ≤ 100 < 128 = 2⁷, so log₂100 ≈ 6.64 and its floor is 6. 7 would be the ceiling.

Question 5 · multiple choice

Which of these is NOT a function from {1,2,3}\{1, 2, 3\} to {a,b}\{a, b\}?

Show answer

Answer: C. 1→a, 1→b, 2→a, 3→b

Element 1 is given two outputs, so the rule does not assign exactly one value to each input. Sending every element to the same value is allowed.

Question 6 · short answer

Use the formula to find ∑i=16i2\sum_{i=1}^{6} i^2 and check it by direct addition.

Show worked answer

Formula: n(n+1)(2n+1)/6 = 6 × 7 × 13 / 6 = 91. Direct: 1 + 4 + 9 + 16 + 25 + 36 = 91. Both give 91.

↑ ↓ move↵ openTab switch type