-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsequences-series.html
More file actions
1549 lines (1395 loc) · 77.7 KB
/
sequences-series.html
File metadata and controls
1549 lines (1395 loc) · 77.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sequences & Series - Math from Zero to CS - Better Dev</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<header class="topbar">
<button class="sidebar-toggle" aria-label="Open navigation" aria-expanded="false">
<span class="hamburger-icon"></span>
</button>
<a href="index.html" class="logo">Better Dev</a>
</header>
<div class="sidebar-backdrop" aria-hidden="true"></div>
<aside class="sidebar" aria-label="Site navigation">
<div class="sidebar-header">
<span class="sidebar-title">Navigation</span>
<button class="sidebar-close" aria-label="Close navigation">×</button>
</div>
<div class="sidebar-search">
<input type="text" class="sidebar-search-input" placeholder="Search topics..." aria-label="Search topics">
<div class="sidebar-search-results"></div>
</div>
<nav class="sidebar-nav">
<div class="sidebar-group">
<a href="index.html">Home</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Mathematics</div>
<a href="pre-algebra.html">Pre-Algebra</a>
<a href="algebra.html">Algebra</a>
<a href="sequences-series.html">Sequences & Series</a>
<a href="geometry.html">Geometry</a>
<a href="calculus.html">Calculus</a>
<a href="discrete-math.html">Discrete Math</a>
<a href="linear-algebra.html">Linear Algebra</a>
<a href="probability.html">Probability & Statistics</a>
<a href="binary-systems.html">Binary & Number Systems</a>
<a href="number-theory.html">Number Theory for CP</a>
<a href="computational-geometry.html">Computational Geometry</a>
<a href="game-theory.html">Game Theory</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Data Structures & Algorithms</div>
<a href="dsa-foundations.html">DSA Foundations</a>
<a href="arrays.html">Arrays & Strings</a>
<a href="stacks-queues.html">Stacks & Queues</a>
<a href="hashmaps.html">Hash Maps & Sets</a>
<a href="linked-lists.html">Linked Lists</a>
<a href="trees.html">Trees & BST</a>
<a href="graphs.html">Graphs</a>
<a href="sorting.html">Sorting & Searching</a>
<a href="patterns.html">LeetCode Patterns</a>
<a href="dp.html">Dynamic Programming</a>
<a href="advanced.html">Advanced Topics</a>
<a href="string-algorithms.html">String Algorithms</a>
<a href="advanced-graphs.html">Advanced Graphs</a>
<a href="advanced-dp.html">Advanced DP</a>
<a href="advanced-ds.html">Advanced Data Structures</a>
<a href="leetcode-650.html">The 650 Problems</a>
<a href="competitive-programming.html">CP Roadmap</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Languages & Systems</div>
<a href="cpp.html">C++</a>
<a href="golang.html">Go</a>
<a href="javascript.html">JavaScript Deep Dive</a>
<a href="typescript.html">TypeScript</a>
<a href="nodejs.html">Node.js Internals</a>
<a href="os.html">Operating Systems</a>
<a href="linux.html">Linux</a>
<a href="git.html">Git</a>
<a href="backend.html">Backend</a>
<a href="system-design.html">System Design</a>
<a href="networking.html">Networking</a>
<a href="cloud.html">Cloud & Infrastructure</a>
<a href="docker.html">Docker & Compose</a>
<a href="kubernetes.html">Kubernetes</a>
<a href="message-queues.html">Queues & Pub/Sub</a>
<a href="selfhosting.html">VPS & Self-Hosting</a>
<a href="databases.html">PostgreSQL & MySQL</a>
<a href="stripe.html">Stripe & Payments</a>
<a href="distributed-systems.html">Distributed Systems</a>
<a href="backend-engineering.html">Backend Engineering</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">JS/TS Ecosystem</div>
<a href="js-tooling.html">Tooling & Bundlers</a>
<a href="js-testing.html">Testing</a>
<a href="ts-projects.html">Building with TS</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">More</div>
<a href="seans-brain.html">Sean's Brain</a>
</div>
</nav>
</aside>
<!-- Page Header -->
<div class="container">
<div class="page-header">
<div class="breadcrumb"><a href="index.html">Home</a> › Sequences & Series</div>
<h1>Sequences & Series</h1>
<p>Arithmetic and geometric progressions from the ground up -- the math behind loop analysis, Big-O notation, and growth rates.</p>
<div class="tip-box" style="margin-top: 1rem;">
<div class="label">Why This Matters for CS</div>
<p>Ever seen this and felt lost?</p>
<pre><code>(n) + (n-1) + (n-2) + (n-3) + ... + 3 + 2 + 1 = n(n+1)/2
So O(n(n+1)/2) = O(n²/2 + n/2) = <strong>O(n²)</strong></code></pre>
<p>This is the sum of an <strong>arithmetic series</strong>. It shows up every time you analyze nested loops, selection sort, bubble sort, or any algorithm where the inner work shrinks by 1 each iteration. By the end of this page, you'll understand exactly where this formula comes from and why it simplifies to O(n²).</p>
<p style="margin-top:0.5rem;">Sequences and series are the mathematical language of algorithm analysis. <strong>Every</strong> time you count iterations, analyze recursion, or prove time complexity, you're using the formulas on this page.</p>
</div>
</div>
</div>
<!-- Table of Contents + Content -->
<div class="page-with-toc">
<!-- Sidebar TOC -->
<aside class="sidebar-toc">
<div class="toc">
<h4>Table of Contents</h4>
<a href="#what-is-sequence">1. What is a Sequence?</a>
<a href="#what-is-series">2. What is a Series?</a>
<a href="#arithmetic-sequences">3. Arithmetic Sequences</a>
<a href="#arithmetic-series">4. Arithmetic Series (Sums)</a>
<a href="#gauss-trick">5. The Gauss Trick & n(n+1)/2</a>
<a href="#big-o-connection">6. Why n(n+1)/2 = O(n²)</a>
<a href="#geometric-sequences">7. Geometric Sequences</a>
<a href="#geometric-series">8. Geometric Series (Sums)</a>
<a href="#infinite-series">9. Infinite Series & Convergence</a>
<a href="#sigma-notation">10. Sigma Notation</a>
<a href="#key-formulas">11. Key Sum Formulas for CS</a>
<a href="#harmonic-series">12. Harmonic Series</a>
<a href="#recursive-sequences">13. Recursive Sequences</a>
<a href="#real-world">14. Real-World Applications</a>
<a href="#quiz">15. Practice Quiz</a>
</div>
</aside>
<!-- Main Content -->
<div class="content">
<div class="container">
<!-- Inline TOC for mobile -->
<div class="toc" style="display:none;" id="mobile-toc">
<h4>Table of Contents</h4>
<a href="#what-is-sequence">1. What is a Sequence?</a>
<a href="#what-is-series">2. What is a Series?</a>
<a href="#arithmetic-sequences">3. Arithmetic Sequences</a>
<a href="#arithmetic-series">4. Arithmetic Series (Sums)</a>
<a href="#gauss-trick">5. The Gauss Trick & n(n+1)/2</a>
<a href="#big-o-connection">6. Why n(n+1)/2 = O(n²)</a>
<a href="#geometric-sequences">7. Geometric Sequences</a>
<a href="#geometric-series">8. Geometric Series (Sums)</a>
<a href="#infinite-series">9. Infinite Series & Convergence</a>
<a href="#sigma-notation">10. Sigma Notation</a>
<a href="#key-formulas">11. Key Sum Formulas for CS</a>
<a href="#harmonic-series">12. Harmonic Series</a>
<a href="#recursive-sequences">13. Recursive Sequences</a>
<a href="#real-world">14. Real-World Applications</a>
<a href="#quiz">15. Practice Quiz</a>
</div>
<!-- ==================== SECTION 1 ==================== -->
<section id="what-is-sequence">
<h2>1. What is a Sequence?</h2>
<p>A <strong>sequence</strong> is just an ordered list of numbers that follows a pattern. That's it. Nothing fancy -- it's a list where each number has a position.</p>
<div class="example-box">
<div class="label">Sequences You Already Know</div>
<p><strong>Counting numbers:</strong> 1, 2, 3, 4, 5, 6, 7, ...</p>
<p><strong>Even numbers:</strong> 2, 4, 6, 8, 10, 12, ...</p>
<p><strong>Odd numbers:</strong> 1, 3, 5, 7, 9, 11, ...</p>
<p><strong>Powers of 2:</strong> 1, 2, 4, 8, 16, 32, 64, ...</p>
<p><strong>Squares:</strong> 1, 4, 9, 16, 25, 36, ...</p>
<p><strong>Multiples of 5:</strong> 5, 10, 15, 20, 25, ...</p>
</div>
<h3>Naming the Terms</h3>
<p>We use <strong>a<sub>n</sub></strong> to mean "the nth term." The subscript tells you the position:</p>
<div class="formula-box">
a<sub>1</sub> = first term<br>
a<sub>2</sub> = second term<br>
a<sub>3</sub> = third term<br>
...<br>
a<sub>n</sub> = the nth term (the general term)
</div>
<div class="example-box">
<div class="label">Example</div>
<p>For the sequence <strong>2, 4, 6, 8, 10, ...</strong></p>
<ul>
<li>a<sub>1</sub> = 2 (first term)</li>
<li>a<sub>2</sub> = 4 (second term)</li>
<li>a<sub>3</sub> = 6 (third term)</li>
<li>a<sub>n</sub> = 2n (formula: to get the nth term, multiply n by 2)</li>
</ul>
<p style="margin-top:0.5rem;">Let's verify: a<sub>5</sub> = 2(5) = 10. Looking at the sequence: 2, 4, 6, 8, <strong>10</strong>. Correct!</p>
</div>
<h3>Two Types of Formulas</h3>
<p>There are two ways to describe a sequence:</p>
<table>
<tr>
<th>Type</th>
<th>How it Works</th>
<th>Example (for 2, 4, 6, 8, ...)</th>
</tr>
<tr>
<td><strong>Explicit formula</strong></td>
<td>Gives you any term directly from its position</td>
<td>a<sub>n</sub> = 2n (plug in n, get the answer)</td>
</tr>
<tr>
<td><strong>Recursive formula</strong></td>
<td>Gives you the next term from the previous one</td>
<td>a<sub>n</sub> = a<sub>n-1</sub> + 2, where a<sub>1</sub> = 2</td>
</tr>
</table>
<div class="tip-box">
<div class="label">Think of It Like Code</div>
<p><strong>Explicit formula</strong> is like array access -- O(1), instant lookup:</p>
<pre><code><span class="keyword">def</span> <span class="function">get_term</span>(n):
<span class="keyword">return</span> <span class="number">2</span> * n <span class="comment"># Instant! Just plug in n</span></code></pre>
<p><strong>Recursive formula</strong> is like a linked list -- you have to walk through from the start:</p>
<pre><code><span class="keyword">def</span> <span class="function">get_term</span>(n):
<span class="keyword">if</span> n == <span class="number">1</span>: <span class="keyword">return</span> <span class="number">2</span>
<span class="keyword">return</span> get_term(n - <span class="number">1</span>) + <span class="number">2</span> <span class="comment"># Need all previous terms!</span></code></pre>
</div>
<h3>Finite vs Infinite Sequences</h3>
<ul>
<li><strong>Finite sequence:</strong> Has a definite end. Example: 1, 2, 3, 4, 5 (5 terms)</li>
<li><strong>Infinite sequence:</strong> Goes on forever. Example: 1, 2, 3, 4, 5, ... (the "..." means it continues)</li>
</ul>
</section>
<!-- ==================== SECTION 2 ==================== -->
<section id="what-is-series">
<h2>2. What is a Series?</h2>
<p>A <strong>series</strong> is what you get when you <em>add up</em> the terms of a sequence. A sequence is a list; a series is a sum.</p>
<div class="example-box">
<div class="label">Sequence vs Series -- Side by Side</div>
<table>
<tr>
<th>Sequence (a list)</th>
<th>Series (a sum)</th>
</tr>
<tr>
<td>1, 2, 3, 4, 5</td>
<td>1 + 2 + 3 + 4 + 5 = <strong>15</strong></td>
</tr>
<tr>
<td>2, 4, 6, 8</td>
<td>2 + 4 + 6 + 8 = <strong>20</strong></td>
</tr>
<tr>
<td>1, 2, 4, 8</td>
<td>1 + 2 + 4 + 8 = <strong>15</strong></td>
</tr>
<tr>
<td>3, 3, 3, 3, 3</td>
<td>3 + 3 + 3 + 3 + 3 = <strong>15</strong></td>
</tr>
</table>
</div>
<div class="tip-box">
<div class="label">Why Series Matter for Programming</div>
<p>When you write a loop, you create a sequence. When you count the <strong>total work</strong> that loop does, you compute a series.</p>
<pre><code><span class="comment"># The loop variable i creates the sequence: 0, 1, 2, 3, 4</span>
<span class="keyword">for</span> i <span class="keyword">in</span> <span class="builtin">range</span>(<span class="number">5</span>):
<span class="comment"># If we do 'i' units of work each iteration,</span>
<span class="comment"># the TOTAL work is the SERIES: 0 + 1 + 2 + 3 + 4 = 10</span>
<span class="keyword">for</span> j <span class="keyword">in</span> <span class="builtin">range</span>(i):
do_something() <span class="comment"># This runs 10 times total, not 5!</span></code></pre>
<p>Understanding series = understanding how to count total iterations of nested loops.</p>
</div>
<h3>Partial Sums</h3>
<p>A <strong>partial sum</strong> S<sub>n</sub> is the sum of the first n terms:</p>
<ul>
<li>S<sub>1</sub> = a<sub>1</sub> (just the first term)</li>
<li>S<sub>2</sub> = a<sub>1</sub> + a<sub>2</sub> (first two terms)</li>
<li>S<sub>3</sub> = a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> (first three terms)</li>
<li>S<sub>n</sub> = a<sub>1</sub> + a<sub>2</sub> + ... + a<sub>n</sub> (first n terms)</li>
</ul>
<div class="example-box">
<div class="label">Example -- Partial Sums of 1, 2, 3, 4, 5, ...</div>
<p>S<sub>1</sub> = 1</p>
<p>S<sub>2</sub> = 1 + 2 = 3</p>
<p>S<sub>3</sub> = 1 + 2 + 3 = 6</p>
<p>S<sub>4</sub> = 1 + 2 + 3 + 4 = 10</p>
<p>S<sub>5</sub> = 1 + 2 + 3 + 4 + 5 = 15</p>
<p style="margin-top:0.5rem;">These numbers (1, 3, 6, 10, 15) are called <strong>triangular numbers</strong>. They form the shape of a triangle when you stack dots. We'll see a formula for these soon.</p>
</div>
</section>
<!-- ==================== SECTION 3 ==================== -->
<section id="arithmetic-sequences">
<h2>3. Arithmetic Sequences</h2>
<p>An <strong>arithmetic sequence</strong> (also called an <strong>arithmetic progression</strong> or <strong>AP</strong>) is a sequence where you get each new term by <strong>adding the same number</strong> every time. That number is called the <strong>common difference (d)</strong>.</p>
<div class="formula-box">
<strong>Rule:</strong> Each term = previous term + d<br><br>
a<sub>1</sub>, a<sub>1</sub> + d, a<sub>1</sub> + 2d, a<sub>1</sub> + 3d, ...
</div>
<div class="example-box">
<div class="label">Spotting Arithmetic Sequences</div>
<p>Subtract consecutive terms. If the difference is always the same, it's arithmetic.</p>
<p style="margin-top:0.5rem;"><strong>3, 7, 11, 15, 19, ...</strong></p>
<p>7 - 3 = <strong>4</strong>, 11 - 7 = <strong>4</strong>, 15 - 11 = <strong>4</strong>, 19 - 15 = <strong>4</strong></p>
<p>Common difference d = 4. Arithmetic!</p>
<p style="margin-top:1rem;"><strong>10, 7, 4, 1, -2, -5, ...</strong></p>
<p>7 - 10 = <strong>-3</strong>, 4 - 7 = <strong>-3</strong>, 1 - 4 = <strong>-3</strong></p>
<p>Common difference d = -3. Still arithmetic! (d can be negative)</p>
<p style="margin-top:1rem;"><strong>1, 2, 4, 8, 16, ...</strong></p>
<p>2 - 1 = 1, 4 - 2 = 2, 8 - 4 = 4 -- differences are NOT the same.</p>
<p>Not arithmetic! (This is geometric -- we'll cover that in section 7)</p>
</div>
<h3>The nth Term Formula</h3>
<p>To find any term without listing them all, use this formula:</p>
<div class="formula-box">
<strong>General term:</strong> a<sub>n</sub> = a<sub>1</sub> + (n - 1) × d<br><br>
<strong>Where:</strong><br>
• a<sub>1</sub> = first term<br>
• d = common difference (subtract any term from the next)<br>
• n = which term you want<br>
• a<sub>n</sub> = the value of that term
</div>
<h3>Why (n - 1) and Not Just n?</h3>
<p>This is the most common mistake, so let's build the formula from scratch to see why:</p>
<div class="example-box">
<div class="label">Building the Formula Step by Step</div>
<p>Start with a<sub>1</sub> = 3, d = 4. Let's write out each term:</p>
<table>
<tr>
<th>Term</th>
<th>Value</th>
<th>How We Got It</th>
<th>How Many Times We Added d</th>
</tr>
<tr>
<td>a<sub>1</sub></td>
<td>3</td>
<td>3 (just the start)</td>
<td>0 times</td>
</tr>
<tr>
<td>a<sub>2</sub></td>
<td>7</td>
<td>3 + 4</td>
<td>1 time</td>
</tr>
<tr>
<td>a<sub>3</sub></td>
<td>11</td>
<td>3 + 4 + 4</td>
<td>2 times</td>
</tr>
<tr>
<td>a<sub>4</sub></td>
<td>15</td>
<td>3 + 4 + 4 + 4</td>
<td>3 times</td>
</tr>
<tr>
<td>a<sub>5</sub></td>
<td>19</td>
<td>3 + 4 + 4 + 4 + 4</td>
<td>4 times</td>
</tr>
<tr>
<td>a<sub>n</sub></td>
<td>?</td>
<td>3 + 4 × (n - 1)</td>
<td>(n - 1) times</td>
</tr>
</table>
<p style="margin-top:1rem;"><strong>Pattern:</strong> To reach term n, you add d exactly <strong>(n - 1)</strong> times because the first term doesn't require any addition.</p>
<p>So: a<sub>n</sub> = 3 + 4(n - 1) = 3 + 4n - 4 = <strong>4n - 1</strong></p>
</div>
<div class="warning-box">
<div class="label">Common Off-By-One Error</div>
<p>The formula is a<sub>n</sub> = a<sub>1</sub> + <strong>(n - 1)</strong>d, NOT a<sub>1</sub> + nd.</p>
<p>Think of it like a fence: to make a fence with 5 posts, you need 4 gaps between them. To get to the 5th term, you make 4 jumps of d.</p>
</div>
<div class="example-box">
<div class="label">Example -- Find the 50th Term</div>
<p>Sequence: 5, 8, 11, 14, 17, ...</p>
<p><strong>Step 1:</strong> Find a<sub>1</sub> = 5</p>
<p><strong>Step 2:</strong> Find d = 8 - 5 = 3</p>
<p><strong>Step 3:</strong> Apply formula: a<sub>50</sub> = 5 + (50 - 1)(3) = 5 + 49(3) = 5 + 147 = <strong>152</strong></p>
<p style="margin-top:0.5rem;"><em>Without the formula, you'd have to list all 50 numbers. With it, instant!</em></p>
</div>
<div class="example-box">
<div class="label">Example -- Find Which Term Equals 100</div>
<p>Sequence: 3, 7, 11, 15, ... Which term is 100? (or: does 100 even appear?)</p>
<p>a<sub>1</sub> = 3, d = 4. Set a<sub>n</sub> = 100:</p>
<p>100 = 3 + (n - 1)(4)</p>
<p>100 = 3 + 4n - 4</p>
<p>100 = 4n - 1</p>
<p>101 = 4n</p>
<p>n = 101/4 = 25.25</p>
<p>Since n must be a whole number, <strong>100 is NOT in this sequence</strong>. The 25th term is 99 and the 26th is 103.</p>
</div>
<div class="tip-box">
<div class="label">CS Connection: Array Indexing & Memory</div>
<p>Array memory addresses form an arithmetic sequence! If an array starts at memory address 1000 and each element takes 4 bytes:</p>
<pre><code><span class="comment"># Element addresses: 1000, 1004, 1008, 1012, ...</span>
<span class="comment"># a₁ = 1000, d = 4</span>
<span class="comment"># Address of element n: 1000 + (n-1) × 4</span>
<span class="comment"># THIS is why array access is O(1) -- it's just plugging into a formula!</span></code></pre>
</div>
</section>
<!-- ==================== SECTION 4 ==================== -->
<section id="arithmetic-series">
<h2>4. Arithmetic Series (Sums)</h2>
<p>An <strong>arithmetic series</strong> is the sum of an arithmetic sequence. Instead of listing the terms, we add them all up.</p>
<div class="formula-box">
<strong>Sum of first n terms:</strong><br><br>
S<sub>n</sub> = n/2 × (a<sub>1</sub> + a<sub>n</sub>) = n/2 × (first term + last term)<br><br>
<strong>Or equivalently:</strong><br>
S<sub>n</sub> = n/2 × (2a<sub>1</sub> + (n - 1)d)<br><br>
<span style="color:#555555;">Use the first form when you know the last term. Use the second when you don't.</span>
</div>
<p>But where does this formula come from? Let's build it up intuitively in the next section.</p>
<div class="example-box">
<div class="label">Quick Example First</div>
<p>Find the sum: 2 + 4 + 6 + 8 + 10</p>
<p>a<sub>1</sub> = 2, a<sub>n</sub> = 10, n = 5 terms</p>
<p>S<sub>5</sub> = 5/2 × (2 + 10) = 2.5 × 12 = <strong>30</strong></p>
<p>Verify: 2 + 4 + 6 + 8 + 10 = 30. Correct!</p>
</div>
</section>
<!-- ==================== SECTION 5 ==================== -->
<section id="gauss-trick">
<h2>5. The Gauss Trick & n(n+1)/2</h2>
<p>This is the most important formula in algorithm analysis. Let's understand it deeply.</p>
<h3>The Story</h3>
<p>Legend says that when the mathematician Carl Friedrich Gauss was a schoolchild, his teacher told the class to add up all numbers from 1 to 100, expecting it to keep them busy. Young Gauss answered almost immediately: <strong>5,050</strong>.</p>
<p>Here's how he did it:</p>
<div class="example-box">
<div class="label">The Pairing Trick -- Step by Step</div>
<p>We want: S = 1 + 2 + 3 + ... + 98 + 99 + 100</p>
<p style="margin-top:1rem;"><strong>Step 1: Write the sum forwards and backwards</strong></p>
<pre><code>S = 1 + 2 + 3 + ... + 98 + 99 + 100
S = 100 + 99 + 98 + ... + 3 + 2 + 1</code></pre>
<p style="margin-top:1rem;"><strong>Step 2: Add the two rows column by column</strong></p>
<pre><code>2S = 101 + 101 + 101 + ... + 101 + 101 + 101</code></pre>
<p>Every pair sums to 101! And there are 100 pairs.</p>
<p style="margin-top:1rem;"><strong>Step 3: Solve</strong></p>
<p>2S = 100 × 101 = 10,100</p>
<p>S = 10,100 / 2 = <strong>5,050</strong></p>
</div>
<h3>The General Formula</h3>
<p>The same trick works for 1 + 2 + 3 + ... + n:</p>
<div class="formula-box">
<strong>Sum of 1 to n:</strong><br><br>
1 + 2 + 3 + ... + n = <strong>n(n + 1) / 2</strong><br><br>
<span style="color:#555555;">First term + last term = (1 + n) = (n + 1). Number of pairs = n/2. Product = n(n+1)/2.</span>
</div>
<div class="example-box">
<div class="label">Why This Works -- Another Way to See It</div>
<p>Pair the terms from outside in:</p>
<pre><code>1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
| |
└──────────────── 1 + 10 = 11 ──────────────────────────┘
| |
└──────────── 2 + 9 = 11 ──────────────────┘
| |
└──────── 3 + 8 = 11 ──────────┘
| |
└──── 4 + 7 = 11 ──┘
| |
5 + 6 = 11</code></pre>
<p><strong>5 pairs, each summing to 11</strong> = 5 × 11 = 55</p>
<p>In general: <strong>n/2 pairs</strong>, each summing to <strong>(n + 1)</strong> = n(n+1)/2</p>
</div>
<div class="example-box">
<div class="label">Worked Examples</div>
<p><strong>1 + 2 + 3 + ... + 10:</strong></p>
<p>n = 10 → 10(11)/2 = 110/2 = <strong>55</strong></p>
<p style="margin-top:1rem;"><strong>1 + 2 + 3 + ... + 100:</strong></p>
<p>n = 100 → 100(101)/2 = 10,100/2 = <strong>5,050</strong></p>
<p style="margin-top:1rem;"><strong>1 + 2 + 3 + ... + 1000:</strong></p>
<p>n = 1000 → 1000(1001)/2 = 1,001,000/2 = <strong>500,500</strong></p>
<p style="margin-top:1rem;"><strong>0 + 1 + 2 + ... + (n-1):</strong></p>
<p>This is the same as 1 + 2 + ... + (n-1) = (n-1)(n)/2 = <strong>n(n-1)/2</strong></p>
<p><em>This version appears more often in code (0-indexed loops)!</em></p>
</div>
<div class="warning-box">
<div class="label">0-indexed vs 1-indexed</div>
<p>In math, we usually sum from 1 to n: <strong>n(n+1)/2</strong></p>
<p>In code, loops usually go from 0 to n-1: <strong>n(n-1)/2</strong></p>
<p>Both are O(n²) -- the constant doesn't matter for Big-O.</p>
</div>
</section>
<!-- ==================== SECTION 6 ==================== -->
<section id="big-o-connection">
<h2>6. Why n(n+1)/2 = O(n²)</h2>
<p>This is <strong>exactly</strong> what the image you saw is about. Let's break it down completely.</p>
<h3>The Setup: A Shrinking Inner Loop</h3>
<p>Consider selection sort, or any algorithm where the inner loop does less work each iteration:</p>
<pre><code><span class="keyword">for</span> i <span class="keyword">in</span> <span class="builtin">range</span>(n): <span class="comment"># Outer: runs n times</span>
<span class="keyword">for</span> j <span class="keyword">in</span> <span class="builtin">range</span>(i, n): <span class="comment"># Inner: runs (n-i) times each iteration</span>
compare(arr[j]) <span class="comment"># Some O(1) work</span></code></pre>
<p>How many times does <code>compare()</code> run in total?</p>
<div class="example-box">
<div class="label">Counting Iterations</div>
<table>
<tr>
<th>i =</th>
<th>Inner loop runs</th>
<th>Times</th>
</tr>
<tr><td>0</td><td>j goes from 0 to n-1</td><td>n</td></tr>
<tr><td>1</td><td>j goes from 1 to n-1</td><td>n - 1</td></tr>
<tr><td>2</td><td>j goes from 2 to n-1</td><td>n - 2</td></tr>
<tr><td>3</td><td>j goes from 3 to n-1</td><td>n - 3</td></tr>
<tr><td>...</td><td>...</td><td>...</td></tr>
<tr><td>n-2</td><td>j goes from n-2 to n-1</td><td>2</td></tr>
<tr><td>n-1</td><td>j goes from n-1 to n-1</td><td>1</td></tr>
</table>
</div>
<p>So the total is:</p>
<div class="formula-box">
<strong>Total = n + (n-1) + (n-2) + (n-3) + ... + 3 + 2 + 1</strong><br><br>
This is just 1 + 2 + 3 + ... + n written backwards!<br><br>
= <strong>n(n+1)/2</strong>
</div>
<h3>From Exact Count to Big-O</h3>
<p>Now, n(n+1)/2 is an exact count. Let's expand it to see why it's O(n²):</p>
<div class="formula-box">
n(n+1)/2<br>
= (n² + n) / 2<br>
= n²/2 + n/2<br><br>
In Big-O, we drop constants and lower-order terms:<br>
• Drop the /2: it's a constant multiplier<br>
• Drop the + n/2: it grows slower than n²<br><br>
= <strong>O(n²)</strong>
</div>
<div class="example-box">
<div class="label">Why We Can Drop Constants and Lower Terms</div>
<p>Big-O cares about <strong>growth rate</strong>, not exact count. Let's see why the n/2 term doesn't matter as n gets large:</p>
<table>
<tr>
<th>n</th>
<th>n²/2</th>
<th>n/2</th>
<th>Total (n²/2 + n/2)</th>
<th>n/2 as % of total</th>
</tr>
<tr><td>10</td><td>50</td><td>5</td><td>55</td><td>9.1%</td></tr>
<tr><td>100</td><td>5,000</td><td>50</td><td>5,050</td><td>1.0%</td></tr>
<tr><td>1,000</td><td>500,000</td><td>500</td><td>500,500</td><td>0.1%</td></tr>
<tr><td>1,000,000</td><td>500,000,000,000</td><td>500,000</td><td>500,000,500,000</td><td>0.0001%</td></tr>
</table>
<p style="margin-top:1rem;">As n grows, the n² term <strong>completely dominates</strong>. The n term becomes negligible. That's why O(n²/2 + n/2) = O(n²).</p>
</div>
<h3>Algorithms That Use This Pattern</h3>
<div class="example-box">
<div class="label">Selection Sort</div>
<pre><code><span class="keyword">def</span> <span class="function">selection_sort</span>(arr):
n = <span class="builtin">len</span>(arr)
<span class="keyword">for</span> i <span class="keyword">in</span> <span class="builtin">range</span>(n): <span class="comment"># n iterations</span>
min_idx = i
<span class="keyword">for</span> j <span class="keyword">in</span> <span class="builtin">range</span>(i+<span class="number">1</span>, n): <span class="comment"># (n-1) + (n-2) + ... + 1 = n(n-1)/2</span>
<span class="keyword">if</span> arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
<span class="comment"># Total comparisons: n(n-1)/2 = O(n²)</span></code></pre>
</div>
<div class="example-box">
<div class="label">Bubble Sort</div>
<pre><code><span class="keyword">def</span> <span class="function">bubble_sort</span>(arr):
n = <span class="builtin">len</span>(arr)
<span class="keyword">for</span> i <span class="keyword">in</span> <span class="builtin">range</span>(n): <span class="comment"># n passes</span>
<span class="keyword">for</span> j <span class="keyword">in</span> <span class="builtin">range</span>(n-<span class="number">1</span>-i): <span class="comment"># (n-1) + (n-2) + ... + 1</span>
<span class="keyword">if</span> arr[j] > arr[j+<span class="number">1</span>]:
arr[j], arr[j+<span class="number">1</span>] = arr[j+<span class="number">1</span>], arr[j]
<span class="comment"># Total comparisons: n(n-1)/2 = O(n²)</span></code></pre>
</div>
<div class="example-box">
<div class="label">Insertion Sort</div>
<pre><code><span class="keyword">def</span> <span class="function">insertion_sort</span>(arr):
<span class="keyword">for</span> i <span class="keyword">in</span> <span class="builtin">range</span>(<span class="number">1</span>, <span class="builtin">len</span>(arr)): <span class="comment"># n-1 iterations</span>
key = arr[i]
j = i - <span class="number">1</span>
<span class="keyword">while</span> j >= <span class="number">0</span> <span class="keyword">and</span> arr[j] > key: <span class="comment"># Worst case: i comparisons</span>
arr[j+<span class="number">1</span>] = arr[j]
j -= <span class="number">1</span>
arr[j+<span class="number">1</span>] = key
<span class="comment"># Worst case: 1 + 2 + ... + (n-1) = n(n-1)/2 = O(n²)</span></code></pre>
</div>
<div class="tip-box">
<div class="label">The Key Insight</div>
<p>Whenever you see a loop pattern where the inner work counts down (or up) like n, n-1, n-2, ..., 1 -- you know instantly it's <strong>O(n²)</strong> because you're summing an arithmetic series, and the sum is n(n+1)/2.</p>
</div>
</section>
<!-- ==================== SECTION 7 ==================== -->
<section id="geometric-sequences">
<h2>7. Geometric Sequences</h2>
<p>A <strong>geometric sequence</strong> (also called a <strong>geometric progression</strong> or <strong>GP</strong>) is a sequence where you get each new term by <strong>multiplying by the same number</strong> every time. That number is called the <strong>common ratio (r)</strong>.</p>
<div class="formula-box">
<strong>Rule:</strong> Each term = previous term × r<br><br>
a<sub>1</sub>, a<sub>1</sub>×r, a<sub>1</sub>×r², a<sub>1</sub>×r³, ...
</div>
<div class="example-box">
<div class="label">Spotting Geometric Sequences</div>
<p>Divide consecutive terms. If the ratio is always the same, it's geometric.</p>
<p style="margin-top:0.5rem;"><strong>2, 6, 18, 54, 162, ...</strong></p>
<p>6/2 = <strong>3</strong>, 18/6 = <strong>3</strong>, 54/18 = <strong>3</strong>, 162/54 = <strong>3</strong></p>
<p>Common ratio r = 3. Geometric!</p>
<p style="margin-top:1rem;"><strong>100, 50, 25, 12.5, 6.25, ...</strong></p>
<p>50/100 = <strong>0.5</strong>, 25/50 = <strong>0.5</strong>, 12.5/25 = <strong>0.5</strong></p>
<p>Common ratio r = 0.5. Geometric! (r can be a fraction)</p>
<p style="margin-top:1rem;"><strong>1, -2, 4, -8, 16, ...</strong></p>
<p>-2/1 = <strong>-2</strong>, 4/(-2) = <strong>-2</strong>, -8/4 = <strong>-2</strong></p>
<p>Common ratio r = -2. Geometric! (r can be negative -- terms alternate sign)</p>
</div>
<div class="example-box">
<div class="label">Arithmetic vs Geometric -- The Core Difference</div>
<table>
<tr>
<th></th>
<th>Arithmetic (AP)</th>
<th>Geometric (GP)</th>
</tr>
<tr>
<td><strong>Operation</strong></td>
<td>Add the same number</td>
<td>Multiply by the same number</td>
</tr>
<tr>
<td><strong>Growth</strong></td>
<td>Linear (steady)</td>
<td>Exponential (explosive or decaying)</td>
</tr>
<tr>
<td><strong>Example</strong></td>
<td>2, 5, 8, 11, 14 (d = 3)</td>
<td>2, 6, 18, 54, 162 (r = 3)</td>
</tr>
<tr>
<td><strong>10th term</strong></td>
<td>2 + 9(3) = 29</td>
<td>2 × 3&sup9; = 39,366</td>
</tr>
<tr>
<td><strong>CS analogy</strong></td>
<td>Linear search O(n)</td>
<td>Exponential blowup O(2<sup>n</sup>)</td>
</tr>
</table>
</div>
<h3>The nth Term Formula</h3>
<div class="formula-box">
<strong>General term:</strong> a<sub>n</sub> = a<sub>1</sub> × r<sup>(n-1)</sup><br><br>
<strong>Where:</strong><br>
• a<sub>1</sub> = first term<br>
• r = common ratio (divide any term by the previous one)<br>
• n = which term you want
</div>
<div class="example-box">
<div class="label">Building the Formula (Same Logic as Arithmetic)</div>
<p>Start with a<sub>1</sub> = 2, r = 3:</p>
<table>
<tr>
<th>Term</th>
<th>Value</th>
<th>How We Got It</th>
<th>Times Multiplied by r</th>
</tr>
<tr>
<td>a<sub>1</sub></td>
<td>2</td>
<td>2 (just the start)</td>
<td>0 times</td>
</tr>
<tr>
<td>a<sub>2</sub></td>
<td>6</td>
<td>2 × 3</td>
<td>1 time</td>
</tr>
<tr>
<td>a<sub>3</sub></td>
<td>18</td>
<td>2 × 3 × 3</td>
<td>2 times</td>
</tr>
<tr>
<td>a<sub>4</sub></td>
<td>54</td>
<td>2 × 3 × 3 × 3</td>
<td>3 times</td>
</tr>
<tr>
<td>a<sub>n</sub></td>
<td>?</td>
<td>2 × 3<sup>(n-1)</sup></td>
<td>(n-1) times</td>
</tr>
</table>
<p style="margin-top:1rem;">Same pattern as arithmetic: the (n - 1) is because the first term doesn't need any multiplication.</p>
</div>
<div class="example-box">
<div class="label">Example -- Powers of 2</div>
<p>Sequence: 1, 2, 4, 8, 16, 32, ...</p>
<p>a<sub>1</sub> = 1, r = 2</p>
<p>a<sub>n</sub> = 1 × 2<sup>(n-1)</sup> = 2<sup>(n-1)</sup></p>
<p style="margin-top:0.5rem;">The 10th term: a<sub>10</sub> = 2<sup>9</sup> = <strong>512</strong></p>
<p>The 20th term: a<sub>20</sub> = 2<sup>19</sup> = <strong>524,288</strong></p>
<p style="margin-top:0.5rem;"><em>This is exponential growth -- it gets enormous fast!</em></p>
</div>
<div class="tip-box">
<div class="label">CS Connection: Exponential Growth</div>
<p>Geometric sequences with r = 2 appear everywhere in CS:</p>
<ul>
<li><strong>Binary tree levels:</strong> 1, 2, 4, 8, 16, ... nodes per level</li>
<li><strong>Recursive branching:</strong> Each call spawns 2 more → 2<sup>n</sup> total calls</li>
<li><strong>Dynamic array resizing:</strong> Capacity doubles: 1, 2, 4, 8, 16, ...</li>
<li><strong>Bit lengths:</strong> n bits can represent 2<sup>n</sup> values</li>
</ul>
</div>
<div class="example-box">
<div class="label">Example -- Compound Interest</div>
<p>You invest $1,000 at 8% annual interest. How much after 10 years?</p>
<p>Each year, your money is multiplied by 1.08 (original + 8%).</p>
<p>a<sub>1</sub> = 1000, r = 1.08</p>
<p>After 10 years (11th term): a<sub>11</sub> = 1000 × 1.08<sup>10</sup> = 1000 × 2.159 = <strong>$2,158.92</strong></p>
<p style="margin-top:0.5rem;">Your money more than doubled! That's the power of geometric growth.</p>
</div>
<h3>Geometric Decay (When |r| < 1)</h3>
<p>When the common ratio is between -1 and 1 (exclusive), terms get smaller and smaller:</p>
<div class="example-box">
<div class="label">Exponential Decay</div>
<p><strong>Half-life:</strong> 1000, 500, 250, 125, 62.5, ... (r = 0.5)</p>
<p>Each term is half the previous. Terms approach zero but never reach it.</p>
<p style="margin-top:0.5rem;"><strong>In CS:</strong> Binary search cuts the problem in half each step. Starting with n elements, the remaining work is: n, n/2, n/4, n/8, ... This geometric decay is why binary search is O(log n)!</p>
</div>
</section>
<!-- ==================== SECTION 8 ==================== -->
<section id="geometric-series">
<h2>8. Geometric Series (Sums)</h2>
<p>A <strong>geometric series</strong> is the sum of a geometric sequence.</p>
<div class="formula-box">
<strong>Finite sum (when r ≠ 1):</strong><br><br>
S<sub>n</sub> = a<sub>1</sub> × (r<sup>n</sup> - 1) / (r - 1) <span style="color:#555555;">(use when r > 1)</span><br><br>
S<sub>n</sub> = a<sub>1</sub> × (1 - r<sup>n</sup>) / (1 - r) <span style="color:#555555;">(use when |r| < 1 -- avoids negatives)</span><br><br>
<span style="color:#555555;">These are the same formula rearranged. Use whichever avoids negative numbers.</span>
</div>
<h3>Where Does the Formula Come From?</h3>
<div class="example-box">
<div class="label">Deriving It -- The Subtraction Trick</div>
<p>Let's find S = 1 + 2 + 4 + 8 + 16 (geometric with a<sub>1</sub> = 1, r = 2, n = 5)</p>
<p style="margin-top:1rem;"><strong>Step 1: Write S</strong></p>
<p>S = 1 + 2 + 4 + 8 + 16</p>
<p style="margin-top:0.5rem;"><strong>Step 2: Multiply both sides by r (= 2)</strong></p>
<p>2S = 2 + 4 + 8 + 16 + 32</p>
<p style="margin-top:0.5rem;"><strong>Step 3: Subtract S from 2S</strong></p>
<pre><code>2S = 2 + 4 + 8 + 16 + 32
S = 1 + 2 + 4 + 8 + 16
─────────────────────────
S = 32 - 1 = 31</code></pre>
<p>Almost everything cancels! We're left with: 2S - S = 32 - 1</p>
<p>S = 2<sup>5</sup> - 1 = 32 - 1 = <strong>31</strong></p>
<p>Verify: 1 + 2 + 4 + 8 + 16 = 31. Correct!</p>
<p style="margin-top:1rem;">In general: rS - S = a<sub>1</sub>(r<sup>n</sup> - 1), so S = a<sub>1</sub>(r<sup>n</sup> - 1)/(r - 1)</p>
</div>
<div class="example-box">
<div class="label">The Powers of 2 Shortcut</div>
<p>For the sum 1 + 2 + 4 + 8 + ... + 2<sup>(n-1)</sup>:</p>
<p>S = (2<sup>n</sup> - 1) / (2 - 1) = <strong>2<sup>n</sup> - 1</strong></p>
<p style="margin-top:0.5rem;">Examples:</p>
<ul>
<li>1 + 2 + 4 = 2³ - 1 = <strong>7</strong></li>
<li>1 + 2 + 4 + 8 = 2<sup>4</sup> - 1 = <strong>15</strong></li>
<li>1 + 2 + 4 + 8 + 16 = 2<sup>5</sup> - 1 = <strong>31</strong></li>
<li>1 + 2 + 4 + ... + 128 = 2<sup>8</sup> - 1 = <strong>255</strong></li>
</ul>
</div>
<div class="tip-box">
<div class="label">CS Connection: Why 2<sup>n</sup> - 1 is Everywhere</div>
<ul>
<li><strong>8-bit max value:</strong> 1 + 2 + 4 + ... + 128 = 2<sup>8</sup> - 1 = 255</li>
<li><strong>32-bit max unsigned:</strong> 2<sup>32</sup> - 1 = 4,294,967,295</li>
<li><strong>Binary tree nodes:</strong> A complete tree of height h has 1 + 2 + 4 + ... + 2<sup>h</sup> = 2<sup>(h+1)</sup> - 1 total nodes</li>
<li><strong>Merge sort calls:</strong> Total recursive calls in merge sort: 2<sup>k</sup> - 1 where k = log<sub>2</sub>(n)</li>
</ul>
</div>
<div class="example-box">
<div class="label">Example -- Total Nodes in a Binary Tree</div>
<p>A complete binary tree of height 4:</p>
<pre><code>Level 0: 1 node (2&sup0;)
Level 1: 2 nodes (2¹)
Level 2: 4 nodes (2²)
Level 3: 8 nodes (2³)
Level 4: 16 nodes (2<sup>4</sup>)
──────────────────────────
Total: 31 nodes = 2<sup>5</sup> - 1</code></pre>
<p>This is a geometric series: 1 + 2 + 4 + 8 + 16 = 2<sup>5</sup> - 1 = 31</p>
</div>
<div class="example-box">
<div class="label">Example -- Work Done in Merge Sort</div>
<p>Merge sort splits n elements in half each level. At each level, the total work is O(n):</p>
<pre><code>Level 0: 1 chunk of size n → n work
Level 1: 2 chunks of size n/2 → n work
Level 2: 4 chunks of size n/4 → n work
...
Level k: n chunks of size 1 → n work</code></pre>
<p>There are log<sub>2</sub>(n) levels, each doing n work:</p>
<p>Total = n × log<sub>2</sub>(n) = <strong>O(n log n)</strong></p>
<p style="margin-top:0.5rem;">The geometric sequence (1, 2, 4, 8, ...) describes how the number of chunks doubles at each level, while the size of each chunk halves.</p>
</div>
</section>
<!-- ==================== SECTION 9 ==================== -->
<section id="infinite-series">
<h2>9. Infinite Series & Convergence</h2>
<p>What happens when you add up <em>infinitely many</em> terms? Sometimes the sum is finite (it <strong>converges</strong>), sometimes it blows up to infinity (it <strong>diverges</strong>).</p>
<div class="tip-box">
<div class="label">The Core Intuition</div>
<p>Can infinitely many things add up to a finite number? <strong>Yes</strong> -- if each new thing is small enough. Think of it physically:</p>
<ul>
<li><strong>Converges:</strong> Walk halfway to a wall, then half of what's left, then half again. Infinitely many steps, but you never pass the wall.</li>
<li><strong>Diverges:</strong> Stack bricks on top of each other, each the same size. Infinitely many bricks = infinite height.</li>
</ul>
<p style="margin-top:0.5rem;">The key question is: <strong>do the terms shrink fast enough?</strong> If each term is a fraction of the previous one (geometric with |r| < 1), they shrink fast enough. If the terms don't shrink, or shrink too slowly, the sum diverges.</p>
</div>
<h3>Convergent Geometric Series</h3>
<p>When |r| < 1, the terms get smaller and smaller, and the sum approaches a fixed value:</p>
<div class="formula-box">
<strong>Infinite geometric sum (|r| < 1):</strong><br><br>
S<sub>∞</sub> = a<sub>1</sub> / (1 - r)
</div>
<div class="example-box">
<div class="label">Example -- 1/2 + 1/4 + 1/8 + ...</div>
<p>a<sub>1</sub> = 1/2, r = 1/2</p>
<p>S<sub>∞</sub> = (1/2) / (1 - 1/2) = (1/2) / (1/2) = <strong>1</strong></p>
<p style="margin-top:0.5rem;">Let's see it converge:</p>
<table>
<tr>
<th>Terms Added</th>
<th>Sum</th>
<th>Distance from 1</th>
</tr>
<tr><td>1/2</td><td>0.5</td><td>0.5</td></tr>
<tr><td>+ 1/4</td><td>0.75</td><td>0.25</td></tr>
<tr><td>+ 1/8</td><td>0.875</td><td>0.125</td></tr>
<tr><td>+ 1/16</td><td>0.9375</td><td>0.0625</td></tr>
<tr><td>+ 1/32</td><td>0.96875</td><td>0.03125</td></tr>
<tr><td>+ 1/64</td><td>0.984375</td><td>0.015625</td></tr>
</table>
<p style="margin-top:0.5rem;">Each step gets closer to 1, but never exceeds it. With infinitely many terms, the sum equals exactly 1.</p>
</div>
<div class="example-box">
<div class="label">Example -- 1 + 1/2 + 1/4 + 1/8 + ...</div>
<p>a<sub>1</sub> = 1, r = 1/2</p>
<p>S<sub>∞</sub> = 1 / (1 - 1/2) = 1 / (1/2) = <strong>2</strong></p>
<p><em>Imagine walking to a wall. First you cover half the distance, then half of what's left, then half again... You'll get arbitrarily close to the wall (sum = 2) but each step is half the previous.</em></p>
</div>
<h3>Divergent Series</h3>
<p>When |r| ≥ 1, the terms don't shrink, so the sum grows without bound:</p>
<div class="example-box">
<div class="label">Divergent Examples</div>
<p><strong>1 + 2 + 4 + 8 + 16 + ...</strong> (r = 2) → Diverges! Each term is bigger than the last.</p>
<p><strong>1 + 1 + 1 + 1 + ...</strong> (r = 1) → Diverges! Adding 1 forever gives infinity.</p>
<p><strong>1 + 2 + 3 + 4 + ...</strong> (arithmetic, d = 1) → Diverges! Arithmetic series always diverge (unless d = 0).</p>
</div>
<div class="tip-box">
<div class="label">CS Connection: Amortized Analysis</div>
<p>Dynamic arrays (like Python lists or Java ArrayLists) use convergent geometric series for resizing:</p>
<pre><code><span class="comment"># ArrayList doubles when full. Copy costs over n insertions:</span>
<span class="comment"># 1 + 2 + 4 + 8 + ... + n = 2n - 1 ≈ 2n</span>
<span class="comment"># Amortized cost per insertion: 2n / n = O(1)</span>
<span class="comment">#</span>
<span class="comment"># The key insight: the geometric sum 1 + 2 + 4 + ... + n</span>
<span class="comment"># is dominated by the LAST term (n), not the sum of all others.</span>
<span class="comment"># So the total copy work is only about 2n, giving O(1) amortized!</span></code></pre>
</div>
</section>
<!-- ==================== SECTION 10 ==================== -->
<section id="sigma-notation">
<h2>10. Sigma Notation</h2>
<p><strong>Sigma notation</strong> (Σ) is shorthand for "add up a bunch of terms." If you can read a for loop, you can read sigma notation -- they're the same thing.</p>
<div class="formula-box">
<strong>Sigma notation:</strong><br><br>
Σ<sub>i=start</sub><sup>end</sup> f(i) = f(start) + f(start+1) + ... + f(end)<br><br>
<strong>Read it as:</strong> "Sum of f(i), for i going from start to end"
</div>
<div class="tip-box">
<div class="label">Read Sigma Like a For Loop</div>
<pre><code><span class="comment"># MATH: Σᵢ₌₁ⁿ f(i)</span>
<span class="comment"># CODE:</span>
total = <span class="number">0</span>
<span class="keyword">for</span> i <span class="keyword">in</span> <span class="builtin">range</span>(<span class="number">1</span>, n+<span class="number">1</span>): <span class="comment"># i = start to end</span>
total += f(i) <span class="comment"># body = the expression after Σ</span>
<span class="comment">#</span>
<span class="comment"># Bottom of Σ → loop start (i=1)</span>
<span class="comment"># Top of Σ → loop end (n)</span>
<span class="comment"># Expression after Σ → loop body (f(i))</span>
<span class="comment"># Σ itself → the += accumulator</span></code></pre>
<p>Every time you see Σ, mentally translate it to a for loop. Every time you see a for loop accumulating a total, mentally translate it to Σ.</p>