Jekyll2023-10-20T10:15:18+00:00https://codingquark.com/feed.xmlCodingquarkMy hacking adventures
Quick Capture Safari Links in Obsidian2023-10-20T04:30:00+00:002023-10-20T04:30:00+00:00https://codingquark.com/2023/10/20/quick-capture-safari-links-with-obsidian<p><img src="/assets/images/obsidian_quick_capture_safari/header_2.png" alt="header" /></p>
<p>I’ve built a straightforward capture system in <a href="(https://github.com/codingquark/emacs.g/blob/790b2a4e11955d0bc7bbc0a8525661dc8feb2773/codingquark.el#L182)">emacs</a> for storing links in a designated file. Lately, however, I’ve ventured into the world of <a href="https://obsidian.md">Obsidian</a>, a note-taking application, to explore its capabilities.</p>
<p>To make link capturing effortless, I initially experimented with <a href="(https://instapaper.com)">Instapaper</a>. The ease of capturing web content with Instapaper is genuinely remarkable—simply click a button in your browser, and voila, the content is saved for offline reading. It’s equally efficient on an iPad; just share a link with Instapaper, and it’s saved. But despite this convenience, I prefer not to rely on third-party apps and sought to create a similar workflow within Obsidian.</p>
<h1 id="objective">Objective</h1>
<p>Capture web links efficiently and store them in an Obsidian vault. The collected links will be treated as a “reading list” and appended to a specific Obsidian file. Be as distraction-free as possible.</p>
<h1 id="implementation">Implementation</h1>
<p>To create a new note in Obsidian, you can do the following:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>obsidian://new?vault=name&filepath=Reading%20List/Untitled.md&content=content
</code></pre></div></div>
<p>This approach is too clunky, it requires URL encoding, making sure <code class="language-plaintext highlighter-rouge">content</code> parameter is constructed properly. Too much work.</p>
<p>Using AppleScript, we can begin by verifying if the file already exists:</p>
<pre><code class="language-AppleScript">set obsidianFilePath to POSIX file "Valut/Reading List.md"
tell application "Finder"
set fileExists to exists file obsidianFilePath
end tell
</code></pre>
<p>If it does, appending new content is straightforward:</p>
<pre><code class="language-AppleScript">set fileDescriptor to open for access file obsidianFilePath with write permission
write obsidianContent to fileDescriptor starting at eof
close access fileDescriptor
</code></pre>
<p>To know if the capture was successful or not, create a system notification:</p>
<pre><code class="language-AppleScript">display notification "Item captured in Obsidian" with title "Quick Capture - Obsidian"
</code></pre>
<p>And the most important thing: get the current safari tab’s URL and title:</p>
<pre><code class="language-AppleScript">tell application "Safari"
set currentURL to URL of current tab of window 1
set currentTitle to name of current tab of window 1
end tell
</code></pre>
<p>Equipped with these, we can write a complete script that:</p>
<ol>
<li>Fetches current tab’s URL and title</li>
<li>Sets up appropriate content to be written</li>
<li>Checks if the file exists</li>
<li>Writes to the file</li>
<li>Generates notification for success & error</li>
</ol>
<script src="https://gist.github.com/codingquark/0821e53850faeb691f1cefc04d72b95c.js"></script>
<h1 id="automation">Automation</h1>
<p>In <a href="https://support.apple.com/en-gb/guide/automator/welcome/mac">Apple Automator</a>, we create a new “Quick Action” and select “Run AppleScript”:</p>
<p><img src="/assets/images/obsidian_quick_capture_safari/image_01.png" alt="screenshot of automator" /></p>
<p>To make this process even more effortless, associate this Automator action with a keyboard shortcut. Navigate to <code class="language-plaintext highlighter-rouge">System Settings → Keyboard → Shortcuts → General</code>, find the Automator Quick Action you’ve saved, and assign your desired key combination. I’ve set mine to <code class="language-plaintext highlighter-rouge">Ctrl+c</code>.</p>
<p><img src="/assets/images/obsidian_quick_capture_safari/image_02.png" alt="screenshot of services" /></p>
<p>Now, capturing links is as simple as pressing <code class="language-plaintext highlighter-rouge">Ctrl+c</code> while browsing in Safari, instantly adding them to your Obsidian reading list.</p>My Water Usage Follows Normal Distribution2023-10-15T04:30:00+00:002023-10-15T04:30:00+00:00https://codingquark.com/2023/10/15/water-usage-data-analysis-stats<p><img src="/assets/images/my_water_usage_follows_normal_dist/header.webp" alt="header" /></p>
<p>I have been collecting <a href="/automation/2021/11/22/motor-automation.html">water usage data</a> for three years. The way home-assistant collects data, I get hourly mean, mix and max values for the water level in a sqlite database.</p>
<h1 id="raw-numbers">Raw Numbers</h1>
<p>To set the context:</p>
<ul>
<li><strong>Total data points</strong>: 13,410</li>
<li><strong>Range of Values</strong>: 0.05 - 99.93</li>
</ul>
<p>Plot of all the points:</p>
<p><img src="/assets/images/my_water_usage_follows_normal_dist/plot_1.png" alt="Uncleaned data plot" /></p>
<h1 id="cleanup">Cleanup</h1>
<p>Negative values come from malfunctioning sensors. A lot of jitter from earlier sensors as well. Cleaning up negative values, assuming they’re sensor noise, here is a plot of data from 2021:</p>
<p><img src="/assets/images/my_water_usage_follows_normal_dist/plot_2.png" alt="Cleaned data plot" /></p>
<p>You might notice four conspicuous gaps in the dataset. The first two correspond to periods when I was tinkering with different sensors to improve system accuracy. The large gap in August-September was due to system failure during the rainy season. This event led me to thoroughly waterproof the sensor setup, resulting in visibly more consistent data collection thereafter.</p>
<h1 id="patterns">Patterns</h1>
<p>To understand usage patterns, the following plot is mean values from Sept’23:</p>
<p><img src="/assets/images/my_water_usage_follows_normal_dist/plot_3.png" alt="Sept mean values" /></p>
<p>And this is Min, Mean, Max:</p>
<p><img src="/assets/images/my_water_usage_follows_normal_dist/plot_4.png" alt="Sept mean min and max values" /></p>
<p>To see the pattern more clearly, here is a plot of mean values bucketed by hour through the month of Sept’23</p>
<p><img src="/assets/images/my_water_usage_follows_normal_dist/plot_5.png" alt="Sept hourly buckets" /></p>
<p>Key observations:</p>
<ul>
<li>Water usage dips early in the morning when the family wakes up.</li>
<li>Another dip occurs around 20:00, signaling the family’s transition to winding down for the day.</li>
</ul>
<p>Further insights can be derived from a histogram focusing on readings below 30:</p>
<p><img src="/assets/images/my_water_usage_follows_normal_dist/plot_6.png" alt="Sept hourly histogram" /></p>
<h1 id="normal-distribution">Normal Distribution</h1>
<p>Taking 1000 random samples out of total data for mean values, for 10000 times gives us the nice bell curve!</p>
<p><img src="/assets/images/my_water_usage_follows_normal_dist/plot_7.png" alt="histogram 1" /></p>
<p>When compared with a standard Normal Distribution curve, the resemblance is uncanny:</p>
<p><img src="/assets/images/my_water_usage_follows_normal_dist/plot_8.png" alt="histogram 2" /></p>
<p>To verify if the distribution really follows Normal Distribution, we can check it with Quantile-Quantile Plot showing strong correlation:</p>
<p><img src="/assets/images/my_water_usage_follows_normal_dist/plot_9.png" alt="quantile-quantile plot" /></p>
<h1 id="bonus-frequency-spectrum-analysis">Bonus: Frequency Spectrum Analysis</h1>
<p>I conducted a frequency spectrum analysis to look for any recurring patterns or cycles in water usage:</p>
<p><img src="/assets/images/my_water_usage_follows_normal_dist/plot_10.png" alt="frequency spectrum analysis" /></p>How to think rationally about laptop insurance?2023-08-25T04:30:00+00:002023-08-25T04:30:00+00:00https://codingquark.com/2023/08/25/rational-laptop-insurance-calculation<p>This post serves as an exercises in Bayesian thinking as well.</p>
<h1 id="tldr">tl;dr</h1>
<ol>
<li>Costs of Usually Damaged Parts and Repairs</li>
<li>Base Rates of Laptop Damage</li>
<li>Personal Laptop Damage History</li>
<li>Types of Damage</li>
<li>Cognitive Biases</li>
<li>Life Events and Lifestyle Changes</li>
<li>Depreciation</li>
<li>Gut Feelings</li>
</ol>
<p>Alternative: accept that <a href="https://en.wikipedia.org/wiki/Efficient-market_hypothesis">markets are efficient</a></p>
<h1 id="costs-of-usually-damaged-parts-and-repairs">Costs of Usually Damaged Parts and Repairs</h1>
<p>What are the common issues faced by other people? What are the repair costs for these parts? Find this information online and perhaps even talk to local repair shops.</p>
<h1 id="base-rates-of-laptop-damage">Base Rates of Laptop Damage</h1>
<p>How much do people around you usually end up spending on repairs? How often does this happen? This data will provide you with insight into the likelihood of your laptop getting damaged. Think of it in terms of Bayesian base rates.</p>
<h1 id="personal-laptop-damage-history">Personal Laptop Damage History</h1>
<p>Reflect on your own experiences with damaged laptops. How many times have you damaged your laptops in the past, and what were the causes? This can help you gauge your personal risk.</p>
<h1 id="types-of-damage">Types of Damage</h1>
<p>Consider the ways you’ve damaged your laptops in the past. Were they due to accidents like water spills or drops, or were they related to wear and tear issues? Different types of damage may have varying insurance coverage.</p>
<h1 id="cognitive-biases">Cognitive Biases</h1>
<p>Be aware of your cognitive biases, such as overestimating the likelihood of rare events or underestimating common risks. Try to make objective judgments by gathering data and considering all possibilities. Cognitive biases are not always bad. But important to know keep them in mind.</p>
<h1 id="life-events-and-lifestyle-changes">Life Events and Lifestyle Changes</h1>
<p>Think about potential lifestyle changes that might affect your laptop’s risk. For example, having a child might increase the chances of accidents, while transitioning to a more stable work environment could reduce risks.</p>
<h1 id="depreciation">Depreciation</h1>
<p>Account for the depreciation of your laptop’s value over time. Electronics typically lose value as they age, which can affect the amount you’d want to insure it for. This data is easily obtainable online.</p>
<h1 id="gut-feelings">Gut Feelings</h1>
<p>Summarize all the information and factors you’ve gathered. This can include estimated repair costs, your personal risk history, lifestyle changes, and depreciation. Trust your gut feeling to some extent, as it’s important to feel comfortable with your decision.</p>
<p>With all this information in hand, you can start comparing insurance premiums from different providers. It might be more cost-effective to self-insure by setting aside a “repair fund” for potential laptop issues instead of paying for insurance with a high deductible! You should now have a better feeling about it.</p>
<p>Of course you could just accept that the markets are mostly efficient and therefore most of the factors that you can and cannot think are already factored in, skip the whole exercise and start comparing the premiums.</p>This post serves as an exercises in Bayesian thinking as well.The Echoes2023-07-26T04:30:00+00:002023-07-26T04:30:00+00:00https://codingquark.com/2023/07/26/pink-floyd-echoes<iframe width="560" height="315" src="https://www.youtube.com/embed/53N99Nim6WE" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen=""></iframe>
<p>The music is their instrument to assert control. The reverberating air carries the commands. They turn up the heat and ice cube starts melting slowly. They know what they’re doing, they are turning us into water, for the rill is our destiny.</p>
<p>Before long, they succeed. We are one with the rill. We flow, we dance, we are euphoric. We fall and rise. Sun rays kissing us, dancing along. It is the metamorphosis accomplished!</p>
<p>We are transformed to the extreme. We hear crows cawing and wind wailing. We are lost, there is no ice cube left. We are abandoned, the Master Musicians have left us. There is no control, there is no direction, what will become of us? The dark dream has swallowed us.</p>
<p>Like the gentle touch of flower on a girl’s cheeks, a guitar note wakes us up. They have not left us! Through sheer magic, they have kept control. We were not lost, but willed into feeling lost. They didn’t just have control over the heat source, they were puppeteering the rill. They had absolute will on the very rays of the Sun reflecting and dancing on the rill! We are caressed back to our natural state of calm and relaxation.</p>
<p>Echoes is where time seems to stand still, and the boundaries between musician, listener, and the music itself dissolve into pure harmony.</p>Arvinddas2023-07-09T04:30:00+00:002023-07-09T04:30:00+00:00https://codingquark.com/2023/07/09/arvinddas<p>Arvinddas was a carefree ascetic who chose to wear white instead of
orange. He traversed the country on motorcycles and bicycles, carrying
no material possessions. He encountered trouble, offended people, made
friends, and shared his experiences and fortunes with generosity. To
my family, he was the wanderer who popped randomly in search of food.</p>
<p>One winter morning, as I tended to the trees and plants, Arvinddas
arrived on his platina, after months of being absent. His arrival felt
ordinary, and we exchanged no pleasantries. “Come! Come!” was my only
invitation, met with his eager response, “Yes! What are you doing? Do
you need help?” Despite his weak lungs and age above 60, he was eager
to assist me in pruning trees and removing weeds.</p>
<p>He went inside and took his place on the sofa. In a few minutes my
father happened to enter the hall and saw Arvinddas sitting. They
exchanged conventional pleasantries. I heard only bits of what they
were talking about. Putting pieces together, I gathered this time he
went to Haridwar first and from there to somewhere in MP. It took him
many days to reach Haridwar because he had no plans, no train tickets,
and no money. He said he woke up one morning, did not feel like being
in Bhuj and took his bike. I don’t know how he managed to pay for fuel
and food, but apparently in about 15 days he reached Haridwar.</p>
<p>Somewhere in Rajasthan, he found himself in a kerfuffle. Having spent
the night beneath the sheltering branches of a banyan tree along the
highway, he washed his face near a roadside dhaba the next morning,
unintentionally causing the dhaba owner’s wife to erupt in
anger. Unfazed, Arvinddas requested soap, which only further enraged
her, threatening him with a beating. Her husband intervened, leading
Arvinddas away, calming his wife in the process. The husband shared
his own tales of aiding people like Arvinddas, finding ways to help
without stirring trouble with his wife. He even offered financial
support.</p>
<p>Arvinddas regaled us with marvelous stories from his recent time in
Madhya Pradesh while a glass of chhaas was prepared to welcome
him. Soon he requested lunch. Despite being advised by a doctor to
avoid certain dishes, Arvinddas opted for khakhra, khichdi, and a
specific dish designed by my mother just for him. He always ate alone,
and after finishing his meal, requested a cold drink. We happened to
have a few forbidden, sugar-filled bottles of coke.</p>
<p>He rummaged through his bag, assessing if he had enough
clothes. Finding the answer to be in the negative, we offered him a
dhoti and a tote bag. He sat beneath the fan for a quarter of an hour,
sharing rambling tales of politics, however incoherent. He expressed
the pain of his deteriorating health, only to declare it no longer
mattered to his liberated mind. This launched him further into the
exploration of his philosophical ideas on human body’s purposes,
techniques to shape and control both the body and mind.</p>
<p>As he prepared to leave, he inspected my garden work and offered
tips. He possessed a knack for it. I always maintained my concern
about the risks he faced on the road, both for himself and others. He
should not have been driving a motorcycle.</p>
<p>I have known Arvinddas living in huts devoid of running water and
electricity, in large two-story houses entrusted to him by absent
tenants. I have seen him construct makeshift shelters in remote
locations and settle amidst decaying farmlands. He lived in ashrams at
Dhinodhar, Haridwar, Rishikesh and other obscure places. I recall his
repeated pilgrimages of “Narmada Parikrama”, each time catching one
disease or another. I have known him to be a man striving for
independence—repairing houses, gardens, roads, experimenting with his
own health, and abandoning places at whim. His religious beliefs
underwent countless transformations, each iteration a new blend of the
previous.</p>
<p>My family observed Arvinddas’s unconventional ideas from a distance,
refraining from expressing opinions on his views. He held some
questionable—and sometimes offensive—beliefs regarding women, one of
which I take credit of breaking him away from by uttering the simple
statement, “Is that what you think of my mother, who is cooking for
you right now?” I vividly recall the flow of changing expressions as
he pondered my words. Eventually he said “You are right, that is
wrong”. It was a small victory, having swayed an old man grappling
with religions and his internal dialogues.</p>
<p>Some months ago, the carefree ascetic departed from this world, his
lungs succumbing to the passage of time. He was at the Kachchhi Ashram
in Haridwar, and his body was hastily flown back to Bhuj. His passing
went unnoticed by those unaware of his existence. But for us, it feels
as though an infinite period stretches out ahead before he
unexpectedly returns, seeking sustenance once again.</p>Arvinddas was a carefree ascetic who chose to wear white instead of orange. He traversed the country on motorcycles and bicycles, carrying no material possessions. He encountered trouble, offended people, made friends, and shared his experiences and fortunes with generosity. To my family, he was the wanderer who popped randomly in search of food.The Seed of Science2023-06-20T04:30:00+00:002023-06-20T04:30:00+00:00https://codingquark.com/2023/06/20/first-classroom-story<p><img src="/assets/images/boy_floating_through_space.jpg" alt="floating through space" /></p>
<p><a href="https://www.youtube.com/watch?v=uwkLZg6WDek&list=PLco_u-O9FeQ_cV5gc3VdUHoQYBI73MYkU">Listen to Interstellar OST</a> as you read this.</p>
<p>As the hour approached, the young boy shouldered his schoolbag and made his way down the stairs. His friends awaited him three stories below, their bicycles ready. During the long summer break, he had eagerly flipped through the pages of the new textbooks, his eyes fixated on the captivating, crude black-and-white line drawings that adorned the science book.</p>
<p>The group of boys reached their classroom, a familiar space that would be their home for the next academic year. Among them, the boy found his place in the second row, occupying the middle column bench. It was his designated spot for the year.</p>
<p>Just as the anticipation settled in, Miss Kusum gracefully entered the classroom for their first science lesson. Dressed in a deep green sari with delicate gray patterns, she wore round spectacles with thin metallic frames. Her slender figure radiated grace and warmth. Unlike the other teachers that day, she urged the students to put away their textbooks and listen.</p>
<p>With a warm smile gracing her lips, Miss Kusum embarked on her storytelling journey. Her voice carried a melodic tone, drawing the young mind in. As she spoke of the extraordinary creation known as the Voyager spacecraft, the boy’s imagination ignited like a flickering flame. Vivid images danced before his mind’s eye, painting a vibrant tapestry of the spacecraft, the planets, and the limitless wonders of the universe. The walls of the classroom melted away, replaced by the vast expanse of a mysterious and captivating darkness, where celestial bodies twinkled like distant beacons. The boy found himself floating amidst the cosmos, it was as if he stood beneath a vast, star-studded sky, where countless dots of light twinkled like celestial beacons.</p>
<p>Miss Kusum described how the Voyager had been meticulously built by humans, a product of tireless effort, unwavering dedication, and absolute genius. It was launched into space with a mission to explore the planets. The boy’s mind raced with excitement as he envisioned the spacecraft journeying through the solar system, capturing stunning images of distant worlds. Saturn, with its majestic rings, would be the Voyager’s next destination, followed by Uranus, Neptune, and even Pluto.</p>
<p>The boy was enthralled, transported to a realm where scientific discovery knew no bounds. The spacecraft traveled through the vastness of space, venturing far beyond the planets. Miss Kusum explained that it would eventually escape the confines of the solar system, venturing into interstellar space. The boy marveled at the idea of a machine venturing where no human had gone before, witnessing sights that had never been seen.</p>
<p>As Miss Kusum concluded her tale, the classroom gradually brightened, returning the boy to reality. Science, she proclaimed, was the pursuit of such remarkable knowledge and the ability to create marvels like the Voyager spacecraft. The rest of the class proceeded with the mundane contents of the first chapter in the textbook.</p>
<hr />
<p>That was remembrance of a day from my childhood. I often think of this day. I do not remember the exact words of the teacher, but it captures my feelings well.</p>
<p>That single story shared by Miss Kusum had a profound impact on me, leaving a lasting impression that has shaped my life. It sparked a deep curiosity within me, driving my fascination with the mysteries of the universe. I vividly recall buying many issues of the magazine called “Safari”. From then on, I used to tell everyone “I want to be a scientist!” on every opportunity.</p>
<p>Looking back now, that story remains one of my most cherished childhood memories.</p>Some equations from Concrete Mathematics Chapter 22023-06-01T04:30:00+00:002023-06-01T04:30:00+00:00https://codingquark.com/mathematics/2023/06/01/concrete-mathematics-chapter-2-useful-eqs<p>Brace yourselves for a wall of equations. These are really just for me, I like them and wanted to have a list.</p>
<p>Chapter is about sums of the general form:</p>
\[\begin{equation}
a_1+a_2+a_3+ \dotsm +a_n \text{,}
\end{equation}\]
<p>Where each \(a_k\) is called a <em>term</em>.</p>
<hr />
<p>Sigma notation</p>
\[\begin{equation}
\sum_{k=1}^n a_k
\end{equation}\]
<p>We called it a “term” in previous notation. Here they’re called <em>summand</em>.</p>
<hr />
<p>Another way to write in Sigma notation</p>
\[\begin{equation}
\sum_{1 \leq k \leq n} a_k
\end{equation}\]
<hr />
<p>For any recurrence of the form</p>
\[\begin{equation}
a_nT_n = b_nT_{n-1}+c_n
\end{equation}\]
<p>The solution is</p>
\[\begin{equation}
T_n = \frac{1}{s_na_n} \left( s_1b_1T_0 + \sum_{k=1}^ns_kc_k \right)
\end{equation}\]
<p>If we can find a <em>summation factor</em> of the form</p>
\[\begin{equation}
s_n = \frac{a_{n-1}a_{n-2} \dots a_1 }{b_nb_{n-1} \dots b_2 }
\end{equation}\]
<hr />
<p>Harmonic numbers appear everywhere.</p>
\[\begin{equation}
H_n = 1 + \frac{1}{2} + \dotsm + \frac{1}{n} = \sum_{k=1}^n \frac{1}{k}
\end{equation}\]
<hr />
<p>Distributive law</p>
\[\begin{equation}
\sum_{k \in K}ca_k = c \sum_{k \in K}a_k
\end{equation}\]
<p>Associative law</p>
\[\begin{equation}
\sum_{k \in K} (a_k + b_k) = \sum_{k \in K}a_k + \sum_{k \in K}b_k
\end{equation}\]
<p>Commutative law (permutative law)</p>
\[\begin{equation}
\sum_{k \in K}a_k = \sum_{p(k) \in K}a_{p(k)}
\end{equation}\]
<hr />
<p>To combine different sets of indices</p>
\[\begin{equation}
\sum_{k \in K}a_k + \sum_{k \in K’}a_k = \sum_{k \in K \cap K’}a_k + \sum_{k \in K \cup K’}a_k
\end{equation}\]
<hr />
<p>Perturbation method</p>
\[\begin{equation}
S_n + a_{n+1} = \sum_{0 \leq k \leq n+1}a_k = a_0 + \sum_{0 \leq k \leq n}a_{k+1}
\end{equation}\]
<hr />
<p>Interchanging the order of summation</p>
\[\begin{equation}
\sum_j\sum_k a_{j,k}[P(j,k)] = \sum_{P(j,k)}a_{j,k} = \sum_k\sum_j a_{j,k}[P(j,k)]
\end{equation}\]
<p>Variation “vanilla” of interchanging the order of summation</p>
\[\begin{equation}
\sum_{j \in J}\sum_{k \in K}a_{j,k} = \sum_{\substack{j \in J \\ k \in K}}a_{j,k} = \sum_{k \in K}\sum_{j \in J} a_{j,k}
\end{equation}\]
<p>Variation “rocky road”
\begin{equation}
\sum_{j \in J}\sum_{k \in K(j)}a_{j,k} = \sum_{k \in K’}\sum_{j \in J’(k)}a_{j,k}
\end{equation}</p>
<p>\begin{equation}
\left( \sum_{k=1}^na_k \right) \left( \sum_{k=1}^nb_k \right) = n \sum_{1 \leq j < k \leq n} (a_k - a_j)(b_k-b_j)
\end{equation}</p>
<hr />
<p>In commutative law, when \(p(k)\) is instead <em>any</em> arbitrary function \(f\)</p>
\[\begin{equation}
\sum_{j \in J}a_{f(j)} = \sum_{k \in K}a_k \# f^-(k)
\end{equation}\]
\[\begin{equation}
\sum_{j \in J}a_{f(j)} = \sum_{f(j) \in K}a_{f(j)} = \sum_{k \in K}a_k
\end{equation}\]
<hr />
\[\begin{align}
\Delta f(x) & = f(x+1) - f(x) \\
x^{\underline{m}} & = \overbrace{x(x-1)\dots (x-m+1)}^{m\; \text{factors}} \\
x^{\overline{m}} & = \overbrace{x(x+1) \dots (x+m-1)}^{m \; \text{factors}} \\
x^{\underline{-m}} & = \frac{1}{(x+1)(x+2)\dots (x+m)} \\
\Delta(x^{\underline{m}}) & = mx^{\underline{m-1}} \\
x^{\underline{m+n}} & = x^{\underline{m}}(x-m)^{\underline{n}}
\end{align}\]
\[\begin{align}
g(x) & = \Delta f(x) \iff \sum g(x) \delta x = f(x) + C \\
\sum_a^bg(x)\delta x & = \sum_{k=a}^{b-1}g(k) = \sum_{a \leq k < b}g(k) \\
\sum_a^bg(x) \delta x & = f(x)\bigg\rvert_a^b = f(b) - f(a) \\
\sum_a^bg(x) \delta x & + \sum_b^cg(x) \delta x = \sum_a^cg(x)\delta x
\end{align}\]
<p>These rules applied, give</p>
\[\begin{equation}
\sum_{0 \leq k < n}k^{\underline{m}} = \frac{k^{\underline{m+1}}}{m+1}\bigg\rvert_0^n = \frac{n^{\underline{m+1}}}{m+1}
\end{equation}\]
<hr />
<p>Summation by parts</p>
\[\begin{equation}
\sum u \Delta v = uv - \sum Ev \Delta u
\end{equation}\]Brace yourselves for a wall of equations. These are really just for me, I like them and wanted to have a list.Concrete Mathematics Chapter 1 Exercise 112023-05-25T04:30:00+00:002023-05-25T04:30:00+00:00https://codingquark.com/mathematics/2023/05/25/concrete-mathematics-ch-one-ex-eleven<p>We often see polished proofs and solutions. They have no mistakes, no signs of exploration… I will not do that here, we are going to explore. Apologies for not having images and videos, they would’ve been of much help.</p>
<h1 id="exercise-111">Exercise 1.11</h1>
<p>A Double Tower of Hanoi contains \(2n\) disks of \(n\) different sizes, two of each size. As usual, we’re required to move only one disk at a time, without putting a larger one over a smaller one.</p>
<ol type="a">
<li>How many moves does it take to transfer a double tower from one peg to another, if disks of equal size are indistinguishable from each other?</li>
<li>What if we are required to reproduce the original top-to-bottom order of all the equal-size disks in the final arrangement? [<i>Hint:</i> This is difficult—it’s really a “bonus problem.”]</li>
</ol>
<h1 id="solution">Solution</h1>
<p>We will always have even number of disks. Let’s run the double tower manually. Pick up some objects and transfer the tower, write the results down:</p>
<table>
<thead>
<tr>
<th>\(\textbf{n}\)</th>
<th>\(\textbf{D(n)}\)</th>
</tr>
</thead>
<tbody>
<tr>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>6</td>
</tr>
<tr>
<td>6</td>
<td>22</td>
</tr>
<tr>
<td>8</td>
<td>30</td>
</tr>
</tbody>
</table>
<p>That is odd, why is it jumping at 6? If we disregard \(D(6)\) then there seems to be a pattern. We need to play more with this. Let’s note the results again (I was indeed making mistake with \(D(6)\) for some reason!):</p>
<table>
<thead>
<tr>
<th>\(\textbf{n}\)</th>
<th>\(\textbf{D(n)}\)</th>
</tr>
</thead>
<tbody>
<tr>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>6</td>
</tr>
<tr>
<td>6</td>
<td>14</td>
</tr>
<tr>
<td>8</td>
<td>30</td>
</tr>
</tbody>
</table>
<p>Let’s remind ourselves of the original Tower of Hanoi problem. The results were:</p>
<table>
<thead>
<tr>
<th>\(\textbf{n}\)</th>
<th>\(\textbf{T(n)}\)</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>3</td>
<td>7</td>
</tr>
<tr>
<td>4</td>
<td>15</td>
</tr>
<tr>
<td>5</td>
<td>31</td>
</tr>
<tr>
<td>6</td>
<td>63</td>
</tr>
</tbody>
</table>
<p>The recurrence was:</p>
\[\begin{equation}
T_n = 2T_{n-1} + 1
\end{equation}\]
<p>There does not seem to be any correlation between \(T(n)\)’s results and \(D(n)\). You can try various things, such as checking if adding or subtracting between \(T(n)\) and \(D(n)\) results into someting interesting. Or perhaps some offset of \(n\) might work. I couldn’t find anything.</p>
<p>Going forward, should we think in terms of \(n\) disks or \(2n\) disks? Not sure… Anyway, let’s still move on.</p>
<p>To move a tower of \(4\) disks, first \(2\) disks must be transferred to the middle peg. Then \(2\) more moves to transfer bottom two disks. Then \(2\) more moves to transfer \(2\) upper disks.</p>
<p>To transfer \(n\) disks, \(n-2\) disks must first be transferred to the middle peg. Requiring \(D_{n-2}\) moves. Next, \(2\) moves for the bottom \(2\) disks. Finally we need \(D_{n-2}\) more moves.</p>
<p>It seems that</p>
\[\begin{equation}
D_n = 2D_{n-2} + 2 \quad \text{for $n>1$.}
\end{equation}\]
<p>Does it fit our data?</p>
\[\begin{align*}
D_2 & = 2D_0 + 2 \\
& = 2 \\
D_4 & = 2D_2 + 2 \\
& = 6 \\
D_6 & = 2D_4 + 2 \\
& = 14 \\
D_8 & = 2D_6 + 2 \\
& = 30
\end{align*}\]
<p>Yes! It fits, and is already shown to be minimum (perhaps it could be a bit more rigorous).</p>
<p>Now we have to <em>solve</em> the recurrence. Instead of trying induction, let’s try “non-clairvoyant” ways similar to “adding \(1\)” that was tried in the chapter. In the graffito, we find an interesting remark: “Interesing: We get rid of the \(+1\) in \((1.1)\) by adding, not subtracting.” Hmm, we would like to get rid of the \(+2\) and so let’s add \(2\) and see where it goes.</p>
\[\begin{align*}
D_n & = 2D_{n-2} + 2 \\
\therefore D_n + 2 & = 2D_{n-2} + 4
\end{align*}\]
<p>Let \(U_n = D_n + 2\).</p>
\[\begin{align*}
\therefore U_n & = 2(D_{n-2}+2) \\
\therefore U_n & = 2U_{n-2}
\end{align*}\]
<p>Does it take a genius to discover that this is \(2^n - 2\)? Probably not.</p>We often see polished proofs and solutions. They have no mistakes, no signs of exploration… I will not do that here, we are going to explore. Apologies for not having images and videos, they would’ve been of much help.Words Worth The Pain2023-05-05T04:30:00+00:002023-05-05T04:30:00+00:00https://codingquark.com/2023/05/05/words-worth-the-pain<p>Pain serves its purpose. It is a message conveying “pay attention here”. Physical pain—of injuries, illnesses—makes damn sure we give it a rest. Pain is essential in identifying what is wrong, where.</p>
<p>I seem to have been infected with some seasonal virus. When such an infection is accompanied with a fluctuation in temperature, the tonsils swell. Swollen tonsils make my life quite rough for a few days. The body starts signalling well in advance that things are going to wrong.</p>
<p>Not just food, it is tough to swallow saliva, to turn the neck. Under these circumstances, speech has a cost.</p>
<p>Marvellous side effect! Every word I speak has been carefully evaluated for the cost about to be incurred. If the set of organs are to be actuated, knowing it is absolutely bound to cause pain that will be felt for a few seconds after the vibrations of the organs have subsided, they better be substantial, meaningful, absolutely necessary, short, concise…</p>
<p>The pain makes me aware of the great benefits of not speaking, not reacting, paying attention, thinking before speaking. So much is better left unsaid, so much can be conveyed by gestures, so much is worth just a smile and a nod!</p>
<blockquote>
<center><i>Pain is the first proper step to real compassion; it can be a foundation for understanding all those who struggle with their existence.</i></center>
<div align="right"><i>~ David Whyte</i></div>
</blockquote>
<p>The pain will fade away soon. Eventually the memory of the pain. After that, it will be hard to imagine “the pain it used to cause”. I will revert back to speaking without having to pay the cost. Perhaps I should have a switch that penalizes for speaking. An efficient reminder to be exceedingly nice to people, avoid most of the dialogues, just sigh and move on, have space to be compassionate. At least, I get this switch turned on often enough to be reminded once a year.</p>
<p>I shall therefore unburden myself of the labor of words and leave you <a href="https://web.archive.org/web/20230505063116/https://toko-pa.com/2013/12/15/on-pain-by-david-whyte/">with</a> (<a href="https://dynamic.wakingup.com/course/C6E1EA?code=SC0D80C41&share_id=C341E126&source=content%20share">listen</a>):</p>
<h1 id="pain-by-david-whyte">Pain by David Whyte</h1>
<p>Pain is the doorway to the here and now. Physical or emotional pain is an ultimate form of ground, saying, to each of us, in effect, there is no other place than this place, no other body than this body, no other limb or joint or pang or sharpness or heartbreak but this searing presence. Pain asks us to heal by focusing not only on the place the pain is felt but also the actual way the pain is felt. Pain is a form of alertness and particularity; pain is a way in.</p>
<p>Through the radical undoing and debilitation of repeated pain we are reacquainted with the essentialities of place and time and existence itself; in deep pain we have energy only for what we can do wholeheartedly and then, only within a narrow range of motion, metaphorically or physically, from tying our shoelace to holding the essential core conversations that are reciprocal and reinforcing within the close-in circle of those we love. Pain teaches us a fine economy, in movement, in the heart’s affections, in what we ask of ourselves and eventually in what we ask in others.</p>
<p>Pain’s beautiful humiliations make us naturally humble and force us to put aside the guise of pretense. In real pain we have no other choice but to learn to ask for help and on a daily basis. Pain tells us we belong and cannot live forever alone or in isolation. Pain makes us understand reciprocation. In real pain we often have nothing to give back other than our own gratitude, a smile that perhaps looks half like a grimace or the passing friendship of the thankful moment to a helpful stranger, and pain can be an introduction to real friendship, it tests those friends we think we already have but also introduces us to those who newly and surprisingly come to our aid.</p>
<p>Pain is the first proper step to real compassion; it can be a foundation for understand all those who struggle with their existence. Experiencing real pain ourselves, our moral superiority comes to an end; we stop urging others to get with the program, to get their act together or to sharpen up, and start to look for the particular form of debilitation, visible or invisible that every person struggles to overcome. In pain, we suddenly find our understanding and compassion engaged as to why others may find it hard to fully participate.</p>
<p>Strangely, the narrow focus that is the central difficult invitation of bodily pain, calls for a greater perspective, for a bigger, more generous sense of humor. With the grand perspective real pain is never far from real laughter - at ourself or for another watching that self-laughter at the predicament or the physical absurdity that has become a daily experience. Pain makes drama of an everyday life with our body and our presence firmly caught on stage and in the spotlight, we are visible to others in a way over which we have no choice, limping here or leaning there.</p>
<p>Lastly, pain is appreciation; for most of all the simple possibility and gift of a pain-free life - all the rest is a bonus. Others do not know the gift in simply being healthy, of being unconsciously free to move or walk or run. Pain is a lonely road, no one can know the measure of our particular agonies, but through pain we have the possibility, just the possibility, of coming to know others as we have, with so much difficulty come to know ourselves.</p>Pain serves its purpose. It is a message conveying “pay attention here”. Physical pain—of injuries, illnesses—makes damn sure we give it a rest. Pain is essential in identifying what is wrong, where.Notes on Recurrent Problems — concrete mathematics chapter 12023-04-29T04:30:00+00:002023-04-29T04:30:00+00:00https://codingquark.com/mathematics/2023/04/29/concrete-mathematics-chapter-one<p>These are some of the notes to help understand the chapter. It fills in some of the gaps I had while studying. It does not provide a summary of any kind. It shows various inductive proofs, follows algebraic steps in a verbose way and points out some subtlties along the way. There’s also a <a href="/assets/files/concrete_mathematics/chapter01/notes.pdf">PDF</a>.</p>
<h1 id="tower-of-hanoi">Tower of Hanoi</h1>
<ul>
<li>
<p>We <em>hope</em> to find the following by playing with the problem:</p>
\[\begin{equation*}
T_n \leq 2 T_{n-1} + 1 \quad \text{for $n > 0$.}
\end{equation*}\]
</li>
<li>
<p>We must move \(n-1\) disks before being able to move \(n^{th}\) disk</p>
\[\begin{equation*}
\therefore T_n \geq 2 T_{n-1} + 1 \quad \text{for $n > 0$.}
\end{equation*}\]
</li>
<li>
<p>By looking at many cases \(n\), we <em>might guess</em> at closed-form:</p>
\[\begin{equation}\label{hanoiclosed}
T_n = 2^n - 1\text{,} \quad \text{for $n \geq 0$.}
\end{equation}\]
</li>
</ul>
<h2 id="proof-of-closed-form">Proof of closed-form</h2>
<p>We proceed using induction.</p>
<p><strong>Base Case:</strong> \(n=0\). The equation is simply</p>
\[\begin{align*}
n & = 0 \\
\therefore T_0 & = 2^0 - 1 \\
& = 0
\end{align*}\]
<p><strong>Inductive Hypothesis:</strong> Assume \(\eqref{hanoiclosed}\) is true for \(n-1\).</p>
\[\begin{align*}
T_n & = 2 T_{n-1} + 1 \\
& = 2(2^{n-1} - 1) + 1 \\
& = 2^n - 2 + 1 \\
\therefore T_n & = 2^n - 1
\end{align*}\]
<p>By induction, we have shown that the hypothesis \(\eqref{hanoiclosed}\) holds for \(n+1\).</p>
<p>The recurrence can be solved usuing other methods. The one shown in the chapter (of adding \(1\) to each side etc) leaves some open questions:</p>
<ul>
<li>How do we know to add \(1\) on each side?</li>
<li>How do we know to represent \(U_n = T_{n-1} + 1\)?</li>
</ul>
<h1 id="lines-in-the-plane">Lines in the Plane</h1>
<p>Prove that the closed-form for the recurrence is</p>
\[\begin{align}\label{linesclosed}
L_n = \frac{n(n-1)}{2} + 1 \text{,} \quad \text{for $n \geq 0$.}
\end{align}\]
<p><strong>Base Case:</strong> \(n=0\).</p>
\[\begin{align*}
L_0 & = \frac{0(0 + 1)}{2} + 1 \\
\therefore L_0 & = 1
\end{align*}\]
<p><strong>Inductive Hypothesis:</strong> Assume \(\eqref{linesclosed}\) to be true \(n-1\).</p>
<p><strong>Inductive Step:</strong> From recurrence</p>
\[\begin{align*}
L_n & = L_{n-1} + n \\
& = \biggl(\frac{n(n-1)}{2} + 1\biggr) + n \\
& = \frac{n^2}{2} - \frac{n}{2} + 1 + n \\
& = \frac{n^2}{2} + \frac{n}{2} + 1 \\
& = \frac{n(n-1)}{2} + 1
\end{align*}\]
<p>Thus, by induction we prove that the hypothesis holds for \(n+1\).</p>
<h1 id="the-josephus-problem">The Josephus Problem</h1>
<p>We have to tackle even \(n\) and odd \(n\) separately. We set \(2n\) people in even case because integers multiplied by \(2\) always result in even number. Adding \(1\) to an even integer always results in odd, hence we set \(2n+1\) for the other case. It might take some effort to see that \(J(5 \cdot 2^m) = 2^{m+1} + 1\).</p>
<h2 id="proving-the-closed-form">Proving the closed form</h2>
<p>We want to prove the following for both — odd and even — cases.</p>
\[\begin{equation}\label{josclosed}
J(2^m + \ell) = 2\ell + 1
\end{equation}\]
<p>We proceed with induction on two separate cases.</p>
<p><strong>Base Case:</strong> \(n=1\).</p>
\[\begin{equation*}
\therefore m = 0 \text{,} \quad \ell = 0\text{.}
\end{equation*}\]
\[\begin{align*}
J(2^m + \ell) & = 2^0 + 0 \\
& = 1
\end{align*}\]
<p><strong>Inductive Step:</strong> Suppose that for all \(n\) such that \(n = 2^k + r\), we have \(J(n) = J(2^k + r) = 2r + 1\)</p>
<ol>
<li>
<p>Suppose \(m > 0\) and \(n = 2^m + r = 2\ell\). That is, \(n\) is even.
From recurrence:</p>
\[\begin{align*}
J(2n) & = 2J(n) - 1 \\
\therefore J(n) & = 2J(\frac{n}{2}) - 1 \\
\therefore J(2^m + \ell) & = 2J\biggl( \frac{2^{m-1}+\ell}{2} \biggr) - 1
\end{align*}\]
<p>Now, assuming the hypothesis to be true for smaller \(n\):</p>
\[\begin{align*}
J\biggl(\frac{2^{m-1}+\ell}{2}\biggr) & = \frac{2\ell}{2} + 1
\end{align*}\]
\[\begin{align*}
\therefore J(2^m + \ell) & = 2\biggl(\frac{2\ell}{2} + 1 \biggr) - 1 \\
& = \frac{4\ell}{2} + 2 - 1 \\
\therefore J(2^m + \ell) & = 2\ell + 1
\end{align*}\]
</li>
<li>
<p>Suppose \(m > 0\) and \(n = 2^m + r = 2\ell + 1\). That is, \(n\) is odd. Similar to the even case,</p>
\[\begin{align*}
J(n) & = 2J\biggl(\frac{n}{2} - 1 \biggr) + 1
\end{align*}\]
<p>Assuming the hypothesis to be true, we get</p>
\[\begin{align*}
J(2^{m-1}+\frac{\ell}{2} - 1) & = 2J(2^{m-1}+\frac{\ell}{2} - 1) + 1
\end{align*}\]
\[\begin{align*}
\therefore J(n) & = 2 \biggl(\frac{2\ell}{2} + 1 - 1 \biggr) + 1 \\
& = 2\biggl(\frac{2\ell}{2} \biggr) + 1 \\
& = 2\ell + 1
\end{align*}\]
</li>
</ol>
<p>Thus by induction we prove the closed-form for both odd and even cases of \(n\).</p>
<h2 id="checking-where-jnfracn2-works">Checking where \(J(n)=\frac{n}{2}\) works</h2>
\[\begin{align*}
J(n) & = \frac{n}{2} \\
\therefore 2\ell + 1 & = \frac{2^m + \ell}{2} \\
\therefore 2^m+\ell & = 2(2\ell + 1) \\
\therefore 2^m & = 4\ell - \ell + 2 \\
\therefore 2^m & = 3\ell + 2 \\
\therefore 3\ell & = 2^m - 2 \\
\therefore \ell & = \frac{1}{3}(2^m - 2)
\end{align*}\]
<h2 id="generalisation-of-josephus-problem">Generalisation of Josephus Problem</h2>
<p>We convert constants in the recurrence into variables:</p>
\[\begin{equation}
\label{eq:josephus:gen}
\begin{split}
f(1) & = \alpha\text{,} \\
f(2n) & = 2f(n) + \beta\text{,} \quad \text{for $n \geq 1$;} \\
f(2n + 1) & = 2f(n) + \gamma\text{,} \quad \text{for $n \geq 1$.}
\end{split}
\end{equation}\]
<p>Equations in (1.12) of the chapter tells us that \(f(n)\) can be written as:</p>
\[\begin{equation}
\label{eq:josephus:gen:2}
f(n) = A(n)\alpha + B(n)\beta + C(n)\gamma
\end{equation}\]
<h2 id="repertoire-method">Repertoire Method</h2>
<p>We find settings for general parameters (in our case \(\alpha,\beta,\gamma\)) for which we know the solution. This gives us <em>repertoire</em> of special cases that we can solve. Usually, we need as many special cases as there are parameters.</p>
\[\begin{equation}
\label{eq:josephus:rep:2}
\begin{split}
A(n) & = 2^m\text{;} \\
B(n) & = 2^m-1-\ell\text{;} \\
C(n) & = \ell\text{.}
\end{split}
\end{equation}\]
<p>We proceed with repertoire method.</p>
<ol>
<li>
<p>Special case \(\alpha=1\text{,}\beta=\gamma=0\).</p>
\[\begin{align*}
f(n) & = A(n)\alpha + B(n)\beta + C(n)\gamma
\end{align*}\]
\[\begin{align*}
f(1) & = A(1)\cdot 1 + B(1)\cdot 0 + C(1)\cdot 0 \\
\therefore f(1) & = A(1) \\
\therefore A(1) & = 1 \quad \text{from recurrence.}
\end{align*}\]
\[\begin{align*}
f(2n) & = 2A(n)\alpha + B(n)\beta + C(n)\gamma \\
& = 2A(n) + 1 \cdot \beta \\
& = 2A(n)
\end{align*}\]
\[\begin{align*}
f(2n+1) & = 2A(n)\alpha + B(n)\beta + C(n)\gamma \\
& = 2A(n)\cdot 1 + 0 \cdot 0 + 1 \cdot 0 \\
& = 2A(n)
\end{align*}\]
<p>Let’s show \(A(n) = 2^m\). We proceed with proof by induction to show:</p>
\[\begin{equation}
\label{eq:josephus:rep:1}
A(2^m+\ell) = 2^m
\end{equation}\]
<p><strong>Base Case:</strong> \(n=1\).</p>
\[\begin{equation*}
\therefore m = 0 \text{,} \quad \ell = 0\text{.}
\end{equation*}\]
\[\begin{align*}
A(2^0+0) & = 1 \\
\therefore A(1) & = 1
\end{align*}\]
<p><strong>Inductive Step:</strong> Assume the hypothesis \eqref{eq:josephus:rep:1} to be true for smaller \(n\) like we did for recurrence solution the previous section.</p>
\[\begin{align*}
A(2n) & = 2A(n) \\
\therefore A(n) & = 2A\biggl(\frac{n}{2}\biggr) \\
\therefore A(n) & = 2A\biggl(\frac{2^{m-1}+\ell}{2}\biggr)
\end{align*}\]
<p>Assuming the hypothesis \(\eqref{eq:josephus:rep:1}\) for \(n-1\)</p>
\[\begin{align*}
A(2^{m-1}+\frac{\ell}{2}) & = 2^{m-1} \\
\quad \\
\therefore A(n) & = 2 \cdot 2^{m-1} \\
\therefore A(n) & = 2^m
\end{align*}\]
<p>Similarly,</p>
\[\begin{align*}
A(2n+1) & = 2A(n) \\
\therefore A(n) & = 2A\biggl(\frac{n}{2} - 1\biggr) \\
\therefore A(n) & = 2A\biggl(\frac{2^{m-1}+\ell}{2} - 1\biggr) \\
& = 2 \cdot 2^{m-1} \\
\therefore A(n) & = 2^m
\end{align*}\]
<p>Thus, we show by induction that \(A(n) = 2^m\).</p>
</li>
<li>
<p>Are there any constants (\(\alpha,\beta,\gamma\)) that give the general equation?</p>
<p>Take \(f(n) = 1\). Since \(f\) always gives \(1\), it is called a “constant function”.</p>
\[\begin{align*}
f(n) & = 1 \\
\therefore \alpha & = 1 \\
\text{And,} \\
1 & = 2\cdot f(n) + \beta \\
\therefore 1 & = 2 \cdot 1 + \beta \\
\therefore \beta & = -1 \\
\text{And,} \\
1 & = 2 \cdot f(n) + \gamma \\
\therefore 1 & = 2 + \gamma \\
\therefore \gamma & = -1
\end{align*}\]
<p>Plugging these values of \(\alpha,\beta,\gamma\) in \(\eqref{eq:josephus:gen:2}\), we get</p>
\[\begin{align*}
f(n) & = A(n)\cdot 1 + B(n)\cdot (-1) + C(n)\cdot (-1) \\
& = A(n) - B(n) - C(n) \\
\quad \\
B(n) & = A(n) - 1 - C(n) \\
\therefore B(n) & = 2^m - 1 - \ell
\end{align*}\]
</li>
<li>
<p>Let’s set \(f(n) = n\)</p>
\[\begin{align*}
1 & = \alpha \\
\text{And,} \\
2n & = 2f(n) + \beta \\
\therefore 2n & = 2n + \beta \\
\therefore \beta & = 0 \\
\text{And,} \\
2n + 1 & = 2f(n) + \gamma \\
\therefore 2n + 1 & = 2n + \gamma \\
\therefore \gamma & = 1
\end{align*}\]
<p>Plugging these values of \(\alpha,\beta,\gamma\) into \(\eqref{eq:josephus:gen:2}\), we get:</p>
\[\begin{align*}
f(n) & = A(n)\alpha + B(n)\beta + C(n)\gamma \\
\therefore n & = A(n)\cdot 1 + B(n)\cdot 0 + C(n)\cdot 1 \\
\therefore n & = A(n) + C(n)
\end{align*}\]
</li>
</ol>
<p>With the three cases, we now have</p>
\[\begin{align*}
A(n) & = 2^m \\
A(n) - B(n) - C(n) & = 1 \\
A(n) + C(n) & = n
\end{align*}\]
<p>Now,</p>
\[\begin{align*}
C(n) & = n - A(n) \\
& = n - 2^m \\
\therefore C(n) & = \ell \\
\text{And,} \\
B(n) & = A(n) - 1 - C(n) \\
\therefore B(n) & = 2^m - 1 - \ell
\end{align*}\]
<p>Thus we have our hypothesis from \(\eqref{eq:josephus:rep:2}\) proven.</p>
<h2 id="checking-if-bit-shift-property-holds">Checking if bit-shift property holds</h2>
\[\begin{equation}
\label{eq:josephus:radix}
f((b_mb_{m-1}\ldots b_1b_0)_2) = (\alpha\beta_{b_{m-1}}\beta_{b_{m-2}}\ldots\beta_{b_1}\beta_{b_0})_2
\end{equation}\]
<p>Equation \(\eqref{eq:josephus:radix}\) is not strictly binary radix. Instead of allowing \(0\) and \(1\) values for \(\beta\), we are allowing <em>any</em> values. This is because our parameters \((\alpha,\beta,\gamma)\) are general and can take any value.</p>
<p>For \(n=100\) in Josephus Problem, we have \(\alpha =1,\beta = -1, \gamma = 1\). Also note that \(\beta_{b_{m-1}}=\beta_{1}=1\), \(\beta_{b_{m-3}}=\beta_{0}=-1\) and so on. The cyclic left bit-shift propery from earlier holds in the generalised form as well:</p>
\[\begin{align*}
100 & = (1100100)_2 \\
73 & = (1001001)_2
\end{align*}\]These are some of the notes to help understand the chapter. It fills in some of the gaps I had while studying. It does not provide a summary of any kind. It shows various inductive proofs, follows algebraic steps in a verbose way and points out some subtlties along the way. There’s also a PDF.