aboutsummaryrefslogtreecommitdiff
path: root/public/post/programming-as-art/index.html
blob: 73038177abd0f3e6e1b3430d24b3b6316fdbfaba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html lang="en">

    <head><title>Romanticism in Programming &ndash; Kumar Damani - Home</title>
<meta name="description" content="Git, email, resume etc.">

<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8"/>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/katex.min.css" integrity="sha384-vKruj+a13U8yHIkAyGgK1J3ArTLzrFGBbBc0tDp4ad/EyewESeXE/Iv67Aj8gKZ0" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/katex.min.js" integrity="sha384-PwRUT/YqbnEjkZO0zZxNqcxACrXe+j766U2amXcgMg5457rve2Y7I6ZJSm2A0mS4" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/contrib/auto-render.min.js" integrity="sha384-+VBxd3r6XgURycqtZ117nYw44OOcIax56Z4dCRWbxyPt0Koah1uHoK0o4+/RRE05" crossorigin="anonymous"
    onload="renderMathInElement(document.body);"></script>
<script>
    document.addEventListener("DOMContentLoaded", function() {
        renderMathInElement(document.body, {
            delimiters: [
                {left: "$$", right: "$$", display: true},
                {left: "$", right: "$", display: false}
            ]
        });
    });
</script>



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" integrity="sha512-1sCRPdkRXhBV2PBLUdRb4tMg1w2YPf37qatUFeS7zlBy7jJI8Lf4VHwWfZZfpXtYSLy85pkm9GaYVYMfw5BC1A==" crossorigin="anonymous" />


<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/academicons/1.9.1/css/academicons.min.css" integrity="sha512-b1ASx0WHgVFL5ZQhTgiPWX+68KjS38Jk87jg7pe+qC7q9YkEtFq0z7xCglv7qGIs/68d3mAp+StfC8WKC5SSAg==" crossorigin="anonymous" />


<link rel="stylesheet" href="https://kumardamani.net/css/palettes/gruvbox-dark.css">
<link rel="stylesheet" href="https://kumardamani.net/css/risotto.css">
<link rel="stylesheet" href="https://kumardamani.net/css/custom.css">
</head>

    <body>
        <div class="page">

            <header class="page__header"><h1 class="page__logo"><a href="https://kumardamani.net/" class="page__logo-inner">Kumar Damani - Home</a></h1>
<nav class="page__nav main-nav">
    <ul>
    
    
    <li class="main-nav__item"><a class="nav-main-item active" href="https://kumardamani.net/post/" title="Posts">Posts</a></li>
    
    </ul>
</nav>

</header>

            <section class="page__body">
    <header class="content__header">
        <h1>Romanticism in Programming</h1>
    </header>
    <div class="content__body">
        <p>Why there is art in programming.</p>
<p>During my early days as a CS student, one of the first mind-blowing moments was
watching <code>Hello World!</code> getting printed out to the console thousands of times
in just two functional lines of code.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#66d9ef">for</span> _ <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">1001</span>):
</span></span><span style="display:flex;"><span>  print(<span style="color:#e6db74">&#34;Hello World!&#34;</span>)
</span></span></code></pre></div><p>At the time it felt like having the <a href="https://harry-potter-compendium.fandom.com/wiki/Elder_Wand">Elder Wand</a>.
<img src="wand.png" alt="Dumbledore with Elder Wand pic"></p>
<p>But there was more to our lesson.
The TA then asks us to put our newly found power to use by computing the
sum of 1 to 100. Of course, it was a natural application of what we had just done earlier:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>sum <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">for</span> i <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">100</span><span style="color:#f92672">+</span><span style="color:#ae81ff">1</span>):
</span></span><span style="display:flex;"><span>  sum <span style="color:#f92672">=</span> sum <span style="color:#f92672">+</span> i
</span></span></code></pre></div><p>Sure enough we saw the answer <code>5050</code> in the console.
But then the TA reminds us that we are making our computers <strong>work too hard</strong>.
In other words, the computer needs to do one-hundred ADD instructions in order to make this computation happen.</p>
<p>What if the number was a million? How well would our method scale?</p>
<p>Well then it would take a million ADD instructions.
We call this scaling <em>linearly</em> with the input size.
Later we would formalize this to $\mathcal{O}(n)$ (pronounced: <em>Big Oh of N</em>).</p>
<p>The TA hinted that there is a better way, and that we already know of the better way in math.</p>
<p><img src="teaching.png" alt="Teaching meme"></p>
<p>$\displaystyle S_n = \sum\limits_{i=1}^n i = 1 + 2 + &hellip; + n = \frac{n (n + 1)}{2}$</p>
<p>With this we are no longer using loops, but a known mathematical fact about sequences.
If you don&rsquo;t belive me, see the <a href="https://letstalkscience.ca/educational-resources/backgrounders/gauss-summation">proof</a>.</p>
<p>Written as code:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>sum <span style="color:#f92672">=</span> n (n <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>) <span style="color:#f92672">/</span> <span style="color:#ae81ff">2</span>
</span></span></code></pre></div><p>This one-liner solves our problem with just 3 (ADD, MULTIPLY, DIVIDE) instructions.
Crucially, it does not depend on the size of the input like our previous solution, thus
<strong>no matter the input, it always takes 3 instructions to compute!</strong>
This is a HUGE win!</p>
<p><img src="math.png" alt="Math meme"></p>
<p>Later we would formalize this to $\mathcal{O}(1)$, or <em>constant</em> scaling.</p>
<blockquote>
<p>Yes, yes I know IRL the complier would optimize the loop solution
such that it does not take N instructions but for the purposes of
learning we were not allowed to depend on that.</p>
</blockquote>
<p>Looking back at it now,
both solutions are equally correct, and modern compilers would optimize the first solution
in the final instructions sent to the cpu, such that any performance differences would be negligible.
In other words, the computer wouldn&rsquo;t acutally be <em>working so hard</em>.</p>
<p>Objectively, the first solution is more readable, and friendly to a new observer than the second.</p>
<p><em>Why then am I still so drawn to the second solution?</em></p>
<p>The first solution reminds me of the saying &ldquo;to a hammer, eveything looks like a nail&rdquo;.
Its a brute force approach.
In comparison, the second solution is using the exact tool for our particular problem.
It somehow feels personalized and dare I say <em>romantic</em>.</p>
<p>When I reflect on moments like this, it reminds me that there is emergent elegance and beauty
even in the seemingly arbitrary sequence of symbols that is <code>code</code>.
<img src="code.png" alt="The Matrix code going by image"></p>
<p>Programming is not quite as <em>objective</em> as people would have you believe.
There are trade-offs to each solution, and which solution you prefer relect on the trade-offs
you are willing to accept, which varies by the observer: much like <em>art</em>.</p>
    </div>
    <footer class="content__footer"></footer>

            </section>

            <section class="page__aside">
                <div class="aside__about">
<div class="aside__about">
    <img class="about__logo" src="https://kumardamani.net/images/rice.svg" alt="Logo">
<h1 class="about__title">Links</h1>
<p class="about__description">Git, email, resume etc.</p>
</div>


<ul class="aside__social-links">
    
    <li>
        <a href="https://gitlab.com/kdam0/" rel="me" aria-label="GitLab" title="GitLab"><i class="fa-brands fa-gitlab" aria-hidden="true"></i></a>&nbsp;
    </li>
    
    <li>
        <a href="mailto:me@kumardamani.net" rel="me" aria-label="Email" title="Email"><i class="fa-solid fa-envelope" aria-hidden="true"></i></a>&nbsp;
    </li>
    
    <li>
        <a href="/KumarDamaniCV.pdf" rel="me" aria-label="Resume" title="Resume"><i class="fa-solid fa-file" aria-hidden="true"></i></a>&nbsp;
    </li>
    
</ul>
</div>
                <hr>
                <div class="aside__content">
    <p>How programming is romantic.</p>
    
        <p>
            By Kumar Damani, 
            2022-11-29
        </p>
    

                </div>
            </section>

            <footer class="page__footer"><p>
    
    
    
    
    
    
      
    
    
    
</p>
<br /><br />
<p class="copyright"></p>
<p class="advertisement">Powered by <a href="https://gohugo.io/">hugo</a> and <a href="https://github.com/joeroe/risotto">risotto</a>.</p>
</footer>

        </div>
    </body>

</html>