The Britney Spears Problem(americanscientist.org)
americanscientist.org
The Britney Spears Problem
http://www.americanscientist.org/issues/id.3822,y.0,no.,content.true,page.1,css.print/issue.aspx
2 comments
How about a special running average: keep the count in one register and the average in the other one. Let's say N is the current count and av the average. The algorithm is in C-style notation:
void addElement( int val ) {
++N;
av = ((N - 1) * av / N) + val / N;
}
I have used a similar trick (with a fixed N) to implement a cheap running average for an image acquisition software and it works quite well, but of course if N is very large val / N becomes negligible.
I have used a similar trick (with a fixed N) to implement a cheap running average for an image acquisition software and it works quite well, but of course if N is very large val / N becomes negligible.
You guys are so goddamn geeky! <3
constant space ftw
Let average-so-far=a, count=c. When you add a new item x, the new average is: a = a + (x-a)/(c+1)
You could.. multiply the average register by the count register, then add the new integer to the average register, increment the count register and then re-divide?
I posted this a while back, but it didn't get many votes.
http://news.ycombinator.com/item?id=243270
http://news.ycombinator.com/item?id=243270
Here's an easy brain teaser: do the above using only two registers.