Projects About AlBasmala
build status

org-special-block-extras: a minimal working example

Article image
  

Ξ  

⭐ The code lives at github.com/alhassy/org-special-block-extras —star it, or discuss it there!

1. A tiny function

We sum the first n odd  numbers by cl-loop; place the cursor after the last parenthesis and press C-x C-e to define it.

(defun sum-odds (n)
  "Return 1 + 3 + ⋯ + (2n - 1)."
  (cl-loop for i from 1 to n sum (- (* 2 i) 1)))
Peek: the first few values

(sum-odds 4) is the sum of the first 4 odd numbers. In order:

n 1 2 3 4
(sum-odds n) 1 4 9 16

These are squares! We prove it below.

2. How the loop accumulates

Each step adds the next odd number to the running sum:

0 1 add 1 4 add 3 9 add 5 16 add 7

3. Why squares?

The squares are no coincidence:

\begin{align*} & \sum_{i=1}^{n} (2i - 1) \\ = & \quad \class{tooltip osbe-tip-hint osbe-hint}{\class{osbe-hint-body}{{\color[RGB]{128,0,0} \left[ \text{Split the sum} \right.}}\class{osbe-hint-fold}{{\color[RGB]{128,0,0} [\,\cdots\,]}}} \notag \\ & 2 · {\color{red}{\sum_{i=1}^{n} i}} - {\color{teal}{\sum_{i=1}^{n} 1}} \\ = & \quad \class{tooltip osbe-tip-hint osbe-hint}{\class{osbe-hint-body}{{\color[RGB]{128,0,0} \left[ \text{Gauss: ${\color{red}{\sum_{i=1}^{n} i}} = n(n+1)/2$; and $n$ ones sum to ${\color{teal}n}$} \right.}}\class{osbe-hint-fold}{{\color[RGB]{128,0,0} [\,\cdots\,]}}} \notag \\ & n(n + 1) - {\color{teal}n} \\ = & \quad \class{tooltip osbe-tip-hint osbe-hint}{\class{osbe-hint-body}{{\color[RGB]{128,0,0} \left[ \text{Distribute, then cancel} \right.}}\class{osbe-hint-fold}{{\color[RGB]{128,0,0} [\,\cdots\,]}}} \notag \\ & n^2 \tag{$\color{violet}{\text{Odd}}$} \label{Odd} \end{align*}

Hence, by \eqref{Odd}, (sum-odds n) is \(n^2\).


Generated by Emacs and Org-mode (•̀ᴗ•́)و
Creative Commons License
Life & Computing Science by Musa Al-hassy is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License