Mathematical Tools: Logs, Recurrences, Summations

গাণিতিক হাতিয়ার — log, recurrence, summation

Read: ~35 min Beginner 7 practice problems Live code runner

1. Why a CSE Student Needs This Math

Algorithms are mathematical objects. To analyse them you need three small but powerful tools: logarithms, summations, and recurrences. We are not doing math for math's sake — every formula here returns later in trees, sorts, graphs, DP and contests.

Algorithm আসলে গণিতের একটি বস্তু। এর বিশ্লেষণে তিনটি ছোট কিন্তু শক্তিশালী হাতিয়ার দরকার: logarithm, summation, recurrence। এগুলো শুধু math-এর জন্য math নয় — পরে tree, sort, graph, DP, contest — সবখানেই এই সূত্রগুলোই ফিরে আসবে।
Key insight log n বার ভাগ করতে করতেই 109 → 30 হয়ে যায়।

অর্থাৎ ১০০ কোটি ডেটার মধ্যে binary search মাত্র ৩০ ধাপে উত্তর দিতে পারে। logarithm-এর শক্তি এখানেই।

2. Logarithm — The Inverse of Doubling

log2(n) answers: "How many times can I halve n before I reach 1?" Equivalently: "How many times must I double 1 to reach n?" That is the entire intuition you need.

nlog₂ nhalving steps
10—
212 → 1
1024101024 → 512 → … → 1
10⁶≈ 2020 halvings
10⁹≈ 3030 halvings
10¹⁸≈ 6060 halvings
log২(n) মানে: "১ পর্যন্ত পৌঁছাতে n-কে কতবার অর্ধেক করতে হবে?" — অথবা, "১ থেকে n-এ পৌঁছাতে কতবার দ্বিগুণ করতে হবে?" এই একটি ধারণা মাথায় থাকলেই binary search, balanced tree, segment tree — সব হজম হয়ে যাবে।
depth 0 depth 1 depth 2 depth 3 depth 30 … 2³⁰ ≈ 10⁹ leaves at depth 30 — only 30 levels to cover a billion items! Figure 3.1 — A binary tree of depth 30 already holds ~109 leaves.
Log identities you'll use weekly
  • log(ab) = log a + log b
  • log(aⁿ) = n · log a
  • logb(n) = log2(n) / log2(b) — base change
  • 2log₂ n = n

3. Arithmetic Series

The classic Gauss formula:

Σ from i=1 to n of i = n(n+1)/2   →   Θ(n²)

This appears every time a nested loop counts unordered pairs — (n choose 2) = n(n-1)/2 ≈ n²/2. Always Θ(n²), but the leading constant is ½ — useful when comparing two O(n²) algorithms in practice.

Gauss-এর সূত্রটি ICPC-তে দিনে কয়েকবার লাগে। nested loop যেখানে অর্ডার-ছাড়া pair গোনা হয় (যেমন i < j), সেটি n(n-1)/2 — Θ(n²)। প্রকৃত operation-সংখ্যা n²/2, পুরো n² নয় — তাই দুটি O(n²) কোডের মধ্যেও 2× পার্থক্য হতে পারে।

4. Geometric Series

1 + 2 + 4 + … + 2n = 2n+1 − 1
More generally: 1 + r + r² + … + rⁿ = (rn+1 − 1) / (r − 1)  (r ≠ 1)

This is why a recursion with doubling work per level still totals only Θ(2n) — the last level dominates. It's also why building all subsets of an n-element set is exactly 2n.

Geometric series-এ শেষ term-ই সবচেয়ে বড় হয়, এবং পুরো সিরিজ সেই শেষ term-এর প্রায় দ্বিগুণ। তাই recursion-এ প্রতি লেভেলে কাজ দ্বিগুণ হলেও মোট কাজ শেষ লেভেলের কাছাকাছি — অর্থাৎ Θ(2ⁿ)। n-element set-এর সব subset গুনলেও পাবেন 2ⁿ — এই কারণেই subset-জনিত brute force দ্রুতই অসাধ্য হয়ে পড়ে।

5. Telescoping Sums

A "telescoping" sum is one where consecutive terms cancel, leaving only the first and the last:

Σ from i=1 to n of (ai+1 − ai) = an+1 − a1

Example: Σ 1/(i(i+1)) = Σ (1/i − 1/(i+1)) = 1 − 1/(n+1) → 1 as n → ∞. This pattern shows up when analysing amortised costs (Fenwick tree updates, dynamic array doubling).

Telescoping sum — যেখানে পরপর term পরস্পরকে কাটে এবং কেবল প্রথম ও শেষ অংশ অবশিষ্ট থাকে। যেমন Σ 1/(i(i+1)) = 1 − 1/(n+1)। Fenwick tree বা dynamic array doubling-এর amortised খরচ বিশ্লেষণে এই কৌশল লাগে।

6. Harmonic Numbers — Σ 1/i

Hn = 1 + 1/2 + 1/3 + … + 1/n grows like ln(n) + γ, where γ ≈ 0.5772. For DSA we just remember Hn = Θ(log n).

This is why the Sieve of Eratosthenes is O(n log log n): the inner loop runs n/p times for each prime p, and Σ 1/p (over primes ≤ n) is log log n. Same harmonic flavour, different denominator.

Harmonic sum Hn = 1 + 1/2 + … + 1/n প্রায় ln(n)-এর সমান বাড়ে — অর্থাৎ Θ(log n)। এই কারণেই Sieve of Eratosthenes-এর জটিলতা O(n log log n) — সব prime p-এর জন্য 1/p যোগ করলে log log n পাওয়া যায়।
harmonic.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    for (int n : { 10, 100, 1000, 10000, 100000 }) {
        double H = 0;
        for (int i = 1; i <= n; i++) H += 1.0 / i;
        double ln_n = log((double)n);
        printf("n=%6d  H=%.5f  ln(n)=%.5f  diff=%.5f\n", n, H, ln_n, H - ln_n);
    }
    return 0;
}

7. Recurrences — A First Look

A recurrence relation expresses an algorithm's runtime in terms of its own runtime on smaller inputs. The three you'll meet most often:

RecurrenceSolves toWhere it appears
T(n) = T(n-1) + 1Θ(n)Linear recursion (factorial, sum)
T(n) = T(n/2) + 1Θ(log n)Binary search
T(n) = 2T(n/2) + nΘ(n log n)Merge sort
T(n) = T(n-1) + nΘ(n²)Insertion sort worst case
T(n) = 2T(n-1) + 1Θ(2ⁿ)Tower of Hanoi, naive subsets
Recurrence relation মানে — algorithm-এর runtime ছোট ইনপুটের নিজের runtime দিয়ে প্রকাশ। উপরের পাঁচটি pattern প্রায় সব সাধারণ algorithm কভার করে। পরবর্তী module-এ Master Theorem দিয়ে এদের সরাসরি বিশ্লেষণ শিখব।
verify_gauss.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // Verify Σ i = n(n+1)/2 for n = 1..1000
    int bad = 0;
    for (long long n = 1; n <= 1000; n++) {
        long long loopSum = 0;
        for (long long i = 1; i <= n; i++) loopSum += i;
        long long formula = n * (n + 1) / 2;
        if (loopSum != formula) bad++;
    }
    cout << (bad == 0 ? "All 1000 values match Σi = n(n+1)/2 ✔\n"
                       : "Mismatch found!\n");
    return 0;
}

8. Glossary (শব্দকোষ)

TermMeaningবাংলায়
LogarithmInverse of exponentiation; how many doublings reach n.সূচকীকরণের বিপরীত — n-এ পৌঁছাতে কতবার দ্বিগুণ করতে হবে।
Arithmetic seriesSum where each term differs by a constant.যেখানে প্রতিটি term পূর্ববর্তী থেকে একই পরিমাণে বেশি।
Geometric seriesSum where each term is a constant factor of the previous.যেখানে প্রতিটি term পূর্ববর্তী-এর একটি স্থির গুণিতক।
Harmonic numberHn = Σ 1/i — grows like ln n.Hn = Σ 1/i — ln n-এর হারে বাড়ে।
RecurrenceAn equation defining T(n) in terms of T of smaller values.T(n)-কে ছোট ইনপুটের T-এর মাধ্যমে প্রকাশ করা সমীকরণ।

9. Practice Problems

  1. Compute 1 + 2 + … + 10⁹ in O(1).
    10⁹ পর্যন্ত যোগফল O(1)-এ বের করুন।
    ✨ Show Answer (উত্তর দেখুন)
    big_sum.cpp
    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        long long n = 1000000000;
        cout << "Sum = " << n * (n + 1) / 2 << "\n";
        return 0;
    }
  2. Compute 1 + 2 + 4 + … + 230.
    1 + 2 + 4 + … + 2৩০ এর মান বের করুন।
    ✨ Show Answer (উত্তর দেখুন)

    Answer: 231 − 1 = 2,147,483,647. Use the geometric formula 2n+1−1.

    Geometric সূত্র অনুযায়ী 2৩১ − 1 = 2,147,483,647।

  3. Solve T(n) = T(n/2) + 1 by hand.
    T(n) = T(n/2) + 1 হাতে সমাধান করুন।
    ✨ Show Answer (উত্তর দেখুন)

    Answer: Unfold: T(n) = T(n/2)+1 = T(n/4)+2 = T(n/8)+3 = … = T(1)+log₂ n. So T(n) = Θ(log n) — exactly the cost of binary search.

    প্রতি ধাপে n অর্ধেক হয়, log₂ n বার অর্ধেক করলে n = 1 হয় — তাই T(n) = Θ(log n)।

  4. How many comparisons does the Sieve of Eratosthenes do for n = 10⁶? (give complexity, not exact count)
    n = 10⁶-এ Sieve of Eratosthenes প্রায় কত comparison করে? (complexity বলুন)
    ✨ Show Answer (উত্তর দেখুন)

    Θ(n log log n). The inner loop sums n/p over primes p ≤ n; that sum equals n · log log n. For n = 10⁶ this is roughly 3 × 10⁷ — well inside any time limit.

  5. Compute Σ from i=1 to 1000 of 1/i and compare with ln(1000).
    Σ 1/i (i = 1..1000) এবং ln(1000) তুলনা করুন।
    ✨ Show Answer (উত্তর দেখুন)
    harm.cpp
    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        double H = 0;
        for (int i = 1; i <= 1000; i++) H += 1.0 / i;
        printf("H_1000 = %.6f\nln(1000) = %.6f\nγ-approx = %.6f\n",
               H, log(1000.0), H - log(1000.0));
        return 0;
    }

    The difference converges to γ ≈ 0.5772.

  6. Express 1 + 3 + 5 + … + (2n-1) in closed form.
    1 + 3 + 5 + … + (2n-1) — এর সংক্ষিপ্ত রূপ লিখুন।
    ✨ Show Answer (উত্তর দেখুন)

    Answer: n². Sum of first n odd numbers is exactly n². Proof: 2·(1+2+…+n) − n = n(n+1) − n = n².

    প্রথম n-টি বিজোড় সংখ্যার যোগফল ঠিক n²। প্রমাণ: 2·(1+…+n) − n = n²।

  7. If T(n) = T(n-1) + n with T(1) = 1, find a closed form.
    T(n) = T(n-1) + n, T(1) = 1 — এর closed form?
    ✨ Show Answer (উত্তর দেখুন)

    T(n) = n(n+1)/2 = Θ(n²). Unfold: T(n) = n + (n-1) + (n-2) + … + 1 = Σ i. This is the work of insertion sort in the worst case.

    Unfold করলে T(n) = n + (n-1) + … + 1 = n(n+1)/2 → Θ(n²)। Insertion sort-এর worst case এই pattern।

Summary — Module 03

Logs measure halvings; arithmetic series give Θ(n²); geometric series give Θ(2ⁿ); harmonic sums grow like log n; recurrences let us turn iterative loops into closed-form complexity. These five tools are enough to analyse 90% of the algorithms you'll meet in this course.

log মাপে কতবার অর্ধেক করতে হবে; arithmetic series দেয় Θ(n²); geometric series Θ(2ⁿ); harmonic sum বাড়ে log n হারে; recurrence-এর সাহায্যে loop-কে closed-form complexity-তে রূপান্তর করা যায়। এই পাঁচটি হাতিয়ার দিয়েই এই কোর্সের ৯০% algorithm-এর বিশ্লেষণ সম্ভব।

Next Module → Recursion & Induction Refresher — যা ছাড়া recurrence-এর প্রমাণ লেখা যায় না।