Useless algorithms that I use fully in my life — part 1?

Foolish Bubbul

They say the more you code, the more you start to think like a computer; I gotta hope that is not true, because computers are dumb as f*ck. We see all the amazing stuff that computers are able to do for us, but really the magic is in the algorithms that are programmed into the computers. Algorithms control the behaviors of computers so they know how to navigate and solve our problems, but computers are not the only thing that algorithms can control. In this post I want to describe a low-tier algorithm that I recently noticed who started possessing me and controlling my actions. Hopefully by calling it out and analyzing it in this post I can convince it that I am not a computational machinery with a low level of intelligence, and it will leave me to possess other people instead (be careful y’all).

Before I dive into describing this algorithm, let me give credit to the book that heavily inspired this blog post, Algorithms to Live By. The book balances the math and algorithms with intriguing stories and real life scenarios, written in a way that evokes curiosity and wonder. I strongly recommend anyone who loves learning about algorithms to give it a read! Also, I am compiling my thoughts about the chapter “Explore and Exploit” for a future blog post, so keep an eye out for that…

And without further ado, the algorithm I want to talk about concerns

Po from Kung Fu Panda complaining about stairs
Wuliuqi and Dabao from Scissor Seven panting at the top of a flight of stairs

Stairs are everywhere, and rightfully so. A flight of stairs reduces the risk and energy required to translate your body in the vertical direction by allowing you to step up evenly spaced sections of concrete onto flat surfaces with high friction. Most people choose to walk up stairs one at a time; however, my energy is limitless but my time on this earth isn’t, so I often find myself bounding up stairs two at a time. Although faster, this kind of behavior with a step size of 2 (let’s call this SS=2 for short) introduces a problem that wasn’t present when SS=1: a potentially larger difference between the soreness felt in each leg. Let’s call that the Leg Soreness Difference, LSD for short. (Note that I will be using a simplified equation to compute LSD. That is the one that only computes the absolute difference between the total number of steps each leg has taken, not the one that differentiates between one step of two stairs and two steps of one stair, nor the one that also considers in the speed of ascent)

When SS=1, it takes two stairs to zero out the LSD: one stair for the left leg and one for the right. Thus one way to solve for the maximum LSD is to consider two cases: the first where the total number of stairs, let’s call that N, modulo 2 is 0 (N is even), and the other case when N mod 2 is 1 (N is odd). The first case is trivial since every two steps the LSD zeros out, so it ends up being 0. The second case we end up with one extra stair after zeroing out the LSD, so LSD=1 and the maximum LSD value for this approach is 1.

This all changes when SS=2. Since it takes four stairs to even out the LSD now, we have 4 cases to consider to solve for the maximum LSD. The first two cases, when N%4==0 and N%4==1, can be reasoned similarly to the two cases when SS=1, and this gives us a LSD=0 and LSD=1 again. The fourth case where N%4==3 isn’t the issue: when we have 3 stairs left with SS=2, we step up the first 2 stairs with one leg and the last one with the other, leaving us with LSD=1. The problem is in the third case, when N%4==2. When there are 2 extra stairs to take after zeroing out the LSD, we would just step up the stairs with one leg, which leaves us with a LSD of 2. How do we reduce the maximum LSD value with this approach?

One fairly straightforward way is to just take those last 2 steps one at a time. However, that requires counting the steps as we travel up the stairs, since we want to distinguish between the N%4==0 case and the N%4==2 case (if we take the last 2 steps one at a time in the N%4==0 case, we will end up with LSD=2). If that sounds like something you want to do, go for it. I, however, want to reserve my working memory while I’m walking for more important matters like cubing prime numbers, so let’s look into other possibilities.

The algorithm that falls into mind is to take the first step with SS=1, then the rest with SS=2. Below I will show a proof of why this works better than the previous approach.

Let’s formalize the problem by looking at the 4 cases we have closely. Since LSD is a difference between the two legs, let’s separate it and set LegA(N) to return the amount of soreness in the first leg that steps up that contributes to the LSD when going up N stairs 2 steps at a time. LegB(N) returns this value for the second leg that steps up. Let’s look at a table of values for these two functions:

Let’s also try to model the algorithm of stepping up the 1st stair with the first leg, then switching to 2 steps at a time for the rest of the stairs. We can define the function G(N) to return (legA, legB), where legA is the amount of soreness in the first leg that contributes to the LSD when going up stairs this way, and legB is that same value for the second leg. Since we determined the first step to always be of size 1, and then we bound up the rest of the stairs starting with the second leg 2 at a time, we can set

G(N) = (legA, legB) = |(1, 0) – (LegB(N-1), LegA(N-1))|

where (1, 0) refers to the first step we make with leg A, and (LegB(N-1), LegA(N-1) refers to stepping the rest of the stairs with the previous algorithm. LegA(N-1) takes the legB spot in the tuple since we are starting the SS=2 steps with the second leg instead of the first one in the context of G.

Filling out the values for the table this time, we can see that there are no longer any values of 2 left.

On top of that, there are more 0s than the previous table, and the 1s are more balanced. At first glance, all this seems to show that this is a much better strategy to go up stairs than the previous one. However I have to admit that this algorithm made some liberal assumptions with the simplest form of the LSD formula. If one starts differentiating the energy usage of 2 step sizes of 1 and 1 step size of 2, then the algorithm no longer seems ideal. Perhaps the only way one can keep the LSD at in every case is to hop up the remaining stairs with both of your legs.

What is an algorithm that you use to optimize your life? Leave a comment down below to educate me on the superiority of your algorithm.

(Oh also, I originally planned to write about two other algorithms, but I spent too much time on this one so let’s leave the remaining ones for a future post. Stay tuned!)

Leave a Reply