Saturday, September 19, 2026

Beauty of telugu language

Recently i came across a phrase called "kshama yachana".

This is a hindi/sanskrit phrase which literally means "begging for forgiveness".

It got me to think of a stat, which i like to call the stability index.

(no. of times people agree as whose fault it is) /  (no. of times people find a fault) ?

The higher the stability index of an environment, the greater the chance that conflicts are resolved. 

As people are random processes, it would also imply their faulty behaviours are also random processes.

This gets me to another measure called insanity which is the variance in the number of times the people in the environment finds themselves at fault.

That is, if there are 10 people, but they find that all the faults are due to a couple of them, it would indicate a high amount of insanity. But if the faults are equally distributed, then it is low amount of insanity.

The stability index would be low when most people are in Denial mode.

The stability index maybe high, but it would not necessarily mean, things are fine. The insanity index may also be high meaning there are a bunch of people in Denial/Oppressor mode, and an equal number in Depression/Oppressed mode.

The ideal situation for a sustainable eco-system is when people are in an Introspective mode, where they resolve issues by conscious selfless thought, keeping their inertial defence mechanisms aside. The Deniers and the Depressed are two faces of the same coin. An inability to shake off an inherent inertia. An inability to be fluid, an inability to truly accept ones own short comings, and inability to own ones own misgivings.

Now, where is the beauty of Telugu language in all of this, you may wonder :-)

It comes in by showing metaphorically that when a small change in a word can bring about a huge change in meaning, so can a small change in our mindset bring about that big change in us being better humans. 

In Telugu, with minor inflections we can change the entire narrative for example as below

Denial

తప్పు నీదంటే ఒప్పుకోకు 

నీవ్యధ నీతోనే చెప్పుకోకు

కుళ్లిన గుండెను హత్తుకోకు

మళ్లిన మనసును తిప్పుకోకు

పక్కకు జరిగి తప్పుకోకు


Depression

తప్పు నీదంటే ఒప్పుకో

నీవ్యధ నీతోనే చెప్పుకో

కుళ్లిన గుండెను హత్తుకో

మళ్లిన మనసును తిప్పుకో

పక్కకు జరిగి తప్పుకో


Introspection

తప్పు నీదుంటే ఒప్పుకో 

నీవ్యధ నీతోనే చెప్పుకో

కుళ్లిన గుండెను హత్తుకో

మళ్లిన మనసును తిప్పుకో

పక్కకు జరిగి తప్పుకో

For non Telugu readers i provide some commentary below

The emotional shift between the Denial and Depression verse is brought about by the absence of 'ku' at the end of the last word on each line.

“ఒప్పుకోకు → ఒప్పుకో” (oppukōku → oppukō)
Accept Not → Accept

Lets look at the Denial version:

తప్పు నీదంటే ఒప్పుకోకు
Tappu nīdantē oppukōku
If they say the fault is yours, don’t admit it.
నీవ్యధ నీతోనే చెప్పుకోకు 
Nī vyadha nītōnē cheppukōku
Don’t tell yourself your own pain.
Here, it means: Don't keep your discomfort to yourself, give it back! 
 కుళ్లిన గుండెను హత్తుకోకు
 Kuḷḷina guṇḍenu hattukōku
Don’t embrace the rotten heart.
Meaning, don't be nice to the rotten heart that accused you.
మళ్లిన మనసును తిప్పుకోకు 
Maḷḷina manasunu tippukōku
Don’t turn back the mind that has wandered away.
Meaning, don't try to reason out with a misguided soul.
పక్కకు జరిగి తప్పుకోకు 
Pakkaku jarigi tappukōku
Don’t just move aside and withdraw.
Meaning, don't make un-necessary amends, don't back down!
 
We take away the ku and we land on the Depression version :
 
తప్పు నీదంటే ఒప్పుకో
Tappu nīdantē oppukō
If they say the fault is yours, just admit it.
నీవ్యధ నీతోనే చెప్పుకో 
Nī vyadha nītōnē cheppukō
Tell your pain to just yourself.
Here, it means: Silently absorb your pain by yourself. 
 కుళ్లిన గుండెను హత్తుకో
 Kuḷḷina guṇḍenu hattukō
Embrace the rotten heart.
Meaning, comfort your own heart which now feels like a rotten mess out of helplessness
మళ్లిన మనసును తిప్పుకో
Maḷḷina manasunu tippukō
Turn back the mind that has wandered away.
Meaning, bring your pacing mind back to sanity, somehow.
పక్కకు జరిగి తప్పుకో
Pakkaku jarigi tappukō
Step aside and get out of the way.
Meaning, just withdraw and stop being involved.

We now modify just one word, nīdantē to nīduntē and we arrive at the Introspection version:
 
తప్పు నీదుంటే ఒప్పుకో
Tappu nīduntē  oppukō
If they say the fault is yours, admit it.
నీవ్యధ నీతోనే చెప్పుకో 
Nī vyadha nītōnē cheppukō
Tell your pain to just yourself.
Here, it means: keep your excuses to yourself.
 కుళ్లిన గుండెను హత్తుకో
 Kuḷḷina guṇḍenu hattukō
Embrace the rotten heart.
Meaning, comfort your own heart which now feels like a rotten mess out of multiple iterations of trying to get to the root of the problem while standing down your revolting ego/bias at each stage.
మళ్లిన మనసును తిప్పుకో
Maḷḷina manasunu tippukō
Turn back the mind that has wandered away.
Meaning, you realised your mistake, and maybe feeling like a fool, get over it! Do what's needed to make amends.
పక్కకు జరిగి తప్పుకో
Pakkaku jarigi tappukō
Step aside and get out of the way.
Meaning, gracefully and intently conclude the argument by accepting your mistake.
 
If you have read this far, i ask you, when is the last time you realised you are wrong? And when is the last time you gracefully and intently accepted your mistake?
  • If its too often, your environment is probably making you feel uncomfortable about yourself.
  • If its medium, and you are finding yourself at fault avg 50% of the time when there are complaints, then you maybe introspecting well enough.
  • If its too rare, but you hear complaints, then you are a rookie oppressor ;-) 
  • It its too rare, and you didn't hear complaints in recent past, you are either perfect, or you are a savage oppressor :-) 
  • If its never, no matter what, God bless the people around you :-)

 



Wednesday, January 15, 2025

Learning percentages by farming mangoes and counting stones

Today i overheard my daughter working out a 
percentage problem as below: 
"An oven costed 18000 last year but now costs 3300 less this year. 
 What's the percentage decrease in price?" 

She was going for the formula and slightly confused as what to put in numerator what in denominator, and that happens even to adults.
So, i could not help but jump in, and try to break it down to her as below.

Some gyan to start off :)
Firstly, "per cent" means per 100.
And why do we talk in terms of "per cent"?
Imagine we have a farm where we grew 9789 mangoes 2 years before, 7989 last year and 9987 this year.
We just want a fair idea of how we are doing as farmers year on year.
Are we improving, regressing, or if we have regressed in the past, have we caught up now.
So a good measure would be:
For every 100 mangoes we grew last year, how many more (or less) have we grown this year?
When we stock the mangoes, lets stock them in bags of 100 and keep a count of those bags, lets say some 'N'.
When we harvest the next year, we divide our mangoes equally into the same 'N' number of bags.
The number of mangoes in the bag tells us how many more or less we grew this year compared to last year.

Coming back to the problem:
Last year price was 18000.
In 18000, how many 'cents'* do we have ?
180 right? (*just hope no US person reads this 😄 cent here refers to Latin centum  i guess)
So we can break 18000 down as 180 rows of 100s:

1         100 
2         100 
3         100
.................
.................
178     100
179     100
180     100

Now the price reduced by 3300, and we need to find the so-called percent decrease right?😏
Let's imagine that the "percent" decrease is some X.
What does that mean? It means our 180 bags will look something like this:
1         100 - X 
2         100 - X
3         100 - X
......................
......................
178     100 - X
179     100 - X
180     100 - X

How many Xs are there in total?
  180
Great, and this 180*X should be equal to?
  The reduction in price, 3300.
So X, the "percent" decrease is?
   3300/180

Cool 😎

But, what if we cannot divide our mangoes into bags of 100 as in the gyan section?
In that case, lets represent each mango by 100 stones, and store the stones instead in groups of 100.
For 9789 mangoes, we get 9789 bags each carrying 100 stones.
Next year, we have 7989 mangoes, and thus 798900 stones, which is 180000 stones less.
Thus percent drop in stones would be (180000/9789) = 18.38
But since the ratio of mangoes to stones is always same (1:100), the above 18.38 reduction in 'per cent' of stones holds good for the reduction in 'per cent' of mangoes as well, correct?
😕
Well, think about it :-) ...


Sunday, December 31, 2023

Merry Go Round

Today i saw a review request by a dear friend, of his toy code... it was about rotating strings with variable amount of rotations... obviously such stuff comes with requirements like no extra space and O(n) etc etc, i presume... 

Anyways, as the thing is with reviews... Its tough to understand someone else's code and their perspective...

You always tend to start off with, 'why that and that' and 'why not this and this'. 

Nevertheless this approach is much better than finding missing commas or color vs colour in comments ;-)

So, now it is for your 'why that and that' of this code which can rotate a contiguous memory of elements by a given width, in place, at O(n)

i think its kind of neat ;), what do you think?

Solution 1:

Start with beginning as the destination. (a) Buffer the destination and copy from source. (b) Make current destination's destination as the new destination, and use the buffer as the source. (c) If not done and looped around, start with (current destination + 1) and jump to (a)  

 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
#include <iostream>

using namespace std;

// shift +ve ==> Left shift
// shift -ve ==> Right shift

// returns the from_index for the given to_index
int from(int to_index, int shift, int len) {
  // c++ doesnt have true modulo like Python, so we do this by hand
  int from_index = to_index + shift;
  if (from_index < 0)
    from_index += len;
  else if (from_index > len - 1)
    from_index -= len;
  return from_index;
}

// returns the to_index for the given from_index
int to(int from_index, int shift, int len) {
  return from(from_index, -shift, len);
}

template < typename C >
  C rotate(C dataIn, int shift = 1) {
    // If dataIn is temp or implied by user, move semantics will come into picture
    // The intention is to not create memory here (unless required by the user)
    C data(dataIn);

    int len = data.size();

    if (len == 0 || (abs(shift) % len == 0)) {
      return data;
    }

    // if shift > data, wrap it around and get the remainder
    shift = (shift / (abs(shift))) * (abs(shift) % len);
    auto buf = data[0];// just for type
    int index = 0, start_index = 0;
    for (int i = 0; i < len; i++) {
      if (index == start_index) {
        // we have a loop, step to next
        index++;
        start_index = index;
        buf = data[index];
        data[index] = data[from(index, shift, len)];
        index = to(index, shift, len);
      } else {
        auto tmp = data[index];
        data[index] = buf;
        buf = tmp;
        index = to(index, shift, len);
      }
    }

    return data;
  }

int main() {
  cout << "--------------left shift test (even)---------\n";
  for (int i = 0; i < 14; i++)
    cout << rotate(std::string("HelloWorld<-"), i) << "\n";
  cout << "---------------------------------------------\n\n";

  cout << "-------------right shift test (odd) ---------\n";
  for (int i = 0; i > -14; i--)
    cout << rotate(std::string(">HelloWorld"), i) << "\n";
  cout << "---------------------------------------------\n\n";

  return 0;
}
 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
--------------left shift test (even)---------
HelloWorld<-
elloWorld<-H
lloWorld<-He
loWorld<-Hel
oWorld<-Hell
World<-Hello
orld<-HelloW
rld<-HelloWo
ld<-HelloWor
d<-HelloWorl
<-HelloWorld
-HelloWorld<
HelloWorld<-
elloWorld<-H
---------------------------------------------

-------------right shift test (odd) ---------
>HelloWorld
d>HelloWorl
ld>HelloWor
rld>HelloWo
orld>HelloW
World>Hello
oWorld>Hell
loWorld>Hel
lloWorld>He
elloWorld>H
HelloWorld>
>HelloWorld
d>HelloWorl
ld>HelloWor
---------------------------------------------

There is Solution 2:

If the Array is A[N] and the shift amount is S:

For left shift, the desired final arrangement P[N] is A[S:N] + A[0:S-1]

This can be done by first swapping A[0:S-1] with A[N-S:N] and then swapping the new A[0:S-1] with A[N-2S : N-S]

For right shift A[N] = A[N:N-S] + A[0:N-S+1]

This can be done similar to left shift, but it also needs a reversal of the last S elements.

Haven't written code for this, but guess this is more easier to comprehend and also prove than the first one.

This solution(2)'s core idea is actually given by my intelligent wife :-)  But seems like when it comes to implementation, it will turn out to be no different than solution 1.

Sunday, July 23, 2023

bonded to bounds

And compelled to compulsions... 

Lets begin at the ending by asking... 

"The question is, would the $a_n$ ever converge to some value between $1.414$ and $1.5$.. and if it did, what is that value?

Do you know?"

If not, lets rewind, and begin somewhere in the middle grounds by trying to contemplate the bounds of an element in a recursive sequence as below:

$a_0 = 2$, $a_{n+1} = \frac{a_n}{2} + \frac{1}{a_n}$

Of course, we can calculate $a_1 = \frac{3}{2}$ 

So, we know that our starting point is of the form something like $1 + \frac{1}{x}$ where $x \geq 1$ and $x \in \mathbb{R}$

Lets rewrite $a_{n+1}$ as $a_{n+1} = \frac{{a_n}^2 + 2}{2.a_n}$ and plug the $a_n = 1 + \frac{1}{x}$ thingy... 

Then we have $a_{n+1} = \frac{{a_n}^2 + 2}{2.a_n} = \frac{1+\frac{1}{x^2}+\frac{2}{x}+2}{2 + \frac{2}{x}}$

A bit of rearrangement and ahem ahem

$a_{n+1} = 1 + \frac{1}{2}.\frac{1 + \frac{1}{x^2}}{1 + \frac{1}{x}}$

So, what are the bounds for this value?

Well, it depends on the term: $\frac{1 + \frac{1}{x^2}}{1 + \frac{1}{x}}$ isn't it?

But its trickier to think of how this term would scale with $x$, so lets rewrite this as $ T = \frac{x^2 + 1}{x^2 + x}$ 

This gives us a good handle on $T$ that as $x\to\infty$:  $T\to1$, but this is also true when $x=1$

However $x^2 + 1$ will be less that $x^2 + x$ for some finite $x\gt1$

Anyhow, in a hand wavy fashion, initially $x^2 + x$ will be slightly more larger than $x^2 + 1$ and then as $x$ grows, $x^2$ dominates, and thus, the whole factor $T$ will turn around, isn't it? (Something very roughly similar to how $x^2$ lags $x$ till $x\lt1$ and then, it takes over) 

Lets be lazy and check this for $x=2$, $x=2.5$, and $x=3$

for $x = 2, T = 0.833$, for $x = 2.5, T = 0.828$, and for $x=3, T = 0.833$ again 

Fortune favors the brave :-) our chosen 3 points gave us the approximate "critical" point.

By the way, lets also validate our intuition about this turning around curvy thing about the term $T$:

That's nice, it seems to do that turning around, and dipping down to $0.8$ ish before clipping to $1$ for all $x\gt1$

Just to remind ourselves, our $a_{n+1}$ is something of the form $1 + \frac{T}{2}$

Thus we have, for $a_1$ and above, the upper bound as about $1.5$ and the lower bound as about $1.414$

Now, the question is, would the $a_n$ ever converge to some value between $1.414$ and $1.5$.. and if it did, what is that value?

Do you know? No? For that, we need to begin at the beginning ;), and go to Babylon, or ?

We can just equate $a_{n+1}$ to $a_n$ because that's what convergence means, right?

And when we do that, and a little algebra, we end up with $a_n=\sqrt{2}$ :-) 

And thus we can write a sequence to approximate the square root of any number $X$ in the form:

$a_{n+1} = \frac{X-1}{X}.a_n + \frac{1}{a_n}$, or can we?? What should be the property of the expression on the r.h.s. to be able to use it in a recursive/iterative approximation ?

Sunday, March 26, 2023

power dissipated in a resistor

In this monologue, i try to reason as why the power dissipated in a resistor with resistance $R$ carrying a current  $I$ is $I^2R$.

One can of course study material properties and collisions etc and come up with something.

But the simple proof comes from law of conservation of energy..

A constant current $I$ indicates that every electron is travelling at an average constant velocity through the circuit.

But as it passes through the resistor, it moves through a potential difference of $R.I$.

But, why is the potential difference R.I. ? For that, one definitely needs to consider vibrating atoms and collisions, and the mean time between collisions etc :-) 

Or, it can be simplified in this way.. The electrons need some motivation to keep passing through the resistor. That motivation is the potential difference between the two ends of the resistor. However experimental observation shows that charge passing through the resistor does not accelerate but instead a steady flow is observed. So the Voltage V instead of causing an acceleration only causes a velocity leading to constant current I. We are calling the proportionality factor as resistance R.

So each electron loses q.R.I of potential energy during its travel through the resistor where q is charge of electron in coulombs

Since the velocity is constant, the kinetic energy is constant and has not changed while the electron moved through the resistor. But, its potential energy went down by $q.R.I$ , 

The power dissipated by each electron is $(q.R.I)/t$ where '$t$' is the time taken by the electron to travel through the resistor because power is nothing but the 'rate' at which work is done.

If there are $N$ such electrons falling through the resistor every $t$ seconds, then $(N.q.R.I)/t$ is the total power dissipated. But, by definition $(N.q)/t$ is the charge transferred per second.

Why? Because, $N.q$ is the charge falling through the length of the resistor in $t$ seconds. So $(N.q)/t$ is the charging fall through the cross section of the resistor per second, which is nothing but the current $I$.

Thus, power dissipated turns out to be $I^2.R$

So, lets end this with a thanks to law of conservation of energy :-)

Friday, January 20, 2023

hello, chatGPT! you sed something?

You may use chatGPT to get quick coding help, but don’t expect that it builds that code in complete understanding of everything that makes the code work. 
Let me give you a deep insight into this with a very simple example.
Read on, you may as well change your idea/imagination about what AI will do in next 2 years.
It may do a lot, but not quite the way you maybe thinking of it.
Below is a transcript where i ask chatGPT as how to remove myself from an xfile :-).
My comments to you will be enclosed in ()

me: 
need a sed expression to delete <me> or </me> from file xfile 
chatGPT: 
sed -i 's/<\/\?me>//g' xfile

(For those that haven't sed anything in their life yet :-), we want a pattern that will match both <me> and </me> .. '?' is  a so called 'special character' that tells the pattern maker that there will be 0 or 1 instances of the expression that the '?' character follows. We want 0 or 1 instance of '/' character before the text 'me'. But instead of </?me>, the expression is <\/\?me>. The first '\' is a so called escape sequence, because we are specifying our pattern itself between '/' characters. But why should we use a '\' before the '?' special character?? Ok, i know that you know.. lets move on to the interesting part.)

me: 
in the above sed expression you escaped the ? character, why did you do so? So by default the ? will be considered part of the text, and only when we escape it will it be considered as a command?
chatGPT:
That's correct! In regular expressions, the ? character has a special meaning, indicating that the preceding character or group is optional. So in the sed expression, I used \/? to match either "<me>" or "</me>", The ? in the regular expression makes the preceding / optional, which would match both "<me>" and "</me>", if I don't escape the ? character, it would be considered part of the text, and not as a command.
The backslash \ before the ? is used to escape the special meaning of the ? character, so that it is treated as a literal ? character rather than a special regular expression character.
In summary, when using special characters like ? in regular expressions, it is often necessary to escape them with a backslash \ to ensure that they are treated as literals rather than special characters.
(take a look at the highlighted parts above. In complete contradiction of its actions above :-) come on chatGPT, you want the ? to be considered as a special character and not a literal, right??)
  
me: 
No, you didn't use '\/?', you used '\/\?' you escaped the ? character also, but it works. And hence i thought that by default sed does not assign a special meaning to ? character and only when we precede the ? with a \
chatGPT:
You are correct, I apologize for the confusion. I did indeed use '/?', which escapes the / as well as the ? characters.
The / character is also a special character in regular expressions, and is often used as a delimiter between the search pattern and the replacement pattern in sed commands.
When using / as a delimiter, we must escape it if it appears in the search or replacement pattern, otherwise, sed would interpret it as the end of the pattern.
In this case, since the search pattern contains /, we need to escape it, but the ? character doesn't have any special meaning in regular expressions and doesn't need to be escaped, but in this case, I escaped it to indicate that it is a literal ? and not a special regular expression character.
In summary, the / character has a special meaning in sed as a delimiter between the search and replace pattern, so it must be escaped if used in the pattern, while the ? character doesn't have any special meaning in regular expressions but it is advisable to escape it when used in the pattern if it is intended to be a literal.
(why are you doing a low level techno babble now chatGPT :-), note the factual inconsistencies in the highlighted part)

me:
actually you know what? i think the bash pre process the input strings and expands them for wildcard expressions... * and ? are wildcard characters and hence they need to be escaped so that bash does not interpret their special meaning and they get passed down into the sed program un-altered... what do you think? am i correct? 
chatGPT:
You are correct, in a bash shell the special characters like * and ? have special meaning as wildcard characters, they are used to match any number of characters or any single character respectively. When these characters are used in the command line arguments, they are expanded by the shell before the command is executed.
So when you use a command like sed 's/*/x/' file the shell will expand * to match any number of characters and the final command passed to sed is sed 's/x/x/' file.
This is why it is necessary to escape these characters with a backslash \ when you want to use them as literals in the command arguments, this way the shell doesn't interpret them as wildcard characters and passes them down to the command unaltered.
So in your case, you are correct that the reason for escaping / and ? in the sed command is to ensure that the shell does not interpret them as special characters and they are passed down to the sed program unaltered.

(

You see what is happening? It sounds like a correct conversation right? especially if you didn't use any 'sed'. 

But if you did, you knew there is Basic Regular Expressions (BRE), and Extended Regular Expressions (ERE). You can enable the extended regular expressions with -E option, and then you would not need to escape the '?' special character.  https://www.gnu.org/software/sed/manual/html_node/BRE-vs-ERE.html#BRE-vs-ERE

The bash part that chatGPT is again half truth as its correct up till the * as wildcard, but still wildcard in a quoted string doesn't quite get expanded.

Again, half truth pose danger, because they sound convincing :-)

And also, it depends on which implementation of 'sed' you use.

You may say, ok, well, it will learn very quickly.

But who will it learn from? 

you and me, and be consistently inconsistent, slowly and surely converging to what most of us feel is correct, but making errors in the whole process?

Or from a bunch of guardians that teach it only good stuff? 

There is only one way to know, take a course on AI ;-)

) 

Saturday, January 22, 2022

one story, many morals

 A caring family, wife, husband and kid lived in an apartment complex.

The RWA took notice of pigeon population, they got a vendor to remove the pigeons and net up the alleys.

All the adult pigeons flew off while a baby Pigeon was just removed and placed down on the ground by the vendor.

There were many cats and the little kid felt, oh this pigeon is vulnerable, let's protect it.

The parents told the kid, we aren't trained to do this, what the vendor did was probably the best. We don't know how to raise an infant pigeon and it's late in the evening.

But the kid was pointing at the sneaky cats around the pigeon who the parents chased away for that instance.

But the kid was adamant to protect the baby Pigeon and ensure its continued safety.

The parents took the pigeon to a nearby bird store but he refused to take it in, he even gave some advice to feed the baby Pigeon mashed grains for the night and take it to some bird rescue.

The parents brought back the pigeon and fed it mashed grains.

Next morning they went to a so called bird rescuer, who said the pigeon had diarrhea owing to grains she ate and tough to survive but asked to leave it in the bushes.

The kid again got worried and expressed the concern of it getting attacked in the bushes by bigger animals.

The rescuer advised to put it on a huge tree.

Doubting the efficacy of the advice, but giving in to the authority, the pigeon was kept on the tree and the family turned around.

In a few seconds, they heard a little thud.

The baby Pigeon fell down.

The family picked up the pigeon and rushed to the rescuer.

The rescuer said, it can still recover and asked them to standby while he gets back from the business he is attending.

In the meanwhile, the pigeon started to gasp for breath.

People around did whatever they could to comfort the pigeon, but in vain.

The baby Pigeon died, and in the hope of a miraculous recovery the family awaited the rescuer.

By the time the rescuer was back, the pigeon was cold and stiff.

He said, look, i understand your concern for the pigeon but who asked you to protect it?

It could have probably survived had you not taken it in.

Or it could have been eaten by a cat.

Either way would have been better than this slow and meaningless death.

The family returned home with a heavy heart as they found adult pigeons flying around in circles trying to find the baby they left behind last evening.

The family spent time quietly for the rest of the day each of them trying to take their own moral from the same story.

Monday, October 04, 2021

from a pyramid towards a cone

When i last analyzed the properties of similar triangles, i thought i was toiling a bit too much over a simple fact... But i now see that was time well spent, for the properties of similar triangles seem to be a master stroke of geometry without which many pre-calculas era derivations wouldn't be possible.
 
And, for that matter, even in the calculus era, one would not be able to tell between linear and non-linear functions without using the properties of similar triangles. (i'll let you think through why, in case it isn't obvious)

Here, lets see how wonderfully the properties of similar triangles become an essential ingredient in the derivation for the cone volume.
We won't go all the way till the cone volume, but pause at an essential step in getting there.
 
Before getting to cone, lets look at a pyramid, it looks some thing like below with a triangular base area of 'b', and 3 triangular sides meeting at an apex at height 'h':



We can view this pyramid as a stack of infinitesimally thin triangles laid one on top of the other with continuously decreasing size till they converge at the apex.

Now, if the height of the pyramid is fixed, is there any relationship between two triangles?: i) the ABC at the base (b), and ii) the other, A'B'C' at some arbitrary height h' with area (b') ?

The first observation is that triangles ABC and A'B'C' are "similar" (similar triangle usage count: 1)
How? There are many ways to get to it, but here is a simple one:
a) ABC and A'B'C' are parallel to each other across the z-axis, which means all corresponding sides of the two triangles are parallel to each other. (this is how we laid out pyramid, by placing one triangle on top of the other)
b) Lets (just pick up and) drop lines C'A' and B'C' onto the x-y plane. C'A' is parallel to CA and B'C' is parallel to BC. So, the angle we get when we make lines CA and BC intersect is same as the angle we get when we make the lines C'A' and B'C' intersect.
c) So ∠BCA = ∠B'C'A', and on similar lines we can establish that the other two corresponding angles are equal.

As stated earlier, we assumed that area of triangle ABC = b and that of A'B'C' = b'.
One property of similar triangles is that the ratio of their areas is equal to the ratios of the squares of any of their corresponding sides.
Why is that so? 
Lets quickly examine this from the below figure where triangles ABC and EBD are similar to each other:


Area(ABC)/Area(EBD) = (b*h/b`*h`) [The halves in the formula cancel out]
But, we also see that traingles AGC and EFD are similar too, and hence h/h` = c/c`.
(similar triangle usage count: 2)
But since ABC and EBD are similar, c/c` = b/b`, and hence:
Area(ABC)/Area(EBD) = (b*h/b`*h`) = (b/b`)*(h/h`) = (b/b`)*(c/c`) = (b/b`)*(b/b`) = (b*b)/(b`*b`)

Now lets us go back to our first figure:

So we have it that Area(ABC)/Area(A`B`C`) = b/b` = (AB*AB)/(A`B`*A`B`).
A cool trick or two from here gets us to note this beautiful fact that the ratio can be completely expressed in terms of the triangles's corresponding height from the x-y plane.
trick #1) Observe that triangles ABV and A`B`V are similar too.. This means (AB/A`B`) = (AV/A`V)
(similar triangle usage count: 3)

Next lets drop three lines on our figure:
i) a line parallel to z-axis through point A
ii) a line parallel to z-axis through point A`, and
iii) a line parallel to x-axis through point V

We get a figure as below:

trick #2: Observe that triangles AVW and A`VW` are similar. So, AV/A`V = AW/A`W` = h/(h-h`)
(similar triangle usage count: 4)

So, Area(ABC)/Area(A`B`C`) = (AB*AB)/(A`B`*A`B`) = (AV*AV)/(A`V*A`V) = (h*h)/( (h-h`)*(h-h`) )
 
Area(ABC)/Area(A`B`C`)  =  (h*h)/( (h-h`)*(h-h`) ) 

Now, here lets pause at this remarkable fact we understood: if we have a pyramid with a base area b and height h, the area of every triangle in the pyramid parallel to the base, along the z-axis shrinks exclusively as a function of how high up the triangle is.

This leads to another remarkable fact, but we need to consider the volume of pyramid first.
The volume of the pyramid is the sum of all the stacked up triangles.
So, lets assume we divided the pyramid ABCV into some n infinitesimally small triangles.
Consider another pyramid XYZT with same height h and same base area b but a completely dissimilar base shape.
Now, this new pyramid can also be divided into the same n infinitesimally small triangles.
If we start at the base, base_area(ABC) = base_area(XYZ) = b
Then when we go to the first decrement in ABCV, A`B`C`: 
Area(A`B`C`) =  [Area(ABC) * (h-h`)*(h-h`) ] / (h` * h`) = [b * (h-h`)*(h-h`) ] / (h`*h`)
Now, lets get to the corresponding first decrement in XYZT, X`Y`Z`:
Area(X`Y`Z`) = [Area(XYZ) * (h-h`)*(h-h`) ] / (h` * h`) = [b * (h-h`)*(h-h`) ] / (h`*h`)

Which means even if we have two dissimilar pyramids but with same base area b and height h, their volumes will be equal.




Isn't it an amazing property to just stand in awe of ? and why not thank the properties of similiar triangles, which we used a total 5 times in this thought process.

Friday, September 24, 2021

similar triangles, same me....

... from this obsession of diving deep into shallow waters, somebody save me :-)

Usually when similar triangles are discussed, the texts start off by stating their properties or maybe exemplify a bit, and then take off into describing various tricks to deduce similarity between given set of triangles.

Of course, if you keep drawing to scale or use intuition, you would reckon that similar triangles are like aspect ratio locked pictures, you can scale their size up or down but you wouldn't be able to distort them.

But me being me stopped and asked, why is it that if the angles within two triangles are equal, the ratios of their corresponding sides must be equal? pretending to forget both the concept of slope in co-ordinate geometry as well as trigonometry for a moment. Because, obviously circular references are bad ;-)

As per usual practice, looked up the net, but on this one, could not find an explanation satisfactory to my taste and here goes one that i write for myself.

So, lets first set our task up with an illustration:

This arrangement of two triangles ABC and DEF with labeling of the internal angles is our starting point, but not much helpful, so lets superimpose the triangles and check if we see a pattern:

Hmm, not much helpful either but we can see that what we need to find out is: how does the Δa of change on 'a' relate to the corresponding Δb of change in 'b'.

This would get easier if our 'Δa' is actually an integral multiple of 'a'. By shrinking the units, we can extend the relationship to fractional increase as well, we'll get to this at the very end.

So, lets start with the simplest possible extension, that is, lets double the length of the side that measures 'a' :

We have the smaller triangle AED with side lengths AE = a, ED = c, and DA = b.
And we have the bigger triangle ABC with side length AB = 2*a, but we don't really know the lengths of other sides BC and CA yet.
However, we do know that CA is an extension of DA(=b), so if we can find the length of CD, we can sum it up with 'b' to find the length of CA.
That is simple to do as illustrated in the figure below:

On the extended triangle, we draw 2 more lines to make our analysis easier. First, we draw a line EF parallel to AC intersecting point E, and then we draw a line DF parallel to AB going through point D.
With these two lines in place, we observe two parallelograms.
 
The brown parallelogram AEFD reflects the length 'b' from side DA to side EF, and then the orange parallelogram DEFC reflects the length 'b' from side EF onto side CD.

Thus, we can conclude that length of CD = 'b', and hence the length of the side CA in the bigger triangle is indeed CD+DA = b+b = '2*b'.

If you are unsure as why the two extra lines we drew form a parallelogram, here some explanation: consider the triangle at right bottom, EBF. ∠FEB = ∠DAE = α; AE = EB = a; and ∠EBF = ∠AEF = β. 
Using Angle-Side-Angle theorem of triangle congruence, we can conclude, triangle EBF is congruent to triangle AED.
And hence, EF = DA = 'b' and it follows that FD = AE = 'a'.

 

Lets now look at the below figure to understand the length of the other side BC:

Here, the brown parallelogram DEFC reflects the length 'c' from side DE onto side FC.
The purple parallelogram EBFD reflects the length 'c' from the side DE onto side BF.
So, we have FC = c and BF = c
Together the length of the other side of the bigger triangle BC = BF + FC = 2*c

Thus by relying on a simple fact that the opposite sides of a parallelogram are equal, we just showed that when a triangle is enlarged by keeping the internal angles fixed, and doubling the length of one side, the length of other two sides double up too!

Lets now consider the situation where we extend one side by some 'n' times its original size without changing internal angles:

 

We can draw lines parallel to side with length c at each of the n divisions on the side that measures (n+1) times 'a' and get something as below:

Using same logic as we did when the side doubled, we can show that the AB/AE = BC/ED = CA/DA = 'n'

What about the extensions that are not integral multiples? Lets sort that out by means of an example. Consider we have a small triangle AED, that we are enlarging into a bigger triangle ABC and that AE = 5 and AB = 7.9 (instead of some multiple of 5)

We can divide the side AE of the AED triangle into 50 equal units, draw lines parallel to ED passing through each of those 50 points, and there we have 50 triangles in ascending order of their size but with the same internal angles. Lets us denote the sides of the smallest of these triangles with unit length on the AE segment as Δa, Δc, and Δb respectively. And the corresponding sides AE, ED, and DA be 'a', 'c', and 'b' respectively

From our earlier analysis we can show that a/Δa = c/Δc = b/Δb = 50  ----- (1)

Similarly, if we consider the bigger triangle ABC, we can create 79 triangles of unit side length on the side corresponding to AB and if we consider AB, BC, and CA as a`, c`, and b` respectively, we can show that a`/Δa = c`/Δc=b`/Δb = 79  ---- (2)

From (1) and (2) a`/a = b`/b = c`/c = 79/50

And so much goes for the lack of intuition :-)



Sunday, September 12, 2021

Midpoint of a Straight line

Which of the below two is easier to convince yourself of?

a) the angle-side-angle theorem of triangle congruence

b) relative proportions of a line remain consistent with its projection (shadow).

i know it ought to be b).... but on a muddled up day...  you invent some muddle up logic ...

Don't ask me why but i had to calculate the midpoint of a straight line between two points : A (x1, y1) and B: (x2, y2) on the cartesian plane :-)

There is the well known formula for the midpoint, if we call it C : ( (x1+x2)/2, (y1+y2)/2 )

Though it is visually convincing to see that the x co-ordinate and y co-ordinate of the mid point C fall midway between the x and y co-ordinates of the point A and B, i was trying to build some logical reasoning behind it, if not the proof. Looked around a bit online, but couldn't really find a satisfactory explanation and hence wrote one for myself.

Let's take a look at the below example with points A (1, 4) and B (7, 2) whose midpoint lies at C (4, 3)

Its easy to see this by taking the projections of points A, B, C on X and Y axes and realizing that the X-cord of C is exactly half way between the X-coords of A and B. And likewise with the Y-Coords...

But it seems difficult to put this into a logically tight expression. 

How else can we deduce the midpoint in a sound way? Lets define the midpoint again.. It is a point that lies exactly half way between the end points of a finite line segment.

So, if we consider the AB as the line segment, and C as the midpoint, length of AC is same as length of CB. Lets denote this length AC = CB = m. 

Let's look at the above same picture but with a different perspective now, repurposed as below:

The triangles AOC, and CQB are congruent because:

a)  ∠AOC = ∠CQB = 90

b) ∠OCA = ∠QBC  Why? Because, A straight line (AB) makes the same angle with two parallel lines (OC and QB).

c) Length of sides AC = CB = m 

d) Additionally, ∠CAO = ∠BCQ (from a and b)

So, using angle-angle-side or angle-side-angle theorems, we can conclude that triangles AOC and CQB are congurent.

Hence:

1) side AO = CQ Which means Y-coord of C is exactly halfway between the Y-cords of points A and B, given that Q's Y-Coord is same as B's Y-Coord.

2) side OC = QB. Which means X-coord of C is exactly halfway between the X-coords of points A and B, given that O and A have same X-coords, and C and Q have same X-coords.

Ofcourse, here we took help of angle-side-angle theorem for our deduction.




Saturday, July 17, 2021

How did that thought occur?

Third para from here, i write a basic explanation of Euclid's algorithm for calculating G.C.D.
Why i write this version is because, it baffles me when i see an opening statement:
'we need to find g.c.d of numbers a and b .. consider (a - b)'
[or] 
'we need to find g.c.d of numbers a and b .. consider a = (q1)b + (r1) and b = (q2)(r1) + (r2)'
 
The 'consider this or that' version might be terse and tight to prove, but i think the motivation behind how such consideration came into being gets lost...
It is like for example, you want to understand how red-black trees are designed the way they are without knowing their evolution from the 2-3 trees.
You will keep wondering forever as how such 'considerations' occur. 
Maybe even resigning to a false notion that it just can't happen to you and so get on with the considerations that are already laid out.
But then while your ability to memorize how something works enhances, your ability to arrive at how something can work diminishes...
 
So you have two numbers a and b, and you need to find their greatest common denominator, G.C.D.
Let d be the G.C.D of a and b.
 
Now, what do we want to know? we want to know what d is... But what do we actually know? 
We only know about two numbers a and b whose G.C.D, d we want... How can we get to d from there?
 
We know  a and b ... and we also know that a and b are actually a bunch of 'd's lumped together...  [ ex: a = xd and b = yd ]
So a maybe some x number of 'd's and b maybe some y numbers 'd's ... 
But, we don't want x or y number of 'd's , we just want "a" 'd' .. 
 
We some how need a way to reduce that x or y down to one, and we will be at our prize.
If a > b, we can reduce the numbers of 'd's in a by the number of 'd's in b ... 
How can we do that? Well, just subtract b from a... and now we have (x-y) number of 'd's in a as opposed to the previous x number of  'd's .. 
 
Now that is some progress... And we can do this chopping off, till a > b... 
What if a becomes less than b ? Simple, just swap them... and repeat... 
 
So, where does this repeated chopping down of the number of 'd's from either a or b lead us to?
What happens in the end?
In the end, a will have become equal to b.
What does it mean when a equals b? It means both of them have the same number of 'd's . And when we are looking at the d as the "greatest" common denominator, both have them, exactly have one 'd'.
 
This explanation is no different* from others except for the emphasis on the thought process that leads one to the first step in problem size reduction.
 
* Or even  deficient w.r.t completeness.




Sunday, May 16, 2021

Gist vs Details

All of us have heard that 'the devil is in the detail', and is intended to mean that we should bother about the details.

But should we really?

Lets check a counter example, which would rather re-interpret 'the devil is in the detail' as don't get into the details or you will awaken the devil 😈

It is a major efficiency issue in many 'structured' organizations, several people focusing on details and the details going ping-pong, and finally what one could implement in a day taking months.

End result is a robust bullet proof museum worthy product crafted by hundreds. Why museum worthy? Oh its market is already taken by a product quickly patched together by a dozen

Just to clear out the air, this is metaphoric writing and 'details' is quite overloaded in its meaning.

Here is the counter example problem statement:

"We are hosting a tournament with 'n' number of participants.
If 'n' is even, we conduct n/2 matches and n/2 winners advance to next round.
If 'n' is odd, we give free pass to a random participant, and conduct (n-1)/2 matches with the remaining (n-1) participants. So the random lucky participant and (n-1)/2 winners advance to the next round.
The tournament thus proceeds with remaining participants till a champion is decided.
Calculate the total number of matches that need to be held in the tournament.
" 
The Gist way of solving this problem: 
We need a champion. How do we get a champion? After n-1 participants lose. How do they lose? By  playing a match. So if we play n-1 matches, we eliminate n-1 participants, and we have a champion.
``But wait...what about odd, even and all that?``
What about it? Does it contradict the statement that every match conducted eliminates exactly one participant? ``Hmm, no... ``
Then, hey, its (n-1)

The Details way of solving this same problem:
``What about odd, even and all that?``
We change course here, and say ``No no, lets do some detailed analysis, we want to be sure we covered the odd/even part of the requirements``

Hang on tight as we get into the "detailed analysis :)"

Detailed Case Analysis:

Part 1: Formulate basic recursion:
Lets define matches required for a given number n as m(n).
"n teams need n/2 matches if n is even" translates to
m(2n) = n + m(n) ------> Eq.1
"n teams need (n-1)/2 matches if n is odd, and one gets a free pass" translates to
m(2n+1) = n + m(n) + 1 ------> Eq.2
From Eq.1 and 2:
m(2n+1) = m(2n) + 1 ------> Eq.3
Now, this is great that we can express m(2n+1) in terms of m(2n).
If we can calculate m(2n) in closed form, we can easily calculate m(2n+1) by adding a 1 to it.
We have base cases: m(1) = 0 and m(2) = 1.
Here, Eq.1 and Eq.2 are good, but they are discontinuous, in the sense that we have to ways to move ahead based on whether number of participants in odd or even
Eq.3 is better, but its still a one way relationship in the sense that we can go from even to next odd, but we don't have a way to go from an odd to next even.

Part 2:Derive a continuous recursion
So, lets try to formulate a recursion that is valid for all 'n' in a uniform way.
Lets consider m(2n+2)...
m(2n+2) = m(2(n+1)) = n+1+m(n+1) ---> Eq.4
 
Part 2.1: n is even:
Here we consider that n is even, which means n+1 is odd.
So, m(n+1) = m(n) + 1 based on Eq.2
Eq.4 reduces to:
m(2n+2) = (n+1 + m(n)) + 1 And from Eq.2 n+1+m(n) = m(2n+1)
So, For all even 'n', m(2n+2) = m(2n+1) + 1 -----> Eq5
 
Part 2.2: n is odd:
Here, we consider that n is odd, which means n = 2x+1, from some integer 'x'.
lets substitute this in m(n+1)
Then, m(n+1) = m(2x+1+1) = m(2x+2) = m(2x+1) + 1 (from Eq5 due courtesy Part2.1)
Now, lets plug back the value of n = 2x+1
We get for all odd n, m(n+1) = m(n) + 1 ---> Eq.6
So, we have proved that:
when n is even m(n+1) = m(n) + 1 (Eq.3)
when n is odd, m(n+1) = m(n) + 1(Eq.6)
And hence, 
For all n, m(n+1) = m(n) + 1 --> Eq.6
And thus we arrive at a continuous recursion of m(n) = m(n-1) + 1
 
Part 3: Arrive at the closed form from continuous recursion  
From base cases, m(1) = 0 and expanding Eq. 6:
m(n) = 1 + m(n-2) + 1 = 1 + 1 + m(n-3) + 1 = 1 + 1 + ... (n-2) times + m(1) + 1
m(n) = (n-2) + m(1) + 1 = n-2 + 0 + 1 = n -1 
m(n) = n-1 --> Eq.7 
Q.E.D
 
And here we see just the tips of devil's horns.
The claws, the thorny mace, and other gory tools of torture lie buried in the reviewing, standardizing, platforms, and ways of working  :-)

Friday, May 14, 2021

A Hare and Tortoise tale of computation

Its one of those usual days when i solved a little practice problem, and on a second look, felt like i touched my nose from around the back of my head..

The problem is this: "Given an array of positive integers, calculate the sum of all possible odd-length sub-arrays."
Below are 2 implementations to solve the problem.. 
Now place your bets on which one will be faster on a general purpose risc machine.. 
Its really like an Ingenious vs Ingenuous face-off, and who in the sane world would ever identify themself with a super brainy victor? 😉

The ingenious way: total= ∑ count[i]*val[i] where count[i] defines the number of odd length arrays that can be formed with val[i]. For indices beginning at 0, count[i] = ceil( (i+1)*(n-i ) / 2 )

The smarter code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int sumOddLengthSubarrays(vector<int>& arr) {
        int total = 0;
        int n = arr.size();
        for(int i = 0; i < n; i++){
            int count = (i+1)*(n-i);
            int count_odd = count/2 + count%2;
            total += count_odd*arr[i];
        }
        return total;
}

The ingenuous way: total = ∑val[i] + ∑combi_sum[i]; where combi_sum[i] is defined as the sum of all the odd length sub-arrays that end at position (i) other than the single element subarray [val[i]] and computed as below:
combi_sum[0]=combi_sum[1]=0;
combi_sum[2]=val[0]+val[1]+val[2];
For all other (i):
combi_sum[i] = ∑ {
                                num_odd_arrays_ending_at[i-2] * (val[i-1] + val[i]) + 
                                combi_sum[i-2] + 
                                val[i-2] + val[i-1] + val[i];
                               }
Wondering what nasty cocktail is that summation above?
Just go with the flow, there is explanation to follow..
The round about code
 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
int sumOddLengthSubarrays(vector<int>& arr) {
        vector<int> running_sum;
        running_sum.reserve(arr.size());
        vector<int> combination_sum;
        combination_sum.reserve(arr.size());
        int total = 0;
        int i = 0;
        int k = arr.size() > 1 ? 2 : arr.size();
        for(i = 0; i < k; i++){
            total += arr[i];
            running_sum.push_back(total);
            combination_sum.push_back(0);
        }
        if(i < arr.size()){
            total += arr[i];
            running_sum.push_back(total);
            combination_sum.push_back(total);
            total += total;
            i++;
        }
        
        for(; i < arr.size(); i++){
            total += arr[i];
            running_sum.push_back(running_sum[i-1] + arr[i]);
            combination_sum.push_back(
                (((i/2) - 1) * (arr[i-1] + arr[i])) + 
                combination_sum[i-2] +
                running_sum[i] - running_sum[i-3]
            );
            total += combination_sum[i];
        }
        return total;
}
Now, which version do you think is faster on a usual computer?
i for sure thought the smarter and concise version was also the faster... but was in for a surprise trying to cheerfully test its performance.
And that too, after a recovery from the rather laborious and clumsy looking round about version.
Let me lay it out for you.. the fact is that the 'round about way', beats the 'smarter way'..
And how is it so? if you wonder, it seems to come from the cost of multiplications... not only the number of them, but also from the value of the multiplicand.
So, lets draw a table tracing through the calculations of each approach.
Lets start with the smarter and concise way first.
index 0 1 2 3 4 5 6 7 8 9 10
example data 6 5 8 7 1 2 4 3 9 11 10
cnt(all) 1*11= 11 2*10= 20 3*9 = 27 4*8 = 32 5*7 = 35 6*6 = 36 7*5 = 35 8*4 = 32 9*3 = 27 10*2 = 20 11*1= 11
cnt(odd) = ceil(cnt(all)/2) 6 10 14 16 18 18 18 16 14 10 6
cnt(odd) * val 6*6= 36
10*5= 50
14*8= 112
16*7= 112
18*1= 18
18*2= 36
18*4= 72 16*3= 48
14*9= 126
10*11= 110
6*10= 60

Total = ∑(cnt(odd)[i]*val[i]) = 780

So, for each element, we have two multiplications and one divisions. That is obvious before we drew the table.
But what we see from the table is that for an array of size 'n', the multiplicands range:
a) from 1 to n for an array of size n when calculating cnt(odd).
b) and from ceil(n/2) to ceil((n*n)/8 when calculating cnt(odd)*val
So, for an array of size 100, the minimum multiplicand is 50, and maximum is 1250.
The n*n part in the multiplicand indicates, that the multiplicand grow faster than the array size.

Now, let us look at the round about way
Lets recap the definition of calculations used
1) combi_sum[0]=combi_sum[1]=0 
2) combi_sum[2]=val[0]+val[1]+val[2]
3) For all other (i) combi_sum[i] = ∑ { 
                                                                num_odd_arrays_ending_at[i-2] * (val[i-1] + val[i]) +
                                                                combi_sum[i-2] + 
                                                                val[i-2] + val[i-1] + val[i]
                                                            };
A bit of explanation: 
At position (i), assume that already the sum of all the odd length arrays ending at (i-2) is computed.
Now (i), together with (i-1) will add to each of these sub-arrays.
Apart from that, (i) will also form a new odd length array with (i-2), (i-1), and itself.
In the example down below, lets consider position 6. position 6 can form an odd length array in following combinations: a) 0 to 6, b) 2 to 6, c) 4 to 6. 
The sum of those odd length arrays a), b), and c) would be ∑val[0..6], ∑val[2..6], and ∑val[4..6] respectively.
But we already calculated the sum of ∑val[0..4] =27 plus ∑val[2..4] =16 and stored it as combi_sum[4]=27+16=43.
a) ∑val[0..6] =    ∑val[0..4] + ∑val[5..6];
b) ∑val[2..6] =    ∑val[2..4] + ∑val[5..6]
c) ∑val[4..6]
a) + b) + c) = (∑val[0..4] + ∑val[5..6]) + (∑val[2..4] + ∑val[5..6]) + ∑val[4..6]
re-arranging we get:
a) + b) + c) = ∑val[0..4] + ∑val[2..4] + 2 * ∑val[5..6] + ∑val[4..6]
But we know ∑val[0..4] + ∑val[2..4] = combi_sum[4], so
a) + b) + c) = combi_sum[4] + 2 * ∑val[5..6] + ∑val[4..6]
q.e.d 😁
4) num_odd_arrays_ending_at[i-2] = (i-2)/2
5) we really don't need to, but we also maintain a running sum to skip an addition.
instead of doing: val[i-2] + val[i-1] + val[i], we do running_sum[i] - running_sum[i-3]
6) total = ∑val[i] + ∑combi_sum[i]


index 0 1 2 3 4 5 6 7 8 9 10
example data 6 5 8 7 1 2 4 3 9 11 10
running sum 6 11  
19  
26 27 29 33 36 45 56 66
combi sum 0 0 19  
20= 0*15+0 +26-6 43= 1*8+19 +27-11 33= 1*3+20 +29-19
62= 2*6+43 +33-26
56= 2*7+33 +36-27
114= 3*12+62 +45-29
139= 3*20+56 +56-33
228= 4*21+114 +66-36

Total =  running_sum[10] +  ∑(combination_sum[i]) =  780

Here, in the round about way, we surely have additional memory to buffer up the combi_sum and also running_sum, but we have only one multiplication per element, though we have a few more additions. 

The highest multiplicand is (n-2)/2 for an array of size n... compare it to n*n/8 in case of the smarter solution.

And hence, sometimes, the operations we assume wont have any noticeable performance impact, can indeed do, or can it not? Especially since the multiplication operation within wordsize is guaranteed to have same asymptotic cost as addition..

While the hare was hopping element to element without looking back much swiftly, it was kept busy in multiplication..
The tortoise was looking back at at least 3 elements but as a result kept its multiplication simpler..
And in the end it went past the finish line without losing much of its breath.

But on a third look, its not the multiplication that's the problem with the smarter way, its just the calculation of odd_count as (count/2+count%2) or ceil(count/2.0) 

Do it as odd_count = (count+1)/2. Now, both hare and rabbit are happy, they crossed the line at the same time, though they took different routes...

Time to think, whats the cost of modulo 😱