<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="/lectures//lectures/feed.xml" rel="self" type="application/atom+xml" /><link href="/lectures//lectures/" rel="alternate" type="text/html" /><updated>2026-04-30T14:40:03+00:00</updated><id>/lectures//lectures/feed.xml</id><title type="html">Adrian Statescu</title><subtitle>You are the stone; You are the chisel; You are the Architect of your own Happiness.    </subtitle><entry><title type="html">Cook–Levin Theorem</title><link href="/lectures//lectures/2026/04/23/theorem-cook-levin.html" rel="alternate" type="text/html" title="Cook–Levin Theorem" /><published>2026-04-23T00:00:00+00:00</published><updated>2026-04-23T00:00:00+00:00</updated><id>/lectures//lectures/2026/04/23/theorem-cook-levin</id><content type="html" xml:base="/lectures//lectures/2026/04/23/theorem-cook-levin.html"><![CDATA[<h2 id="overview">Overview</h2>

<p>The Cook–Levin Theorem is one of the fundamental results in theoretical computer science and complexity theory. It establishes the first known NP-complete problem and forms the foundation of the theory of NP-completeness.</p>

<p>It was independently proven by <strong>Stephen Cook (1971)</strong> and <strong>Leonid Levin (1973)</strong>.</p>

<hr />

<h2 id="statement-of-the-theorem">Statement of the Theorem</h2>

<p>The theorem states that:</p>

<blockquote>
  <p><strong>The Boolean Satisfiability Problem (SAT) is NP-complete.</strong></p>
</blockquote>

<p>This means two key properties hold:</p>

<h3 id="1-sat-is-in-np">1. SAT is in NP</h3>
<p>Given a candidate assignment of variables (True/False), we can verify whether it satisfies a Boolean formula in polynomial time.</p>

<h3 id="2-sat-is-np-hard">2. SAT is NP-hard</h3>
<p>Every problem in NP can be reduced to SAT in polynomial time:</p>

<p>[
\forall L \in NP,\quad L \leq_p SAT
]</p>

<hr />

<h2 id="key-ideas-behind-the-proof">Key Ideas Behind the Proof</h2>

<p>The proof is based on the following concepts:</p>

<h3 id="1-verification-in-np">1. Verification in NP</h3>
<p>Any problem in NP has a polynomial-time verifier. This means that given a solution (certificate), we can check its correctness efficiently.</p>

<h3 id="2-computation-as-a-boolean-circuit">2. Computation as a Boolean Circuit</h3>
<p>A nondeterministic polynomial-time computation can be represented as a sequence of logical steps, which can be encoded as a Boolean circuit.</p>

<h3 id="3-encoding-computation-into-sat">3. Encoding Computation into SAT</h3>
<p>The computation of the verifier is transformed into a Boolean formula such that:</p>
<ul>
  <li>The formula is satisfiable if and only if the computation accepts the input.</li>
  <li>Each step of computation is encoded using Boolean variables.</li>
</ul>

<h3 id="4-polynomial-time-reduction">4. Polynomial-Time Reduction</h3>
<p>The transformation from any NP problem to SAT is done in polynomial time.</p>

<hr />

<h2 id="consequences">Consequences</h2>

<p>The Cook–Levin Theorem has major consequences:</p>

<h3 id="1-definition-of-np-completeness">1. Definition of NP-completeness</h3>
<p>It introduced the concept of NP-complete problems.</p>

<h3 id="2-universal-problem">2. Universal Problem</h3>
<p>SAT becomes the “universal problem” for NP:</p>
<ul>
  <li>Any NP problem can be encoded as SAT.</li>
  <li>Solving SAT efficiently would solve all NP problems efficiently.</li>
</ul>

<h3 id="3-foundation-of-reductions">3. Foundation of Reductions</h3>
<p>It introduced polynomial-time reductions as a central tool in complexity theory.</p>

<hr />

<h2 id="important-implications">Important Implications</h2>

<ul>
  <li>If SAT can be solved in polynomial time, then <strong>P = NP</strong>.</li>
  <li>If P ≠ NP, then SAT cannot be solved efficiently.</li>
  <li>Many important problems (TSP, Subset Sum, Vertex Cover) are NP-complete because they reduce to SAT.</li>
</ul>

<hr />

<h2 id="intuition">Intuition</h2>

<p>The theorem shows that:</p>

<blockquote>
  <p>Any efficiently verifiable computation can be encoded as a Boolean formula.</p>
</blockquote>

<p>Thus, SAT acts as a universal language for all NP problems.</p>

<hr />

<h2 id="summary">Summary</h2>

<ul>
  <li>SAT is NP-complete</li>
  <li>Every NP problem reduces to SAT</li>
  <li>Computation can be encoded as logic</li>
  <li>This forms the basis of modern complexity theory</li>
</ul>

<hr />

<h2 id="authors">Authors</h2>

<ul>
  <li>Stephen Cook (USA)</li>
  <li>Leonid Levin (independent discovery, USSR)</li>
</ul>

<hr />

<h2 id="conclusion">Conclusion</h2>

<p>The Cook–Levin Theorem is the starting point of NP-completeness theory and shows that SAT is the central problem of the class NP.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Overview]]></summary></entry><entry><title type="html">Collatz Conjecture 3n + 1 in Rust</title><link href="/lectures//lectures/2026/04/12/collatz-conjecture.html" rel="alternate" type="text/html" title="Collatz Conjecture 3n + 1 in Rust" /><published>2026-04-12T00:00:00+00:00</published><updated>2026-04-12T00:00:00+00:00</updated><id>/lectures//lectures/2026/04/12/collatz-conjecture</id><content type="html" xml:base="/lectures//lectures/2026/04/12/collatz-conjecture.html"><![CDATA[<h3 id="iterative-version-time-complexity-on-space-o1">Iterative Version Time Complexity O(n) Space O(1)</h3>

<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="k">use</span> <span class="nn">std</span><span class="p">::</span><span class="n">io</span><span class="p">;</span>

<span class="k">fn</span> <span class="nf">collatz_sequence</span><span class="p">(</span><span class="k">mut</span> <span class="n">n</span><span class="p">:</span> <span class="nb">u64</span><span class="p">)</span> <span class="p">{</span>
   
   <span class="nd">print!</span><span class="p">(</span><span class="s">"{} "</span><span class="p">,</span> <span class="n">n</span><span class="p">);</span>
   
   
   <span class="k">while</span> <span class="n">n</span> <span class="o">!=</span> <span class="mi">1</span> <span class="p">{</span>
   
         <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span> <span class="p">{</span>
         
           <span class="n">n</span> <span class="o">/=</span> <span class="mi">2</span><span class="p">;</span>
            
         <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
                  
           <span class="n">n</span> <span class="o">=</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span>        
         <span class="p">}</span> 
         
         <span class="nd">print!</span><span class="p">(</span><span class="s">"{} "</span><span class="p">,</span> <span class="n">n</span><span class="p">);</span>
   <span class="p">}</span>
    
<span class="p">}</span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>

   <span class="k">let</span> <span class="k">mut</span> <span class="n">input</span> <span class="o">=</span> <span class="nn">String</span><span class="p">::</span><span class="nf">new</span><span class="p">();</span> <span class="c1">//creates an enpty mutable string that will store the user input</span>
   
   <span class="nn">io</span><span class="p">::</span><span class="nf">stdin</span><span class="p">()</span><span class="nf">.read_line</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span> <span class="n">input</span><span class="p">)</span><span class="nf">.unwrap</span><span class="p">();</span> <span class="c1">//reads a full line from standard input keyboard and store it in input</span>
   <span class="c1">//for example, if you type 23, then input becomes "23\n" including newline character,so input becomes 23</span>
   
   <span class="k">let</span> <span class="n">n</span><span class="p">:</span> <span class="nb">u64</span> <span class="o">=</span> <span class="n">input</span><span class="nf">.trim</span><span class="p">()</span><span class="nf">.parse</span><span class="p">()</span><span class="nf">.unwrap</span><span class="p">();</span>
   <span class="c1">//attempts to convert the trimmed string into a number type</span>
   <span class="c1">//specifies that the parsed valud should be interpreted as an unsigned 64-bit integer(u64)</span>
   
   <span class="c1">//unwrap() -&gt; if parsing succeeds, it returns the numbers, otherwise if the parsing fails (e.g. input = "abc") the program will crash (panic)</span>
   
   <span class="nf">collatz_sequence</span><span class="p">(</span> <span class="n">n</span> <span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>

<h3 id="recursive-version-time-complexity-on-space-o1">Recursive Version Time Complexity O(n) Space O(1)</h3>

<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="k">use</span> <span class="nn">std</span><span class="p">::</span><span class="n">io</span><span class="p">;</span>

<span class="k">fn</span> <span class="nf">collatz_recursive</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">u64</span><span class="p">)</span> <span class="p">{</span>

    <span class="nd">print!</span><span class="p">(</span><span class="s">"{} "</span><span class="p">,</span> <span class="n">n</span><span class="p">);</span>

    <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span> <span class="p">{</span>
    
        <span class="k">return</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span> <span class="p">{</span>
    
        <span class="nf">collatz_recursive</span><span class="p">(</span><span class="n">n</span> <span class="o">/</span> <span class="mi">2</span><span class="p">);</span>
        
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
    
        <span class="nf">collatz_recursive</span><span class="p">(</span><span class="mi">3</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>

    <span class="k">let</span> <span class="k">mut</span> <span class="n">input</span> <span class="o">=</span> <span class="nn">String</span><span class="p">::</span><span class="nf">new</span><span class="p">();</span>
    
    <span class="nn">io</span><span class="p">::</span><span class="nf">stdin</span><span class="p">()</span><span class="nf">.read_line</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span> <span class="n">input</span><span class="p">)</span><span class="nf">.unwrap</span><span class="p">();</span>
    
    <span class="k">let</span> <span class="n">n</span><span class="p">:</span> <span class="nb">u64</span> <span class="o">=</span> <span class="n">input</span><span class="nf">.trim</span><span class="p">()</span><span class="nf">.parse</span><span class="p">()</span><span class="nf">.unwrap</span><span class="p">();</span>

    <span class="nf">collatz_recursive</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>

<p>–</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Iterative Version Time Complexity O(n) Space O(1)]]></summary></entry><entry><title type="html">Lambda Functions in c++11</title><link href="/lectures//lectures/2026/04/11/lambda-functions.html" rel="alternate" type="text/html" title="Lambda Functions in c++11" /><published>2026-04-11T00:00:00+00:00</published><updated>2026-04-11T00:00:00+00:00</updated><id>/lectures//lectures/2026/04/11/lambda-functions</id><content type="html" xml:base="/lectures//lectures/2026/04/11/lambda-functions.html"><![CDATA[<h3 id="an-introductory-course--c11-and-beyond">An introductory course — C++11 and beyond</h3>
<blockquote>
  <table>
    <tbody>
      <tr>
        <td>Covers: syntax</td>
        <td>capture</td>
        <td>STL algorithms</td>
        <td>closures</td>
        <td>best practices</td>
      </tr>
    </tbody>
  </table>
</blockquote>

<hr />

<h2 id="1-what-is-a-lambda-function">1. What is a Lambda Function?</h2>

<p>A lambda function (also called a lambda expression or anonymous function) is a function defined inline, without a name, exactly where it is needed. Introduced in C++11, lambdas are one of the most important modern features of the language.</p>

<p>Before lambdas, passing custom behaviour to algorithms required writing a separate named function or a functor (a class with <code class="language-plaintext highlighter-rouge">operator()</code>). Lambdas eliminate that boilerplate entirely.</p>

<p><strong>Without lambda (C++03)</strong></p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// A separate comparator function was required</span>
<span class="kt">bool</span> <span class="nf">byLength</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">a</span><span class="p">,</span> <span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()</span> <span class="o">&lt;</span> <span class="n">b</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
<span class="p">}</span>

<span class="n">std</span><span class="o">::</span><span class="n">sort</span><span class="p">(</span><span class="n">words</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">words</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span> <span class="n">byLength</span><span class="p">);</span>
</code></pre></div></div>

<p><strong>With lambda (C++11)</strong></p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// The comparator lives right at the call site</span>
<span class="n">std</span><span class="o">::</span><span class="n">sort</span><span class="p">(</span><span class="n">words</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">words</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span>
    <span class="p">[](</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">a</span><span class="p">,</span> <span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">()</span> <span class="o">&lt;</span> <span class="n">b</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
    <span class="p">});</span>
</code></pre></div></div>

<p>The second version is shorter, easier to read, and keeps the logic <strong>exactly where it belongs</strong>.</p>

<hr />

<h2 id="2-syntax-breakdown">2. Syntax Breakdown</h2>

<p>Every lambda expression has four parts, three of which are optional:</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">[</span> <span class="n">capture</span> <span class="p">]</span> <span class="p">(</span> <span class="n">parameters</span> <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">return_type</span> <span class="p">{</span> <span class="n">body</span> <span class="p">}</span>
</code></pre></div></div>

<table>
  <thead>
    <tr>
      <th>Part</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">[ capture ]</code></td>
      <td>Variables from the outer scope accessible inside the lambda. Required, but can be empty <code class="language-plaintext highlighter-rouge">[]</code>.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">( parameters )</code></td>
      <td>Input parameters, exactly like a normal function. Can be omitted if there are none.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">-&gt; return_type</code></td>
      <td>The return type. Optional — the compiler deduces it automatically in almost all cases.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">{ body }</code></td>
      <td>The function body. Any valid C++ code.</td>
    </tr>
  </tbody>
</table>

<p><strong>Minimal examples</strong></p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// Absolutely minimal — no capture, no params, deduced return</span>
<span class="k">auto</span> <span class="n">greet</span> <span class="o">=</span> <span class="p">[]</span> <span class="p">{</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"Hello!</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span> <span class="p">};</span>

<span class="c1">// With parameters</span>
<span class="k">auto</span> <span class="n">add</span> <span class="o">=</span> <span class="p">[](</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">;</span> <span class="p">};</span>

<span class="c1">// With explicit return type</span>
<span class="k">auto</span> <span class="n">divide</span> <span class="o">=</span> <span class="p">[](</span><span class="kt">double</span> <span class="n">a</span><span class="p">,</span> <span class="kt">double</span> <span class="n">b</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">double</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">b</span> <span class="o">==</span> <span class="mf">0.0</span><span class="p">)</span> <span class="k">return</span> <span class="mf">0.0</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">a</span> <span class="o">/</span> <span class="n">b</span><span class="p">;</span>
<span class="p">};</span>
</code></pre></div></div>

<hr />

<h2 id="3-the-capture-clause">3. The Capture Clause</h2>

<p>The capture clause <code class="language-plaintext highlighter-rouge">[ ]</code> determines which variables from the surrounding scope are available inside the lambda, and how they are passed in.</p>

<table>
  <thead>
    <tr>
      <th>Syntax</th>
      <th>Meaning</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">[]</code></td>
      <td>Nothing captured. The lambda cannot access any outer variables.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">[=]</code></td>
      <td>Capture everything by value (copy). The lambda gets its own copy of each variable.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">[&amp;]</code></td>
      <td>Capture everything by reference. The lambda reads and writes the originals.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">[x]</code></td>
      <td>Capture only <code class="language-plaintext highlighter-rouge">x</code>, by value.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">[&amp;x]</code></td>
      <td>Capture only <code class="language-plaintext highlighter-rouge">x</code>, by reference.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">[x, &amp;y]</code></td>
      <td><code class="language-plaintext highlighter-rouge">x</code> by value, <code class="language-plaintext highlighter-rouge">y</code> by reference.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">[=, &amp;x]</code></td>
      <td>Everything by value, except <code class="language-plaintext highlighter-rouge">x</code> which is by reference.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">[this]</code></td>
      <td>Capture the <code class="language-plaintext highlighter-rouge">this</code> pointer (used inside class methods).</td>
    </tr>
  </tbody>
</table>

<p><strong>Capture by value vs. by reference</strong></p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">20</span><span class="p">;</span>

<span class="c1">// By value — lambda gets copies; originals are unchanged</span>
<span class="k">auto</span> <span class="n">byVal</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">]()</span> <span class="p">{</span> <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">;</span> <span class="p">};</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">byVal</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 30</span>

<span class="c1">// By reference — lambda modifies the original variable</span>
<span class="k">auto</span> <span class="n">byRef</span> <span class="o">=</span> <span class="p">[</span><span class="o">&amp;</span><span class="n">x</span><span class="p">]()</span> <span class="p">{</span> <span class="n">x</span> <span class="o">+=</span> <span class="mi">5</span><span class="p">;</span> <span class="p">};</span>
<span class="n">byRef</span><span class="p">();</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">x</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 15</span>
</code></pre></div></div>

<blockquote>
  <p><strong>Rule of thumb:</strong> capture by <strong>value</strong> when the lambda outlives the variable (e.g. stored in a callback). Capture by <strong>reference</strong> for short-lived lambdas used immediately.</p>
</blockquote>

<hr />

<h2 id="4-the-mutable-keyword">4. The <code class="language-plaintext highlighter-rouge">mutable</code> Keyword</h2>

<p>By default, variables captured by value are <code class="language-plaintext highlighter-rouge">const</code> inside the lambda — you cannot modify them. The <code class="language-plaintext highlighter-rouge">mutable</code> keyword lifts this restriction, allowing the lambda to modify its local copy (not the original).</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">int</span> <span class="n">counter</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

<span class="c1">// Without mutable — compile error: counter is const</span>
<span class="c1">// auto f = [counter]() { counter++; };</span>

<span class="c1">// With mutable — modifies the local copy only</span>
<span class="k">auto</span> <span class="n">f</span> <span class="o">=</span> <span class="p">[</span><span class="n">counter</span><span class="p">]()</span> <span class="k">mutable</span> <span class="p">{</span>
    <span class="n">counter</span><span class="o">++</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">counter</span><span class="p">;</span>
<span class="p">};</span>

<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">f</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>       <span class="c1">// 1</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">f</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>       <span class="c1">// 2</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">counter</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>   <span class="c1">// 0  — original unchanged</span>
</code></pre></div></div>

<hr />

<h2 id="5-storing-and-passing-lambdas">5. Storing and Passing Lambdas</h2>

<p>Lambdas can be stored in variables and passed around like any other value. There are two common approaches.</p>

<p><strong><code class="language-plaintext highlighter-rouge">auto</code> — for local storage</strong></p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">auto</span> <span class="n">multiply</span> <span class="o">=</span> <span class="p">[](</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">a</span> <span class="o">*</span> <span class="n">b</span><span class="p">;</span> <span class="p">};</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">multiply</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 20</span>
</code></pre></div></div>

<p><strong><code class="language-plaintext highlighter-rouge">std::function</code> — for flexible storage and passing</strong></p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;functional&gt;</span><span class="cp">
</span>
<span class="c1">// Store as a typed function object</span>
<span class="n">std</span><span class="o">::</span><span class="n">function</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">op</span><span class="p">;</span>

<span class="n">op</span> <span class="o">=</span> <span class="p">[](</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">;</span> <span class="p">};</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">op</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 7</span>

<span class="n">op</span> <span class="o">=</span> <span class="p">[](</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">a</span> <span class="o">*</span> <span class="n">b</span><span class="p">;</span> <span class="p">};</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">op</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 12</span>

<span class="c1">// Pass a lambda to a function</span>
<span class="kt">void</span> <span class="n">apply</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">function</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">f</span><span class="p">,</span> <span class="kt">int</span> <span class="n">val</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">f</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>
<span class="p">}</span>

<span class="n">apply</span><span class="p">([](</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span><span class="p">;</span> <span class="p">},</span> <span class="mi">6</span><span class="p">);</span>  <span class="c1">// 36</span>
</code></pre></div></div>

<blockquote>
  <p><strong>Note:</strong> <code class="language-plaintext highlighter-rouge">auto</code> is faster (no overhead); <code class="language-plaintext highlighter-rouge">std::function</code> is necessary when the lambda must be stored in a class member, a container, or passed across translation units.</p>
</blockquote>

<hr />

<h2 id="6-lambdas-with-stl-algorithms">6. Lambdas with STL Algorithms</h2>

<p>This is where lambdas truly shine. Every STL algorithm that accepts a callable becomes dramatically more readable with a lambda.</p>

<p><strong><code class="language-plaintext highlighter-rouge">std::sort</code> — custom comparator</strong></p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">v</span> <span class="o">=</span> <span class="p">{</span><span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">9</span><span class="p">};</span>

<span class="c1">// Descending order</span>
<span class="n">std</span><span class="o">::</span><span class="n">sort</span><span class="p">(</span><span class="n">v</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">v</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span>
    <span class="p">[](</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">a</span> <span class="o">&gt;</span> <span class="n">b</span><span class="p">;</span> <span class="p">});</span>
<span class="c1">// v = {9, 8, 5, 2, 1}</span>
</code></pre></div></div>

<p><strong><code class="language-plaintext highlighter-rouge">std::find_if</code> — find first match</strong></p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">nums</span> <span class="o">=</span> <span class="p">{</span><span class="mi">3</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">4</span><span class="p">};</span>

<span class="k">auto</span> <span class="n">it</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">find_if</span><span class="p">(</span><span class="n">nums</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">nums</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span>
    <span class="p">[](</span><span class="kt">int</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">n</span> <span class="o">&gt;</span> <span class="mi">5</span><span class="p">;</span> <span class="p">});</span>

<span class="k">if</span> <span class="p">(</span><span class="n">it</span> <span class="o">!=</span> <span class="n">nums</span><span class="p">.</span><span class="n">end</span><span class="p">())</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"First &gt; 5: "</span> <span class="o">&lt;&lt;</span> <span class="o">*</span><span class="n">it</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 7</span>
</code></pre></div></div>

<p><strong><code class="language-plaintext highlighter-rouge">std::transform</code> — map over a vector</strong></p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">nums</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">};</span>
<span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">squares</span><span class="p">;</span>

<span class="n">std</span><span class="o">::</span><span class="n">transform</span><span class="p">(</span><span class="n">nums</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">nums</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span>
    <span class="n">std</span><span class="o">::</span><span class="n">back_inserter</span><span class="p">(</span><span class="n">squares</span><span class="p">),</span>
    <span class="p">[](</span><span class="kt">int</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">n</span> <span class="o">*</span> <span class="n">n</span><span class="p">;</span> <span class="p">});</span>
<span class="c1">// squares = {1, 4, 9, 16, 25}</span>
</code></pre></div></div>

<p><strong><code class="language-plaintext highlighter-rouge">std::copy_if</code> — filter elements</strong></p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">nums</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">};</span>
<span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">evens</span><span class="p">;</span>

<span class="n">std</span><span class="o">::</span><span class="n">copy_if</span><span class="p">(</span><span class="n">nums</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">nums</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span>
    <span class="n">std</span><span class="o">::</span><span class="n">back_inserter</span><span class="p">(</span><span class="n">evens</span><span class="p">),</span>
    <span class="p">[](</span><span class="kt">int</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">;</span> <span class="p">});</span>
<span class="c1">// evens = {2, 4, 6}</span>
</code></pre></div></div>

<p><strong><code class="language-plaintext highlighter-rouge">std::accumulate</code> — reduce / fold</strong></p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;numeric&gt;</span><span class="cp">
</span>
<span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">nums</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">};</span>

<span class="kt">int</span> <span class="n">sum</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">accumulate</span><span class="p">(</span><span class="n">nums</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">nums</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span> <span class="mi">0</span><span class="p">,</span>
    <span class="p">[](</span><span class="kt">int</span> <span class="n">acc</span><span class="p">,</span> <span class="kt">int</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">acc</span> <span class="o">+</span> <span class="n">n</span><span class="p">;</span> <span class="p">});</span>
<span class="c1">// sum = 15</span>
</code></pre></div></div>

<hr />

<h2 id="7-closures--lambda-returning-lambda">7. Closures — Lambda Returning Lambda</h2>

<p>A closure is a lambda that captures its environment and returns another lambda. This is a powerful pattern for creating configurable, reusable function objects.</p>

<p><strong>Factory function example</strong></p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// makeMultiplier returns a NEW lambda each time</span>
<span class="k">auto</span> <span class="nf">makeMultiplier</span><span class="p">(</span><span class="kt">int</span> <span class="n">factor</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="p">[</span><span class="n">factor</span><span class="p">](</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="n">x</span> <span class="o">*</span> <span class="n">factor</span><span class="p">;</span>  <span class="c1">// factor is captured</span>
    <span class="p">};</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">auto</span> <span class="n">triple</span> <span class="o">=</span> <span class="n">makeMultiplier</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span>
    <span class="k">auto</span> <span class="n">byTen</span>  <span class="o">=</span> <span class="n">makeMultiplier</span><span class="p">(</span><span class="mi">10</span><span class="p">);</span>

    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">triple</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>            <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 15</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">byTen</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>             <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 50</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">triple</span><span class="p">(</span><span class="n">byTen</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>     <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 60</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<p>Each call to <code class="language-plaintext highlighter-rouge">makeMultiplier</code> creates an independent lambda with its own captured copy of <code class="language-plaintext highlighter-rouge">factor</code>. This pattern is the C++ equivalent of partial application in functional programming.</p>

<hr />

<h2 id="8-recursive-lambda">8. Recursive Lambda</h2>

<p>A lambda cannot refer to itself by its own name inside its body — it does not have one. The solution is to use <code class="language-plaintext highlighter-rouge">std::function</code>, which allows the lambda to refer to a named variable.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;functional&gt;</span><span class="cp">
</span>
<span class="n">std</span><span class="o">::</span><span class="n">function</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">factorial</span> <span class="o">=</span> <span class="p">[](</span><span class="kt">int</span> <span class="n">n</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">int</span> <span class="p">{</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">)</span> <span class="o">?</span> <span class="mi">1</span> <span class="o">:</span> <span class="n">n</span> <span class="o">*</span> <span class="n">factorial</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span>
<span class="p">};</span>

<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">factorial</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 720</span>
</code></pre></div></div>

<blockquote>
  <p><strong>Important:</strong> the variable <code class="language-plaintext highlighter-rouge">factorial</code> must be declared with <code class="language-plaintext highlighter-rouge">std::function</code>, not <code class="language-plaintext highlighter-rouge">auto</code>, because at the time the lambda is defined, its own type is not yet known.</p>
</blockquote>

<hr />

<h2 id="9-complete-example-with-main">9. Complete Example with <code class="language-plaintext highlighter-rouge">main()</code></h2>

<p>The following program demonstrates all major concepts in a single, self-contained example.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
#include</span> <span class="cpf">&lt;vector&gt;</span><span class="cp">
#include</span> <span class="cpf">&lt;algorithm&gt;</span><span class="cp">
#include</span> <span class="cpf">&lt;numeric&gt;</span><span class="cp">
#include</span> <span class="cpf">&lt;functional&gt;</span><span class="cp">
</span>
<span class="k">auto</span> <span class="nf">makeAdder</span><span class="p">(</span><span class="kt">int</span> <span class="n">base</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="p">[</span><span class="n">base</span><span class="p">](</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">base</span> <span class="o">+</span> <span class="n">x</span><span class="p">;</span> <span class="p">};</span>
<span class="p">}</span>

<span class="n">std</span><span class="o">::</span><span class="n">function</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">factorial</span> <span class="o">=</span> <span class="p">[](</span><span class="kt">int</span> <span class="n">n</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">int</span> <span class="p">{</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">)</span> <span class="o">?</span> <span class="mi">1</span> <span class="o">:</span> <span class="n">n</span> <span class="o">*</span> <span class="n">factorial</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span>
<span class="p">};</span>

<span class="kt">int</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="c1">// 1. Basic lambda</span>
    <span class="k">auto</span> <span class="n">greet</span> <span class="o">=</span> <span class="p">[]()</span> <span class="p">{</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"Hello, Lambda!</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span> <span class="p">};</span>
    <span class="n">greet</span><span class="p">();</span>

    <span class="c1">// 2. Lambda with parameters</span>
    <span class="k">auto</span> <span class="n">add</span> <span class="o">=</span> <span class="p">[](</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">;</span> <span class="p">};</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"3+4="</span> <span class="o">&lt;&lt;</span> <span class="n">add</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>

    <span class="c1">// 3. Capture by reference</span>
    <span class="kt">int</span> <span class="n">counter</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="k">auto</span> <span class="n">inc</span> <span class="o">=</span> <span class="p">[</span><span class="o">&amp;</span><span class="n">counter</span><span class="p">]()</span> <span class="p">{</span> <span class="n">counter</span><span class="o">++</span><span class="p">;</span> <span class="p">};</span>
    <span class="n">inc</span><span class="p">();</span> <span class="n">inc</span><span class="p">();</span> <span class="n">inc</span><span class="p">();</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"counter="</span> <span class="o">&lt;&lt;</span> <span class="n">counter</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 3</span>

    <span class="c1">// 4. std::sort with lambda comparator</span>
    <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">v</span> <span class="o">=</span> <span class="p">{</span><span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">3</span><span class="p">};</span>
    <span class="n">std</span><span class="o">::</span><span class="n">sort</span><span class="p">(</span><span class="n">v</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">v</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span> <span class="p">[](</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="n">b</span><span class="p">){</span> <span class="k">return</span> <span class="n">a</span> <span class="o">&gt;</span> <span class="n">b</span><span class="p">;</span> <span class="p">});</span>
    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">x</span> <span class="o">:</span> <span class="n">v</span><span class="p">)</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">x</span> <span class="o">&lt;&lt;</span> <span class="s">" "</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 9 8 5 3 2 1</span>

    <span class="c1">// 5. std::transform — squares</span>
    <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">nums</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">};</span>
    <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">sq</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">transform</span><span class="p">(</span><span class="n">nums</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">nums</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span> <span class="n">std</span><span class="o">::</span><span class="n">back_inserter</span><span class="p">(</span><span class="n">sq</span><span class="p">),</span>
        <span class="p">[](</span><span class="kt">int</span> <span class="n">n</span><span class="p">){</span> <span class="k">return</span> <span class="n">n</span> <span class="o">*</span> <span class="n">n</span><span class="p">;</span> <span class="p">});</span>
    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">x</span> <span class="o">:</span> <span class="n">sq</span><span class="p">)</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">x</span> <span class="o">&lt;&lt;</span> <span class="s">" "</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 1 4 9 16 25</span>

    <span class="c1">// 6. std::accumulate — sum</span>
    <span class="kt">int</span> <span class="n">sum</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">accumulate</span><span class="p">(</span><span class="n">nums</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">nums</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span> <span class="mi">0</span><span class="p">,</span>
        <span class="p">[](</span><span class="kt">int</span> <span class="n">acc</span><span class="p">,</span> <span class="kt">int</span> <span class="n">n</span><span class="p">){</span> <span class="k">return</span> <span class="n">acc</span> <span class="o">+</span> <span class="n">n</span><span class="p">;</span> <span class="p">});</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"sum="</span> <span class="o">&lt;&lt;</span> <span class="n">sum</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 15</span>

    <span class="c1">// 7. Closure — lambda returning lambda</span>
    <span class="k">auto</span> <span class="n">add10</span> <span class="o">=</span> <span class="n">makeAdder</span><span class="p">(</span><span class="mi">10</span><span class="p">);</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"add10(7)="</span> <span class="o">&lt;&lt;</span> <span class="n">add10</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 17</span>

    <span class="c1">// 8. Recursive lambda</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"5!="</span> <span class="o">&lt;&lt;</span> <span class="n">factorial</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span>  <span class="c1">// 120</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<p><strong>Expected output</strong></p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Hello, Lambda!
3+4=7
counter=3
9 8 5 3 2 1
1 4 9 16 25
sum=15
add10(7)=17
5!=120
</code></pre></div></div>

<hr />

<h2 id="10-best-practices">10. Best Practices</h2>

<ul>
  <li>Use <code class="language-plaintext highlighter-rouge">auto</code> to store lambdas locally. Reserve <code class="language-plaintext highlighter-rouge">std::function</code> only when you need to store or pass them across boundaries.</li>
  <li>Prefer <strong>specific captures</strong> (<code class="language-plaintext highlighter-rouge">[x, &amp;y]</code>) over blanket captures (<code class="language-plaintext highlighter-rouge">[=]</code>, <code class="language-plaintext highlighter-rouge">[&amp;]</code>). Explicit captures make dependencies obvious.</li>
  <li>Avoid capturing <code class="language-plaintext highlighter-rouge">[&amp;]</code> in lambdas that outlive the current scope — dangling references cause undefined behaviour.</li>
  <li>Keep lambda bodies <strong>short</strong>. If the body exceeds ~5 lines, consider extracting it into a named function.</li>
  <li>Use <code class="language-plaintext highlighter-rouge">mutable</code> sparingly. If you need to mutate a lot of state, a regular function or a functor is likely clearer.</li>
  <li>For <strong>recursive lambdas</strong>, always use <code class="language-plaintext highlighter-rouge">std::function</code> — <code class="language-plaintext highlighter-rouge">auto</code> cannot refer to itself.</li>
</ul>

<hr />

<h2 id="11-quick-reference">11. Quick Reference</h2>

<table>
  <thead>
    <tr>
      <th>Feature</th>
      <th>Syntax / Example</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Basic lambda</td>
      <td><code class="language-plaintext highlighter-rouge">auto f = []() { ... };</code></td>
    </tr>
    <tr>
      <td>With parameters</td>
      <td><code class="language-plaintext highlighter-rouge">auto f = [](int a, int b) { return a+b; };</code></td>
    </tr>
    <tr>
      <td>Explicit return type</td>
      <td><code class="language-plaintext highlighter-rouge">auto f = [](double x) -&gt; double { return x; };</code></td>
    </tr>
    <tr>
      <td>Capture by value</td>
      <td><code class="language-plaintext highlighter-rouge">[x, y]</code> — copies of x and y</td>
    </tr>
    <tr>
      <td>Capture by reference</td>
      <td><code class="language-plaintext highlighter-rouge">[&amp;x, &amp;y]</code> — references to x and y</td>
    </tr>
    <tr>
      <td>Capture all by value</td>
      <td><code class="language-plaintext highlighter-rouge">[=]</code></td>
    </tr>
    <tr>
      <td>Capture all by reference</td>
      <td><code class="language-plaintext highlighter-rouge">[&amp;]</code></td>
    </tr>
    <tr>
      <td>mutable</td>
      <td><code class="language-plaintext highlighter-rouge">[x]() mutable { x++; }</code> — modifies local copy</td>
    </tr>
    <tr>
      <td>Store in std::function</td>
      <td><code class="language-plaintext highlighter-rouge">std::function&lt;int(int)&gt; f = [](int x){ return x; };</code></td>
    </tr>
    <tr>
      <td>Sort comparator</td>
      <td><code class="language-plaintext highlighter-rouge">std::sort(v.begin(), v.end(), [](int a, int b){ return a&gt;b; });</code></td>
    </tr>
    <tr>
      <td>Transform</td>
      <td><code class="language-plaintext highlighter-rouge">std::transform(... [](int n){ return n*n; });</code></td>
    </tr>
    <tr>
      <td>Filter</td>
      <td><code class="language-plaintext highlighter-rouge">std::copy_if(... [](int n){ return n%2==0; });</code></td>
    </tr>
    <tr>
      <td>Reduce</td>
      <td><code class="language-plaintext highlighter-rouge">std::accumulate(... 0, [](int acc, int n){ return acc+n; });</code></td>
    </tr>
    <tr>
      <td>Closure factory</td>
      <td><code class="language-plaintext highlighter-rouge">auto make = [](int x){ return [x](int y){ return x+y; }; };</code></td>
    </tr>
    <tr>
      <td>Recursive lambda</td>
      <td><code class="language-plaintext highlighter-rouge">std::function&lt;int(int)&gt; f = [](int n){ return n&lt;=1?1:n*f(n-1); };</code></td>
    </tr>
  </tbody>
</table>]]></content><author><name></name></author><summary type="html"><![CDATA[An introductory course — C++11 and beyond Covers: syntax capture STL algorithms closures best practices]]></summary></entry><entry><title type="html">Fibonacci Function in Rust</title><link href="/lectures//lectures/2026/04/10/fibonacci-in-rust.html" rel="alternate" type="text/html" title="Fibonacci Function in Rust" /><published>2026-04-10T00:00:00+00:00</published><updated>2026-04-10T00:00:00+00:00</updated><id>/lectures//lectures/2026/04/10/fibonacci-in-rust</id><content type="html" xml:base="/lectures//lectures/2026/04/10/fibonacci-in-rust.html"><![CDATA[<p>The following function calculates the Fibonacci number at a given position <code class="language-plaintext highlighter-rouge">n</code>:</p>

<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">fn</span> <span class="nf">fibonacci</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">i32</span><span class="p">)</span> <span class="k">-&gt;</span> <span class="nb">i32</span> <span class="p">{</span>
    <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0i32</span> <span class="p">{</span>
        <span class="k">return</span> <span class="mi">0i32</span><span class="p">;</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1i32</span> <span class="p">{</span>
        <span class="k">return</span> <span class="mi">1i32</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">let</span> <span class="k">mut</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">0i32</span><span class="p">;</span>
    <span class="k">let</span> <span class="k">mut</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">1i32</span><span class="p">;</span>

    <span class="k">for</span> <span class="n">_</span> <span class="k">in</span> <span class="mi">2i32</span><span class="o">..=</span><span class="n">n</span> <span class="p">{</span>
        <span class="k">let</span> <span class="n">temp</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">;</span>
        <span class="n">a</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span>
        <span class="n">b</span> <span class="o">=</span> <span class="n">temp</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="n">b</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">let</span> <span class="n">n</span> <span class="o">=</span> <span class="mi">10i32</span><span class="p">;</span>
    <span class="nd">println!</span><span class="p">(</span><span class="s">"Fibonacci({}) = {}"</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="nf">fibonacci</span><span class="p">(</span><span class="n">n</span><span class="p">));</span>
<span class="p">}</span>
</code></pre></div></div>

<h3 id="explanation">Explanation:</h3>
<ul>
  <li>If <code class="language-plaintext highlighter-rouge">n</code> is <code class="language-plaintext highlighter-rouge">0</code>, the function returns <code class="language-plaintext highlighter-rouge">0</code>.</li>
  <li>If <code class="language-plaintext highlighter-rouge">n</code> is <code class="language-plaintext highlighter-rouge">1</code>, the function returns <code class="language-plaintext highlighter-rouge">1</code>.</li>
  <li>Otherwise, it initializes two variables, <code class="language-plaintext highlighter-rouge">a</code> and <code class="language-plaintext highlighter-rouge">b</code>, with the first two Fibonacci numbers (<code class="language-plaintext highlighter-rouge">0</code> and <code class="language-plaintext highlighter-rouge">1</code>).</li>
  <li>It iterates from <code class="language-plaintext highlighter-rouge">2</code> to <code class="language-plaintext highlighter-rouge">n</code>, updating the values to calculate the Fibonacci sequence iteratively.</li>
  <li>Finally, it returns the <code class="language-plaintext highlighter-rouge">n</code>-th Fibonacci number.</li>
</ul>

<h2 id="printing-the-fibonacci-sequence-up-to-a-limit">Printing the Fibonacci Sequence up to a Limit</h2>

<p>The following function prints the Fibonacci sequence up to a given number <code class="language-plaintext highlighter-rouge">n</code>:</p>

<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">fn</span> <span class="nf">fibonacci_sequence</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">i32</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">let</span> <span class="k">mut</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">0i32</span><span class="p">;</span>
    <span class="k">let</span> <span class="k">mut</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">1i32</span><span class="p">;</span>

    <span class="nd">print!</span><span class="p">(</span><span class="s">"Fibonacci sequence up to {}: {} {}"</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">);</span>

    <span class="k">while</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="o">&lt;=</span> <span class="n">n</span> <span class="p">{</span>
        <span class="k">let</span> <span class="n">temp</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">;</span>
        <span class="nd">print!</span><span class="p">(</span><span class="s">" {}"</span><span class="p">,</span> <span class="n">temp</span><span class="p">);</span>
        <span class="n">a</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span>
        <span class="n">b</span> <span class="o">=</span> <span class="n">temp</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="nd">println!</span><span class="p">();</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">let</span> <span class="n">n</span> <span class="o">=</span> <span class="mi">50i32</span><span class="p">;</span>
    <span class="nf">fibonacci_sequence</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>

<h3 id="explanation-1">Explanation:</h3>
<ul>
  <li>Initializes <code class="language-plaintext highlighter-rouge">a</code> and <code class="language-plaintext highlighter-rouge">b</code> as <code class="language-plaintext highlighter-rouge">0</code> and <code class="language-plaintext highlighter-rouge">1</code>, the first two Fibonacci numbers.</li>
  <li>Prints the initial values.</li>
  <li>Uses a <code class="language-plaintext highlighter-rouge">while</code> loop to generate and print Fibonacci numbers until the sum exceeds <code class="language-plaintext highlighter-rouge">n</code>.</li>
  <li>This efficiently prints all Fibonacci numbers up to <code class="language-plaintext highlighter-rouge">n</code>.</li>
</ul>]]></content><author><name></name></author><summary type="html"><![CDATA[The following function calculates the Fibonacci number at a given position n:]]></summary></entry><entry><title type="html">Declaring Variables in Rust</title><link href="/lectures//lectures/2026/01/11/declaring-vars-rust.html" rel="alternate" type="text/html" title="Declaring Variables in Rust" /><published>2026-01-11T00:00:00+00:00</published><updated>2026-01-11T00:00:00+00:00</updated><id>/lectures//lectures/2026/01/11/declaring-vars-rust</id><content type="html" xml:base="/lectures//lectures/2026/01/11/declaring-vars-rust.html"><![CDATA[<h1 id="declaring-variables-in-rust">Declaring Variables in Rust</h1>

<p>In Rust, you can declare variables in various ways:</p>

<h2 id="1-implicit-type-declaration">1. Implicit Type Declaration</h2>

<p>Rust deduces the type of a variable based on the assigned value. In the following example, Rust infers that <code class="language-plaintext highlighter-rouge">a</code> is of type <code class="language-plaintext highlighter-rouge">i32</code> because <code class="language-plaintext highlighter-rouge">10</code> is an integer, and <code class="language-plaintext highlighter-rouge">i32</code> is the default type for integer values:</p>

<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">let</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
</code></pre></div></div>

<h2 id="2-explicit-type-specification">2. Explicit Type Specification</h2>

<p>You can explicitly specify the type of a variable:</p>

<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">let</span> <span class="n">a</span><span class="p">:</span> <span class="nb">i32</span> <span class="o">=</span> <span class="mi">20</span><span class="p">;</span>
</code></pre></div></div>

<p>Here, the type is explicitly defined as <code class="language-plaintext highlighter-rouge">i32</code> for variable <code class="language-plaintext highlighter-rouge">a</code>.</p>

<h2 id="3-adding-a-type-suffix">3. Adding a Type Suffix</h2>

<p>You can append a type suffix to a numeric literal to specify its type explicitly:</p>

<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">let</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">20i32</span><span class="p">;</span>
</code></pre></div></div>

<p>In this case, <code class="language-plaintext highlighter-rouge">20i32</code> explicitly defines <code class="language-plaintext highlighter-rouge">b</code> as an <code class="language-plaintext highlighter-rouge">i32</code>. This method ensures that Rust interprets the literal as the specified type.</p>

<h2 id="4-using-a-digit-separator">4. Using a Digit Separator</h2>

<p>Rust allows the use of an underscore (<code class="language-plaintext highlighter-rouge">_</code>) as a separator in numeric literals to improve readability:</p>

<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">let</span> <span class="n">d</span> <span class="o">=</span> <span class="mi">20_000_i32</span><span class="p">;</span>
</code></pre></div></div>

<p>The underscore does not affect the numerical value; it simply enhances readability, especially for large numbers.</p>

<p>By using these various methods, Rust provides flexibility and clarity when declaring variables.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Declaring Variables in Rust]]></summary></entry><entry><title type="html">Overflow in cpp</title><link href="/lectures//lectures/2025/01/10/overflow-cpp.html" rel="alternate" type="text/html" title="Overflow in cpp" /><published>2025-01-10T00:00:00+00:00</published><updated>2025-01-10T00:00:00+00:00</updated><id>/lectures//lectures/2025/01/10/overflow-cpp</id><content type="html" xml:base="/lectures//lectures/2025/01/10/overflow-cpp.html"><![CDATA[<h1 id="overflow-in-c">Overflow in C++</h1>

<h2 id="integer-overflow">Integer Overflow</h2>
<p>Integer overflow occurs when an arithmetic operation results in a value exceeding the storage capacity of the integer type. In C++, signed integer overflow leads to <strong>undefined behavior</strong>.</p>

<h3 id="example">Example:</h3>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
#include</span> <span class="cpf">&lt;limits&gt;</span><span class="cp">
</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">max</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">numeric_limits</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;::</span><span class="n">max</span><span class="p">();</span> <span class="c1">// 2,147,483,647 for 32-bit int</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"Max int: "</span> <span class="o">&lt;&lt;</span> <span class="n">max</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>

    <span class="kt">int</span> <span class="n">overflow</span> <span class="o">=</span> <span class="n">max</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span>  <span class="c1">// Undefined behavior</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"Overflow int: "</span> <span class="o">&lt;&lt;</span> <span class="n">overflow</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<h4 id="possible-output">Possible Output:</h4>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Max int: 2147483647
Overflow int: -2147483648 (depends on the system)
</code></pre></div></div>
<p>The result wraps around due to <strong>two’s complement representation</strong>, but since signed integer overflow is undefined behavior, the actual result is unpredictable.</p>

<h2 id="unsigned-integer-overflow">Unsigned Integer Overflow</h2>
<p>For <strong>unsigned integers</strong>, overflow behavior is well-defined and follows <strong>modular arithmetic</strong>.</p>

<h3 id="example-1">Example:</h3>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
#include</span> <span class="cpf">&lt;limits&gt;</span><span class="cp">
</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">max</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">numeric_limits</span><span class="o">&lt;</span><span class="kt">unsigned</span> <span class="kt">int</span><span class="o">&gt;::</span><span class="n">max</span><span class="p">();</span> <span class="c1">// 4,294,967,295 for 32-bit unsigned int</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"Max unsigned int: "</span> <span class="o">&lt;&lt;</span> <span class="n">max</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>

    <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">overflow</span> <span class="o">=</span> <span class="n">max</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span>  <span class="c1">// Wraps around to 0</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"Overflow unsigned int: "</span> <span class="o">&lt;&lt;</span> <span class="n">overflow</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<h4 id="output">Output:</h4>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Max unsigned int: 4294967295
Overflow unsigned int: 0
</code></pre></div></div>
<p>Unlike signed integers, unsigned integer overflow is <strong>defined behavior</strong> in C++.</p>

<h2 id="floating-point-overflow">Floating-Point Overflow</h2>
<p>Floating-point overflow occurs when a value exceeds the representable range of the floating-point type. Instead of wrapping around or causing undefined behavior, the result becomes <strong>infinity (inf)</strong>.</p>

<h3 id="example-2">Example:</h3>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
#include</span> <span class="cpf">&lt;limits&gt;</span><span class="cp">
</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="kt">float</span> <span class="n">large</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">numeric_limits</span><span class="o">&lt;</span><span class="kt">float</span><span class="o">&gt;::</span><span class="n">max</span><span class="p">();</span> <span class="c1">// ~3.4e+38</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"Max float: "</span> <span class="o">&lt;&lt;</span> <span class="n">large</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>

    <span class="kt">float</span> <span class="n">overflow</span> <span class="o">=</span> <span class="n">large</span> <span class="o">*</span> <span class="mf">10.0</span><span class="n">f</span><span class="p">;</span> <span class="c1">// Becomes infinity (inf)</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"Overflow float: "</span> <span class="o">&lt;&lt;</span> <span class="n">overflow</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<h4 id="output-1">Output:</h4>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Max float: 3.40282e+38
Overflow float: inf
</code></pre></div></div>
<p>For floating-point types, exceeding the maximum representable value results in <code class="language-plaintext highlighter-rouge">inf</code> instead of undefined behavior.</p>

<h2 id="preventing-overflow">Preventing Overflow</h2>
<p>To avoid overflow issues, consider:</p>
<ul>
  <li>Checking value limits using <code class="language-plaintext highlighter-rouge">std::numeric_limits&lt;T&gt;::max()</code>.</li>
  <li>Using <strong>wider data types</strong> (<code class="language-plaintext highlighter-rouge">long long</code>, <code class="language-plaintext highlighter-rouge">double</code>, etc.).</li>
  <li>Employing <strong>safe arithmetic libraries</strong> like <code class="language-plaintext highlighter-rouge">SafeInt</code> (Microsoft) or <code class="language-plaintext highlighter-rouge">boost::multiprecision</code>.</li>
  <li>Enabling compiler warnings or sanitizers (<code class="language-plaintext highlighter-rouge">-fsanitize=undefined</code> in Clang/GCC).</li>
</ul>

<h2 id="conclusion">Conclusion</h2>
<p>Overflow behavior in C++ differs based on the data type:</p>
<ul>
  <li><strong>Signed integer overflow</strong>: Undefined behavior.</li>
  <li><strong>Unsigned integer overflow</strong>: Modular arithmetic.</li>
  <li><strong>Floating-point overflow</strong>: Results in <code class="language-plaintext highlighter-rouge">inf</code>.</li>
</ul>

<p>Proper checks and safe practices are necessary to avoid unintended results in arithmetic operations.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Overflow in C++]]></summary></entry><entry><title type="html">Polynomial Derivatives</title><link href="/lectures//lectures/2025/01/02/polynomial-derivative.html" rel="alternate" type="text/html" title="Polynomial Derivatives" /><published>2025-01-02T00:00:00+00:00</published><updated>2025-01-02T00:00:00+00:00</updated><id>/lectures//lectures/2025/01/02/polynomial-derivative</id><content type="html" xml:base="/lectures//lectures/2025/01/02/polynomial-derivative.html"><![CDATA[<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>void readPolynom(int **p, int *n) {
    printf("Give the degree of the polynom -&gt; ");
    scanf("%d", n);

    // Allocate memory for coefficients (degree + 1 elements)
    *p = (int *)malloc((*n + 1) * sizeof(int));
    if (*p == NULL) {
        printf("Memory allocation failed!\n");
        exit(1);
    }

    for(int i = 0; i &lt;= *n; i++) {
        printf("P[%d] = ", i);
        scanf("%d", (*p + i));
    }
}

void writePolynom(int *p, int *n) {
    printf("p(x) = %dx^0", p[0]);
    for(int i = 1; i &lt;= *n; i++) {
        printf(" + %dx^%d", p[i], i);
    }
}

float valuePolynom(int *p, int *n, float x) {
    float value = p[0],
          aux = 1;

    for(int i = 1; i &lt;= *n; ++i) {
        aux = aux * x;
        value += aux * p[i];
    }

    return value;
}

void derivatePolynom(int *p, int *n) {
    for(int i = 0; i &lt; *n; i++) {
        p[i] = p[i + 1] * (i + 1);
    }
    (*n)--;
}

</code></pre></div></div>

<h1 id="polynomial-derivative-calculator">Polynomial Derivative Calculator</h1>

<p>A C program that handles polynomial operations including reading coefficients, calculating derivatives, and evaluating polynomials at specific points.</p>

<h2 id="features">Features</h2>

<ul>
  <li>Read polynomial coefficients dynamically</li>
  <li>Display polynomial in standard mathematical notation</li>
  <li>Calculate first derivative of polynomials</li>
  <li>Evaluate polynomials at given points</li>
  <li>Dynamic memory management for polynomial coefficients</li>
</ul>

<h2 id="technical-details">Technical Details</h2>

<h3 id="functions">Functions</h3>

<h4 id="readpolynomint-p-int-n"><code class="language-plaintext highlighter-rouge">readPolynom(int **p, int *n)</code></h4>
<ul>
  <li>Takes a pointer to pointer for coefficients array and pointer to degree</li>
  <li>Dynamically allocates memory for coefficients</li>
  <li>Reads polynomial degree and coefficients from user input</li>
  <li>Error handling for memory allocation failures</li>
</ul>

<h4 id="writepolynomint-p-int-n"><code class="language-plaintext highlighter-rouge">writePolynom(int *p, int *n)</code></h4>
<ul>
  <li>Displays polynomial in standard mathematical notation</li>
  <li>Format: ax^0 + bx^1 + cx^2 + …</li>
  <li>Takes coefficient array and degree as parameters</li>
</ul>

<h4 id="valuepolynomint-p-int-n-float-x"><code class="language-plaintext highlighter-rouge">valuePolynom(int *p, int *n, float x)</code></h4>
<ul>
  <li>Evaluates polynomial at a given point x</li>
  <li>Uses Horner’s method for efficient computation</li>
  <li>Returns float value of polynomial evaluation</li>
</ul>

<h4 id="derivatepolynomint-p-int-n"><code class="language-plaintext highlighter-rouge">derivatePolynom(int *p, int *n)</code></h4>
<ul>
  <li>Calculates first derivative of the polynomial</li>
  <li>Modifies original coefficient array</li>
  <li>Updates polynomial degree (reduces by 1)</li>
</ul>

<h2 id="usage">Usage</h2>

<ol>
  <li>Compile the program:
    <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>gcc polynomial.c <span class="nt">-o</span> polynomial
</code></pre></div>    </div>
  </li>
  <li>Run the executable:
    <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>./polynomial
</code></pre></div>    </div>
  </li>
  <li>Follow the prompts:
    <ul>
      <li>Enter the degree of polynomial</li>
      <li>Enter coefficients starting from constant term</li>
      <li>Program will display:
        <ul>
          <li>Original polynomial</li>
          <li>Its derivative</li>
          <li>Value at x = 1.450</li>
        </ul>
      </li>
    </ul>
  </li>
</ol>

<h2 id="example">Example</h2>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Give the degree of the polynom -&gt; 2
P[0] = 1
P[1] = 2
P[2] = 3

Original polynomial: p(x) = 1x^0 + 2x^1 + 3x^2
Derivative: p(x) = 2x^0 + 6x^1
p(1.450) = 8.273
</code></pre></div></div>

<h2 id="memory-management">Memory Management</h2>

<p>The program uses dynamic memory allocation to handle polynomials of any degree:</p>
<ul>
  <li>Memory is allocated using <code class="language-plaintext highlighter-rouge">malloc()</code></li>
  <li>Proper memory deallocation using <code class="language-plaintext highlighter-rouge">free()</code></li>
  <li>Includes error checking for allocation failures</li>
</ul>

<h2 id="technical-requirements">Technical Requirements</h2>

<ul>
  <li>C compiler (gcc recommended)</li>
  <li>Standard C libraries:
    <ul>
      <li>stdio.h</li>
      <li>stdlib.h</li>
    </ul>
  </li>
</ul>

<h2 id="error-handling">Error Handling</h2>

<p>The program includes basic error handling for:</p>
<ul>
  <li>Memory allocation failures</li>
  <li>Invalid input validation (basic)</li>
</ul>

<h2 id="limitations">Limitations</h2>

<ul>
  <li>Handles only integer coefficients</li>
  <li>Calculates only first derivative</li>
  <li>No input validation for polynomial degree limits</li>
</ul>]]></content><author><name></name></author><summary type="html"><![CDATA[``` void readPolynom(int **p, int *n) { printf(“Give the degree of the polynom -&gt; “); scanf(“%d”, n);]]></summary></entry><entry><title type="html">Viktor Frankl</title><link href="/lectures//lectures/2024/10/10/Viktor-Frankl.html" rel="alternate" type="text/html" title="Viktor Frankl" /><published>2024-10-10T00:00:00+00:00</published><updated>2024-10-10T00:00:00+00:00</updated><id>/lectures//lectures/2024/10/10/Viktor-Frankl</id><content type="html" xml:base="/lectures//lectures/2024/10/10/Viktor-Frankl.html"><![CDATA[<p>Viktor Frankl’s life is a remarkable journey of resilience, intellectual achievement, and compassion. His personal experiences, particularly during World War II, deeply influenced his development of <strong>logotherapy</strong> and his philosophical outlook on human existence. Here’s a detailed exploration of his life:</p>

<h3 id="early-life-and-education-19051930s">Early Life and Education (1905–1930s):</h3>
<p>Viktor Emil Frankl was born on <strong>March 26, 1905</strong>, in <strong>Vienna, Austria</strong>, into a Jewish family. He grew up in a modest, intellectual household where education was highly valued. His father worked as a government official, and Frankl’s early life was stable and nurturing.</p>

<p>From a young age, Frankl showed a keen interest in <strong>psychology and philosophy</strong>. While still in high school, he began corresponding with <strong>Sigmund Freud</strong>, the father of psychoanalysis. This early exposure to Freud’s ideas shaped his initial understanding of human psychology. However, as Frankl matured, he became more aligned with the ideas of <strong>Alfred Adler</strong>, Freud’s contemporary, and was attracted to the concept of individual psychology.</p>

<p>Frankl pursued <strong>medicine</strong> at the <strong>University of Vienna</strong> in the 1920s, where he specialized in <strong>neurology and psychiatry</strong>. He was deeply interested in existential philosophy and questions about the meaning of life, which would later become central themes in his work.</p>

<h3 id="developing-logotherapy-1930s1940s">Developing Logotherapy (1930s–1940s):</h3>
<p>During the 1930s, Frankl’s work focused on treating patients with <strong>depression</strong> and <strong>suicidal tendencies</strong>. He became the director of the <strong>Vienna Rothschild Hospital</strong> for mentally ill patients. In his practice, Frankl observed that many of his patients suffered not from clinical illnesses, but from a <strong>lack of meaning</strong> in their lives. This observation led him to develop the foundations of <strong>logotherapy</strong>, a new approach to psychotherapy that emphasizes the search for meaning as a fundamental human drive.</p>

<p>However, as the political situation in Europe deteriorated, Frankl’s life took a dramatic turn. In 1938, the Nazis annexed Austria, and soon after, Jews were stripped of their rights. Frankl, along with other Jewish professionals, was forbidden from practicing medicine. During this time, Frankl completed his <strong>doctoral dissertation</strong> on the philosophical concept of meaning in psychiatry.</p>

<h3 id="the-holocaust-and-concentration-camps-19421945">The Holocaust and Concentration Camps (1942–1945):</h3>
<p>In 1942, at the height of the Holocaust, Frankl, along with his family, was deported to the <strong>Theresienstadt ghetto</strong>. Later, he and his wife were sent to <strong>Auschwitz</strong>, and from there, Frankl was transferred to various other concentration camps, including <strong>Dachau</strong>.</p>

<p>During his time in the concentration camps, Frankl experienced the horrific brutality and inhumanity inflicted upon Jewish prisoners. Yet, it was in these darkest moments that his theories of logotherapy were tested and reinforced. Despite the unimaginable suffering, Frankl observed that those who were able to survive emotionally and psychologically were often those who could find <strong>meaning</strong>, even in suffering. He famously noted that <strong>“those who have a ‘why’ to live can bear almost any ‘how.’“</strong></p>

<p>In 1945, Frankl was liberated by the Allied forces. Tragically, his wife, parents, and brother perished in the Holocaust.</p>

<h3 id="post-war-work-and-writing-19461990s">Post-War Work and Writing (1946–1990s):</h3>
<p>After the war, Frankl returned to Vienna, where he resumed his work in psychiatry and published his groundbreaking book, <strong><em>Man’s Search for Meaning</em></strong> (originally titled <em>Ein Psychologe erlebt das Konzentrationslager</em> or “A Psychologist Experiences the Concentration Camp”). In this book, he recounted his concentration camp experiences and outlined the principles of logotherapy. It became an international bestseller, profoundly impacting readers and helping popularize Frankl’s ideas worldwide.</p>

<p>Frankl also returned to teaching and clinical practice. He became a <strong>professor of neurology and psychiatry</strong> at the <strong>University of Vienna</strong>, where he taught until his retirement in the 1970s. Throughout the following decades, he lectured widely across the world, becoming an influential figure in psychology, philosophy, and spirituality.</p>

<h3 id="later-life-and-legacy">Later Life and Legacy:</h3>
<p>In his later years, Frankl continued writing and lecturing. He authored more than <strong>30 books</strong> on psychology, existentialism, and spirituality, all centered around the core concept of finding meaning in life. He received numerous honorary doctorates and accolades for his contributions to psychology and humanistic thought.</p>

<p>Frankl’s influence extended beyond psychology into fields like <strong>existential philosophy</strong>, <strong>theology</strong>, and even business, where his ideas about purpose and resilience found wide application. Logotherapy became known as the “Third Viennese School of Psychotherapy,” after Freud’s psychoanalysis and Adler’s individual psychology.</p>

<p>Frankl passed away on <strong>September 2, 1997</strong>, at the age of 92, leaving behind a rich legacy that continues to inspire individuals in their search for meaning.</p>

<h3 id="major-contributions-and-influence">Major Contributions and Influence:</h3>
<ol>
  <li>
    <p><strong>Logotherapy and Existential Analysis</strong>: Frankl’s <strong>existential analysis</strong> focuses on the pursuit of meaning and the ability of individuals to find purpose even in suffering. His approach is still widely used in existential therapy, counseling, and crisis intervention.</p>
  </li>
  <li>
    <p><strong>Influence on Positive Psychology</strong>: Frankl’s ideas have been foundational for the development of <strong>positive psychology</strong>, particularly in the focus on meaning as essential for well-being. Psychologists like <strong>Martin Seligman</strong> have drawn from Frankl’s work in their exploration of what makes life worth living.</p>
  </li>
  <li>
    <p><strong>Human Resilience and Meaning</strong>: Frankl’s personal story of surviving the Holocaust while maintaining his belief in human dignity and freedom of choice in the face of suffering resonates with anyone facing adversity, reinforcing his notion that one can find meaning even in the direst of circumstances.</p>
  </li>
</ol>

<h3 id="viktor-frankls-enduring-message">Viktor Frankl’s enduring message:</h3>
<p>Frankl’s life and work serve as a profound reminder that <strong>the quest for meaning</strong> is universal and timeless. He proved, through his own experiences, that even when stripped of everything, humans can still find meaning, and through that meaning, maintain their humanity.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Viktor Frankl’s life is a remarkable journey of resilience, intellectual achievement, and compassion. His personal experiences, particularly during World War II, deeply influenced his development of logotherapy and his philosophical outlook on human existence. Here’s a detailed exploration of his life:]]></summary></entry><entry><title type="html">Men’s Search for Meaning</title><link href="/lectures//lectures/2024/10/08/mens-search-for-meaning.html" rel="alternate" type="text/html" title="Men’s Search for Meaning" /><published>2024-10-08T00:00:00+00:00</published><updated>2024-10-08T00:00:00+00:00</updated><id>/lectures//lectures/2024/10/08/mens-search-for-meaning</id><content type="html" xml:base="/lectures//lectures/2024/10/08/mens-search-for-meaning.html"><![CDATA[<p>Viktor Frankl’s “Man’s Search for Meaning” is a powerful memoir and psychological treatise based on his experiences as a prisoner in Nazi concentration camps during World War II. The book is divided into two parts: the first describing his time in the camps, and the second outlining his theory of logotherapy.
In the camps, Frankl observed that prisoners who maintained a sense of purpose and meaning in their lives were more likely to survive the horrific conditions. He realized that while they couldn’t control their circumstances, they could control their reactions to those circumstances. Frankl argues that the primary drive in human beings is not pleasure (as Freud believed) or power (as Adler thought), but the pursuit of meaning. He posits that life has meaning under all circumstances, even the most miserable ones.
The author identifies three main sources of meaning: purposeful work, love, and courage in the face of difficulty. In the camps, prisoners who could connect to a sense of future purpose - such as reuniting with loved ones or completing unfinished work - had a better chance of survival. Frankl introduces his theory of logotherapy, which focuses on the future and on a person’s search for meaning, rather than dwelling on past events or inner conflicts. The term “logos” is Greek for “meaning.” According to logotherapy, meaning can be discovered through three avenues: creating a work or doing a deed; experiencing something or encountering someone; and the attitude we take toward unavoidable suffering. Frankl emphasizes the importance of responsibility in finding meaning. He argues that we are responsible for giving meaning to our lives and for living up to our potential, regardless of our circumstances. The book discusses the concept of “tragic optimism,” which is the ability to remain optimistic and find meaning in life despite its inherent tragic nature. This includes the “tragic triad” of pain, guilt, and death. Frankl warns against the “existential vacuum,” a state of emptiness and meaninglessness that he believed was a widespread phenomenon of the 20th century. He saw this as the root of many societal problems, including depression, aggression, and addiction.
Ultimately, “Man’s Search for Meaning” is a testament to the resilience of the human spirit and the power of purpose in overcoming even the most horrific circumstances. It challenges readers to find meaning in their own lives and to take responsibility for their attitudes and actions, regardless of their situation.</p>

<p>Viktor Frankl’s “Man’s Search for Meaning” is a powerful memoir and psychological treatise based on his experiences as a prisoner in Nazi concentration camps during World War II. The book is divided into two parts:</p>
<ol>
  <li>Frankl’s experiences in the concentration camps</li>
  <li>An outline of his theory of logotherapy</li>
</ol>

<p>Key Observations: In the camps, Frankl observed that prisoners who maintained a sense of purpose and meaning in their lives were more likely to survive the horrific conditions. He realized that while they couldn’t control their circumstances, they could control their reactions to those circumstances.</p>

<p>Central Argument: Frankl argues that the primary drive in human beings is not pleasure (as Freud believed) or power (as Adler thought), but the pursuit of meaning. He posits that life has meaning under all circumstances, even the most miserable ones.
Frankl identifies three main sources of meaning:</p>
<ul>
  <li>Purposeful work</li>
  <li>Love</li>
  <li>Courage in the face of difficulty</li>
</ul>

<p>In the camps, prisoners who could connect to a sense of future purpose - such as reuniting with loved ones or completing unfinished work - had a better chance of survival.</p>

<h3 id="logotherapy">Logotherapy</h3>

<p>Frankl introduces his theory of logotherapy, which focuses on the future and on a person’s search for meaning, rather than dwelling on past events or inner conflicts. The term “logos” is Greek for “meaning.”</p>

<h3 id="discovering-meaning">Discovering Meaning</h3>

<p>According to logotherapy, meaning can be discovered through three avenues:</p>
<ol>
  <li>Creating a work or doing a deed</li>
  <li>Experiencing something or encountering someone</li>
  <li>The attitude we take toward unavoidable suffering</li>
</ol>

<h3 id="responsibility-and-meaning">Responsibility and Meaning</h3>

<p>Frankl emphasizes the importance of responsibility in finding meaning. He argues that we are responsible for giving meaning to our lives and for living up to our potential, regardless of our circumstances.</p>

<h3 id="tragic-optimism">Tragic Optimism</h3>

<p>The book discusses the concept of “tragic optimism,” which is the ability to remain optimistic and find meaning in life despite its inherent tragic nature. This includes the “tragic triad” of pain, guilt, and death.</p>

<h3 id="existential-vacuum">Existential Vacuum</h3>

<p>Frankl warns against the “existential vacuum,” a state of emptiness and meaninglessness that he believed was a widespread phenomenon of the 20th century. He saw this as the root of many societal problems, including depression, aggression, and addiction.</p>

<h3 id="conclusion">Conclusion</h3>

<p>Ultimately, “Man’s Search for Meaning” is a testament to the resilience of the human spirit and the power of purpose in overcoming even the most horrific circumstances. It challenges readers to find meaning in their own lives and to take responsibility for their attitudes and actions, regardless of their situation.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Viktor Frankl’s “Man’s Search for Meaning” is a powerful memoir and psychological treatise based on his experiences as a prisoner in Nazi concentration camps during World War II. The book is divided into two parts: the first describing his time in the camps, and the second outlining his theory of logotherapy. In the camps, Frankl observed that prisoners who maintained a sense of purpose and meaning in their lives were more likely to survive the horrific conditions. He realized that while they couldn’t control their circumstances, they could control their reactions to those circumstances. Frankl argues that the primary drive in human beings is not pleasure (as Freud believed) or power (as Adler thought), but the pursuit of meaning. He posits that life has meaning under all circumstances, even the most miserable ones. The author identifies three main sources of meaning: purposeful work, love, and courage in the face of difficulty. In the camps, prisoners who could connect to a sense of future purpose - such as reuniting with loved ones or completing unfinished work - had a better chance of survival. Frankl introduces his theory of logotherapy, which focuses on the future and on a person’s search for meaning, rather than dwelling on past events or inner conflicts. The term “logos” is Greek for “meaning.” According to logotherapy, meaning can be discovered through three avenues: creating a work or doing a deed; experiencing something or encountering someone; and the attitude we take toward unavoidable suffering. Frankl emphasizes the importance of responsibility in finding meaning. He argues that we are responsible for giving meaning to our lives and for living up to our potential, regardless of our circumstances. The book discusses the concept of “tragic optimism,” which is the ability to remain optimistic and find meaning in life despite its inherent tragic nature. This includes the “tragic triad” of pain, guilt, and death. Frankl warns against the “existential vacuum,” a state of emptiness and meaninglessness that he believed was a widespread phenomenon of the 20th century. He saw this as the root of many societal problems, including depression, aggression, and addiction. Ultimately, “Man’s Search for Meaning” is a testament to the resilience of the human spirit and the power of purpose in overcoming even the most horrific circumstances. It challenges readers to find meaning in their own lives and to take responsibility for their attitudes and actions, regardless of their situation.]]></summary></entry><entry><title type="html">Gödel’s Incompleteness Theorems</title><link href="/lectures//lectures/2024/10/07/G%C3%B6del-s-Incompleteness-Theorems.html" rel="alternate" type="text/html" title="Gödel’s Incompleteness Theorems" /><published>2024-10-07T00:00:00+00:00</published><updated>2024-10-07T00:00:00+00:00</updated><id>/lectures//lectures/2024/10/07/G%C3%B6del%E2%80%99s-Incompleteness-Theorems</id><content type="html" xml:base="/lectures//lectures/2024/10/07/G%C3%B6del-s-Incompleteness-Theorems.html"><![CDATA[<p>Have you ever thought that traditional mathematical reasoning could prove everything that is true about numbers? Or that we could prove, using traditional mathematical reasoning, that mathematics was free of contradictions? Well, think again. Gödel proved that these things are not possible. How did he do this?</p>

<h3 id="kurt-gödel-is-famous-for-the-following-two-theorems">Kurt Gödel is famous for the following two theorems:</h3>

<ol>
  <li>
    <p>Any formal system (with a finite axiom schema and a computationally enumerable set of theorems) able to do elementary arithmetic is either inconsistent or incomplete.</p>
  </li>
  <li>
    <p>Any formal system able to express its own consistency can prove its own consistency if and only if it is inconsistent.</p>
  </li>
</ol>]]></content><author><name></name></author><summary type="html"><![CDATA[Have you ever thought that traditional mathematical reasoning could prove everything that is true about numbers? Or that we could prove, using traditional mathematical reasoning, that mathematics was free of contradictions? Well, think again. Gödel proved that these things are not possible. How did he do this?]]></summary></entry></feed>