Mathematical Tools: Logs, Recurrences, Summations
গাণিতিক হাতিয়ার — log, recurrence, summation
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.
অর্থাৎ ১০০ কোটি ডেটার মধ্যে 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.
| n | log₂ n | halving steps |
|---|---|---|
| 1 | 0 | — |
| 2 | 1 | 2 → 1 |
| 1024 | 10 | 1024 → 512 → … → 1 |
| 10⁶ | ≈ 20 | 20 halvings |
| 10⁹ | ≈ 30 | 30 halvings |
| 10¹⁸ | ≈ 60 | 60 halvings |
- 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:
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.
4. Geometric Series
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.
5. Telescoping Sums
A "telescoping" sum is one where consecutive terms cancel, leaving only the first and the last:
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).
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.
#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:
| Recurrence | Solves to | Where 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 |
#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 (শব্দকোষ)
| Term | Meaning | বাংলায় |
|---|---|---|
| Logarithm | Inverse of exponentiation; how many doublings reach n. | সূচকীকরণের বিপরীত — n-এ পৌঁছাতে কতবার দ্বিগুণ করতে হবে। |
| Arithmetic series | Sum where each term differs by a constant. | যেখানে প্রতিটি term পূর্ববর্তী থেকে একই পরিমাণে বেশি। |
| Geometric series | Sum where each term is a constant factor of the previous. | যেখানে প্রতিটি term পূর্ববর্তী-এর একটি স্থির গুণিতক। |
| Harmonic number | Hn = Σ 1/i — grows like ln n. | Hn = Σ 1/i — ln n-এর হারে বাড়ে। |
| Recurrence | An equation defining T(n) in terms of T of smaller values. | T(n)-কে ছোট ইনপুটের T-এর মাধ্যমে প্রকাশ করা সমীকরণ। |
9. Practice Problems
-
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; } -
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।
-
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)।
-
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.
-
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.
-
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²।
-
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.