Drawing Blanks

Premature Optimization is a Prerequisite for Success

Optimal angle for throwing things from a height

leave a comment »

We know that we need neither calculus nor trigonometry in order to prove that the 45 degree angle maximizes the distance.



That is, when we are on a flat surface, or more precisely, when the landing point is at the same height as the launch point.

Finding the angle that maximizes the horizontal distance when the launch point is elevated is generally considered a much harder problem. See e.g. here for a solution http://luckytoilet.wordpress.com/2010/08/07/throwing-a-rock-off-a-cliff-calculating-the-optimal-angle/

I’ve just learned that there exists a trick that allows to solve this problem without differentiation.

First let’s consider the family of trajectories of the body launched at different angles at the same initial speed:


The red-highlighted curve is the 45-degrees parabola.

And what is the green curve? It is the envelope of our family of trajectories.

It is called Torricelli’s Parabola of safety http://en.wikipedia.org/wiki/Parabola_of_safety

Judging by its name, it is a parabola, and Torricelli likely proved that fact without any calculus. I’ll demonstrate that in one of the next posts, and now I’m going to quickly show how to use it to solve the original problem.

Using the fact the the 45-degree parabola is optimal, we trivially find that the point (V^2/g, 0) belongs to the green parabola. And (even more trivially) since the point (0, V^2/2g) belongs to it too, we can write its equation y = -\frac{g}{2V^2}x^2 + \frac{V^2}{2g} , substitute our initial height h and find the max distance S = V/g \sqrt{V^2 + 2gh} . Finding the angle corresponding to this distance is just a boring exercise that I leave to you.

After the break – some ugly R code that I used to plot the picture.

Read the rest of this entry »


Written by bbzippo

10/09/2013 at 5:07 am

Posted in Uncategorized

SQL Server collations and string comparison issues

leave a comment »

Evil Kitty 😼 equals to the empty string

With the “default” collation setting (SQL_Latin1_General_CP1_CI_AS) SQL Server cannot properly compare Unicode strings that contain so called Supplementary Characters (4-byte characters). For example, the “kitty face” below is equal to the empty string:

(If you don’t see the kitty faces then your browser and/or OS sucks as does Chrome on Windows. The symbol is U+1F63C “cat face with wry smile” http://en.wikipedia.org/wiki/Emoticons_(Unicode_block) )

SELECT ‘oops’ WHERE N’😼’ = N”

Moreover, the default collation considers phonetically equivalent Katakana and Hiragana characters to be equal:

SELECT ‘oops’ WHERE N’お’ = N’オ’

These issues are easy to work around by using a binary collation. But in most scenarios we want case-insensitive comparison, so binary is not an option.

Here is a collation that is case insensitive and doesn’t have issues with Supplementary Characters or with Kana:

Latin1_General_100_CI_AS_KS_WS (CI – case-sensitive, AS – accent sensitive, KS – Kana-sensitive, WS – width-sensitive).

In SQL 2012 this one also works: Latin1_General_100_CI_AS_KS_SC (SC –  supplementary characters).

SELECT ‘oops’ WHERE N’A’ COLLATE Latin1_General_100_CI_AS_KS_WS != N’a’ — Case insensitive

SELECT ‘oops’ WHERE N’お’ COLLATE Latin1_General_100_CI_AS_KS_WS = N’オ’ — Kana sensitive

SELECT ‘oops’ WHERE N’😼’ COLLATE Latin1_General_100_CI_AS_KS_WS = N” — Supplements differ from ‘’

SELECT ‘oops’ WHERE N’😼’ COLLATE Latin1_General_100_CI_AS_KS_WS = N’😽’ — Different Supplements differ

SELECT ‘oops’ WHERE N’😼’ COLLATE Latin1_General_100_CI_AS_KS_WS != N’😼’– Equal supplements are equal

One remaining issue that I’m aware of is the German Eszett: ß equals to “ss” under any non-binary collation.

SELECT ‘oops’ WHERE N’ß’ COLLATE Latin1_General_100_CI_AS_KS_WS = N’ss’

Microsoft acknowledged the Eszett issue as a bug in 2008: http://connect.microsoft.com/SQLServer/feedback/details/341130/-and-ss-are-not-equal

But the real question is:

Is it the correct design to use the same collations for the purpose of sorting and for the purpose of equality comparison?

Written by bbzippo

09/10/2013 at 1:22 am

Posted in Uncategorized

Antichains: Crocodiles and schoolgirls in parliament

leave a comment »

Antichain is a subset of a partially ordered set such that any two elements in the subset are incomparable.

I’ve mentioned them a couple of times explicitly or implicitly: in Crocodile Dinner, in Important Theorems From Combinatorics, in the Crossing Lines problem and in its “anime variation”.

Here is another elementary problem that is kind of in between “schoolgirl rivalry” and “crocodiles that swallow each other”.

Consider 435 persons. Assume that every one of them has lied to exactly one other person from this set. (assume they don’t lie to themselves).
Prove that it’s possible to find a subset of at least 145 people so that nobody in that subset has lied to nobody in that subset.

Solution follows after a short break:

Read the rest of this entry »

Written by bbzippo

01/27/2013 at 11:47 pm

Posted in fun, math

A loaded die study

leave a comment »

I got a set of handcrafted dice for Xmas. They look like this:

DSC00555 (640x550)DSC00554 (640x481)

They are wooden, the shapes are obviously imperfect, and the dots are made of metal.

I immediately suspected that they can’t be well balanced and decided to conduct a little study, that turned out to be fun and instructive.

The first experiment I did was dropping the die into water. It appeared that the die is lighter than water and it always floats the 1 side up and the 6 side down:

DSC00560 (640x427)

[Side quest: A perfect cube with the density of exactly 1/2 of the density of water floats in water. Will it float side-up, edge-up or vertex-up? This problem is too tough for me, and I don’t have a solution.]

Anyway, after the float test it was evident to me that the die is biased in the 1- 6 direction. But by how much is this imbalance affecting the outcome of rolling the die on the table?

My next test was the Roll Test. I rolled the die 121 times. Why 121? I wanted a number that is close to 100, close to a multiple of 6 and close to a perfect square. That is because I was pretty sure I’d be able to compute the sigma on a napkin and that would be it. Both 120 and 121 are good numbers. But after I did 120 rolls I thought, why don’t I do one more.

Here are the results of my 121 rolls:

1 2 3 4 5 6
27 19 19 20 21 15


Right, 1 and 6 are obvious outliers… ??? … But they are within the 2-sigma range… But I’m more than confident in my alternative hypothesis! Like a true researcher, I’m going to find a way to confirm it! Winking smile

So, do I do another 100 rolls? No way, that would be no fun! I’m going to pretend that I’m not dealing with a stupid die, but rather with a particle accelerator, and that I’m over the budget, so this sample is all I have. I’ll do various stats tests on my sample, and I’m going to find one that confirms that this stupid die is loaded!

Sadly, both Chi-square and Kolmogorov-Smirnoff yield p-values about 0.5 that is 10 times greater than what I need in order to reject the hypothesis that the die is fair.

But I’m not giving up. Why was I doing all those tests that attempted to refute the null-hypothesis without having any information about my alternative hypothesis: that the die is biased specifically towards 1 and specifically against 6. And why was I doing all those old-fashioned tests at all? It’s not 19th century and I have a very capable computer at my disposal. I can simulate whatever I want.


Alternative Hypothesis
(out of 121 rolls)
At least 1 number appears more that 26 times 0.37
At least 1 number appears more that 26 times AND at least 1 number appears less that 16 times 0.3
Exactly 1 number appears more that 26 times AND exactly 1 number appears less that 16 times 0.2
The number ONE appears more that 26 times AND the number SIX appears less that 16 times 0.01


So I guess I could now conclude that the die is biased specifically towards 1 and specifically against 6. But should I?

The next step should be checking how significant this bias is for some actual game. I’d need to simulate a game with a realistic bet and see how much money can be won using this die within a realistic timeframe.

I used R for the simulations. Below is the piece of code that does that. Happy New Year!

Read the rest of this entry »

Written by bbzippo

12/31/2012 at 2:51 am

Posted in fun, math, science

Red vs. Green

leave a comment »


This is from Microsoft Excel 2010.

And I have a confession to make: I can hardly tell Good from Bad… like 1% – 6% of all human males I have a slight defect of color vision. We are not a threat to society: we can tell apart the traffic light signals and other color-based discriminators used in machinery, weaponry etc.

So why don’t UI designers take us into consideration? I’m sure there is a ton of research on creating dichromacy-friendly color palettes.

The RGB values for Good are 198, 239, 206. For Bad: 255, 199, 206. Are you kidding? How can anyone tell these apart? Now let’s increase saturation (+40 in Photoshop):


Now I can clearly see the difference. Good RGB: 200, 255, 130. Bad RGB: 255, 199, 206. See, Bad wasn’t affected by the saturation boost. And Good became… greener and lighter.

… Lately I’m sooo disappointed by what Microsoft is doing in UI/UX. It’s cargo cult… Failed attempts to copy Apple… I’m so glad Steve Sinofsky is no longer there… I wanted to write a huge rant about Windows 8, but I don’t even know where to start.

Happy Holidays!

Written by bbzippo

12/26/2012 at 2:35 am

Posted in Uncategorized

The measure of massiveness

leave a comment »

This year’s Nobel Prize has not been awarded for the discovery of the Higgs. It rather celebrates achievements that will undoubtedly have practical applications. In order to compensate for this unfairness I wanted to dedicate a couple of hours of my time recollecting and reminiscing of some fundamental things that are related to the notion of mass. As I’m not formally trained in physics, this is based on what I learned in school and read in popular literature.

Mass in Intuitive mechanics

  • Mass measures the amount of stuff
  • Mass is conserved
  • Mass is additive
  • Mass is a measure of inertia

Mass in Newtonian mechanics

  • All of the above
  • Mass is invariant in all inertial frames of reference
  • Mass is the source of gravity
  • F=ma

Note that F=ma is nothing more than:

  • Definition of force F = dp/dt
  • Definition of acceleration a = dv/dt
  • Definition of momentum p = mv
  • Conservation of momentum (with no F there is no change of p)

Note that it is easy to guess that gravity is no different from inertia. Acceleration does not depend on the mass? Come on, it’s not a real force! It’s the same kind of force that gives the same acceleration to skinny and fat people on a bus that suddenly breaks.
Also note that it’s easy to predict that massless thing are affected by gravity: once you consider F=ma and F~mM, you see that a doesn’t depend on m. Which Laplace did. Laplace was brilliant, as I mentioned here and here, but this simplistic prediction did not match the observation quantitatively (by the factor of 2).


  • We need to redefine momentum and energy so that they are still conserved
  • Mass is still invariant.
  • The rest energy is not zero. The mass is the rest energy.
  • The definitions of force and acceleration are still the same, but F is not ma.
  • Mass is not a measure of inertia. In fact, there is no invariant measure of inertia. Bodies resist forces depending on how they are moving.
  • Mass does not define the gravitational force. In the same manner as inertia, gravity depends on how the bodies are moving – it’s defined by the energy and momentum.
  • Mass is not additive. If we have a system of moving or interacting bodies, its mass is not the sum of the masses of the constituents.
  • Massless things cannot be at rest, in any frame of reference.
  • Mass is not the amount of stuff. It is the amount of motion and interaction – just like energy. But motion is relative… Once again, the mass is the rest energy.

The Higgs

  • When it seems that there are no interacting parts, but the massiveness is still there – it is due to interaction with the Higgs field.

Written by bbzippo

10/10/2012 at 3:08 am

Posted in Uncategorized


leave a comment »

Here is how double.IsNaN() implemented, it’s funny:

/// Returns a value indicating whether the specified 
/// number evaluates to a value that is not a number 
public static bool IsNaN(double d)
   return d != d;

The double type can get you in trouble because it can store things like NaN and Infinity. It will silently divide by zero, take square roots and logarithms of negatives, and keep calculating, and you will have a hard time finding out where exactly your math is failing. It gets particularly funny when your formulas are right, but because of loss of precision your value under Math.Acos() becomes slightly greater than 1…

Sometimes you’d get a number when you would expect a NaN, e.g.

Math.Pow(0, double.PositiveInfinity) //is zero and not NaN
Math.Pow(1, double.PositiveInfinity) //is one and not NaN


And there is no such thing as double.IsNumber(), so if you want to check whether your calculation succeeded you need to check .IsNaN() && .IsInfinity()

So it’s a lot of fun…

Written by bbzippo

07/19/2012 at 4:10 am

Posted in programming