Multiplying two numbers is the average squared minus sum of odd numbers of k equal average minus smallest number. Why

arithmetic

Multiplying two numbers is the average squared minus sum of odd numbers k equal average minus smallest number.

Why is that happening? What is the logic behind it? It is puzzling me.

For example (examples only for numbers that give integer averages, since for fractional averages you have to add the first number to the result):

a) 5 * 3 is (4*4)-1 (since difference between 4 and 5 is 1, its only the 1st odd number substracted)
b) 3 * 9 is (6*6)-1-3-5 (since difference between 6 and 3 is 3, its first 3 odd numbers substracted)
c) 21 * 35 is (28*28)-1-3-5-7-9-11-13 (difference between 28 and 21 is 7, its first 7 odd numbers substracted)
d) 21 * 36 is (28*28)-1-3-5-7-9-11-13+21 (fractional average, compute with *35 and add another 21 at the end)
and so on ... 233 * 53213 ... (too many odd numbers between 26723 and 233, but the result is sound)

This is a basic code implemented to compute the formula

function multiply(no1,no2)

 no_computation = ( floor( (no1 + no2)/2) )^2;  
 for k from 1 to abs( floor((nr1 + nr2)/2) - no1)   
  no_computation = no_computation -  (k*2-1);  
 end of for  
 multiply_result = no_computation + modulo(no1+no2, 2)*no1;

end of function multiply

Steps

  1. The "no_computation" variable is initialized as the average value, squared. If the average is fractional then it takes the largest integer smaller than the average (ie, a no1 number is substracted in order to give an integer average).
  2. Then the "for" function determines how many odd numbers there are between the average and the first number, and for each time it substracts the next odd number and refreshes the value of "no_computation"
  3. the multiply_result is the value "no_computation" with the added twist that if the average was fractional then it'll correct the result by adding the value of number no1 (since in the 1st step above a no1 number was substracted at the "floor" operation). This is done by "modulo" function.

What arithmetic property is involved in this computation?

Best Answer

The sum of the first odd integers is the square of the quantity of numbers we added $$\sum _{k=1}^{n} (2 k-1)=n^2$$ You subtract from the square of the mean $$\left(\frac{a+b}{2}\right)^2$$ the quantity of odd integers given by the difference between the mean and the minimum between $a$ and $b$ $$\sum _{k=1}^{\frac{a+b}{2}-\min (a,b)} (2 k-1)=\frac{1}{4} \left(-4 a \min (a,b)+4 \min (a,b)^2-4 b \min (a,b)+a^2+2 a b+b^2\right)=$$ $$=\frac{1}{4} \left(4 a \min (a,b)-4 \min (a,b)^2+4 b \min (a,b)-a^2-2 a b-b^2\right)+\frac{1}{4} (a+b)^2=$$ $$=a \min (a,b)- \min (a,b)^2+b \min (a,b)+\left[-\frac{1}{4} a^2-\frac{1}{2} ab-\frac{1}{4} b^2+\frac{1}{4} a^2+\frac{1}{2} ab+\frac{1}{4} b^2\right]=$$ the terms in the square bracket cancel $$=a \min (a,b)+b \min (a,b)-\min (a,b)^2=\ldots$$ now suppose that $\min (a,b)=a$. We get $$a \min (a,b)+b \min (a,b)-\min (a,b)^2=a^2+ab-a^2=ab$$ if $\min (a,b)=b$, we get $$a \min (a,b)+b \min (a,b)-\min (a,b)^2=ab+b^2-b^2=ab$$

and that's why your intuition works.

Related Question