Excel: why is the average of divisions different from division of sums? - excel

So lets say I have two variables x and y. I calculate the total sum of each variable.
I calculate y/x for each row and also calculate y/x for the sum of both x and y columns (280/10=28).
I would expect this value (28) to be equal to the average of y/x (230/4=32.5), but it is different.
This might have a simple explanation, but I can't seem to find it.
Thanks in advance

This is really a mathematics question. The two are not equal:
10 200 30 40
─── + ──── + ─── + ───
10 + 200 + 30 + 40 1 2 3 4
────────────────── ≠ ────────────────────────
1 + 2 + 3 + 4 4
There really is no reason why they could be expected to be the same: when the second expression is rewritten with one common denominator, it becomes even more evident there is little it has in common with the first expression.
To get a common denominator, one finds the least common denominator, which in this case is 12, and so the second expression could be written as follows:
120 1200 120 120
──── + ──── + ──── + ────
12 12 12 12
──────────────────────────
4
Which is simplified to:
120 + 1200 + 120 + 120 5 + 50 + 5 + 5 65
────────────────────── = ────────────── = ── = 32.5
48 2 2
There clearly is no relation with the first expression.
What is equal?
10 + 200 + 30 + 40 AVG(10, 200, 30, 40)
────────────────── = ────────────────────
1 + 2 + 3 + 4 AVG(1, 2, 3, 4)
This works because you really divide both numerator and denominator with the same factor (4), which is a null-operation.

Related

recursive function does not work as expected

Could someone explain the code? I just can not understand why this code gives output like this:
1
3
6
10
15
21
I expected the code to give something like this:
1
3
5
7
9
11
What am I missing here?
def tri_recursion(k):
if(k > 0):
result = k + tri_recursion(k-1)
print(result)
else:
result = 0
return result
tri_recursion(6)
For your recursive function, the termination condition is k=0.
It's clear that if k=0, tri_recursion(0) = 0.
If k=1, tri_recursion(1) = 1 + tri_recursion(0), which from above, is 1 + 0 or 1.
If k=2, tri_recursion(2) = 2 + tri_recursion(1), which from above, is 2 + 1 or 3.
If k=3, tri_recursion(3) = 3 + tri_recursion(2), which from above, is 3 + 3 or 6.
If k=4, tri_recursion(4) = 5 + tri_recursion(3), which from above, is 4 + 6 or 10.
If k=5, tri_recursion(5) = 4 + tri_recursion(4), which from above, is 5 + 10 or 15.
If k=6, tri_recursion(6) = 6 + tri_recursion(5), which from above, is 6 + 15 or 21.
See the pattern?
Your code is calculating the sum of numbers up to n where n is 6 in the above case. The print statement prints the intermediate results. Hence the output 1 3 6 10 15 21.
1 - The sum of numbers from 0 to 1
3 - The sum of numbers from 0 to 2
6 - The sum of numbers from 0 to 3
10 - The sum of numbers from 0 to 4
15 - The sum of numbers from 0 to 5
21 - The sum of numbers from 0 to 6

How can you find the nth digit in a fractal number

The sequence look like this 112123123412345...
If the input is 55,it should return 1,not 10. And if the input is 56,it should return 0,not 1. You got the idea.
So we have a sequence composed of the composition of
1 1 digit 1 digit total
12 2 digits 3 digits total
123 3 digits 3*(3+1)/2 = 6 digits total
1234 4 digits 4*(4+1)/2 = 10 digits total
...
123..89 9 digits 9*(9+1)/2 = 45 digits total
123..8910 11 digits 10*(10+1)/2 + 1 = 56
123..891011 13 digits 11*(10+1)/2 + 3 = 69
123..89101112 15 digits 12*(12+1)/2 + 3*(3+1)/2 = 84 digits
This is OEIS Sequence A165145 and also see the related sequence OEIS A058183.
A formula for the total number of digits is
f(n) = n*(n+1)/2 + {(n-9)*(n-8)/2 : if n>=10} + {(n-99)*(n-98)/2 : if n>=100) + ...
Some key points f(9) = 45, f(99) = 99*100/2 + 90*91/2 = 9045, f(999) = 1,395,495.
An outline algorithm for finding the k-th digit would be
Find which part of the sequence you are in n<=9, 10 <= n <= 99, 100 <= n <= 999 by comparing k with the boundary values 45, 9045, 1395495.
Recover the value of n
Find the actual digit which will be k-f(n) along sequence for the n-th number.
If we take 10 <= n <= 99 the formula for the number of digits is
n*(n+1)/2 + (n-9)*(n-8)/2
= 1/2( n^2 + n + n^2 - 17 n + 72)
= n^2 - 8 n + 36
So given 45 < k <= 9045 we solve k = n^2 - 8 n + 36, using the quadratic formula
n = ceil( ( 8 + sqrt(64 - 4 (36 - k)))/2)
We need a different quadratic formula for k outside the range.
For example take k = 100, using the formula gives n=13. There are f(12)=84 digits for all the numbers upto 12, so the first digit of the 13th string is at position 85. So we are looking for the 16th digit. We can use the formula
digit(l) := l <= 9 ? l : (l%2==0 ? floor((l+10)/20) : ((l-11)/2)%10 )
to find the actual digit, which is 1.

Excel sum only single or first duplicated value in a list

I've a formula in Excel which I've found on the net which gives the required result but I don't fully understand how it works. This is =SUMPRODUCT(B1:B9/COUNTIF(A1:A9,A1:A9)) and the result is 129 for the following data (it adds single occurrences in Column B of the data, this being 13 + 24 + 92 = 129 which is the required result).
Row A B
1 1 13
2 1 13
3 1 13
4 1 13
5 3 24
6 3 24
7 3 24
8 12 92
9 12 92
I understand the COUNTIF(A1:A9,A1:A9) is creating an array {4;4;4;4;3;3;3;2;2) but I don't know how the range B1:B9 numerator is working to create the result. If the numerator was say the number "1" (i.e. the formula being instead =SUMPRODUCT(1/COUNTIF(A1:A9,A1:A9)), the result is 3 and I think is worked out as the sum of 1/4 + 1/4 + 1/4 + 1/4 + 1/3 + 1/3 + 1/3 + 1/2 + 1/2. So how when the B1:B9 is in the formula how step by step is it working it out?
You are pretty close to the answer yourself. Given your example you have two arrays:
The first one is the numbers from column B and the other one from the countif in column A:
{13;13;13;13;24;24;24;92;92} and {4;4;4;4;3;3;3;2;2}
In the sumproduct formula you have the division and therefore you get the arrays divided:
{13/4; 13/4; 13/4; 13/4; 24/3; 24/3; 24/3; 92/2; 92/2} and the sum of these numbers are:
3,25 + 3,25 + 3,25 + 3,25 + 8 + 8 + 8 + 46 + 46 = 129
And there is the magic number :-)

Why does Excel average gives different result?

Here's the table:
Should not they have the same result mathematically? (the average score of the per column and per row average)
The missing cells mean that your cells aren't all weighted evenly.
For example, row 11 has only two cells 82.67 and 90. So for your row average for row 11 they are weighted much more heavily than in your column averages where they are 1/13 and 1/14 of a column instead of 1/2 of a row.
Try filling up all the empty cells with 0 and the averages should match.
Taking a more extreme version of Ruslan Karaev's example:
5 5 5 | 5
1 | 1 Average of Average of Rows = (5 + 1 + 0) / 3 = 2
0 | 0
-----
2 5 5
Average of Average of Columns = (2 + 5 + 5) / 3 = 4
Yes, for example, the following two expressions:
/ a + b X + Y \ / a + X b + Y \
( ----- + ----- ) ( ----- + ----- )
\ 2 2 / \ 2 2 /
------------------- -------------------
2 2
are indeed mathematically equivalent, both coming out to be (a + b + X + Y) / 4.
However, short of having enough sufficient precision to store values, you may find that rounding errors accumulate differently depending on the order of operations.
You can see this sort of effect in a much simpler example if you assume a 3-digit precision and divide one by three, then multiply the result of that by three again:
1 / 3 -> 0.333, 0.333 x 3 -> 0.999
Contrast that with doing the operations in the oppisite order:
1 x 3 = 3, 3 / 1 = 1

spoj - CPCRC1C, sum of digits of numbers 1 to n, need clarification, not solution

Once, one boy's teacher asked him to calculate the sum of numbers 1 through n.
the boy quickly answered, and his teacher made him another challenge. He asked him to calculate the sum of the digits of numbers 1 through n.
Input
Two space-separated integers 0 <= a <= b <= 109.
Output
The sum of the digits of numbers a through b.
Example
Input:
1 10
Output: 46
can someone explain what is meant by sum of the digits of numbers a to b?
from above, sum of {1 2 3 4 5 6 7 8 9 10 } is 55 , it is a well known Gaussian formula
but the output is 46!
if i count from 2 to 9, excluding the border numbers 1 and 10, the answer is 44 , still not 46
So what is meant by sum of digits of numbers?
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + (1 + 0)
Don't treat the 10 as the number 10, rather the digits 1 and 0

Resources