I shared this algorithm on the Android Developers Group when I was writing it. It wasn't finished at the time. I had worked through the logic by writing the code one step at a time, checking the results at each step, and then figuring out if those results were within the realm of possibility. Since then I have received numerous emails from other developers asking for my final solution. It turns out that my first solution was almost my final solution. The only thing I have done to the alrorithm since was to shorten it, and get rid of some redundancy. It really didn't make much difference to my program as it is only used when a user makes a new search, and therefore is only called an average of 2 or 3 times a day.

Anyway.... I'll show my original code first, because it walks through the logic. The second implementation is a little faster, but not as easy to follow the logic.


Below is what I am actually using in production. It's really not much faster, but it does allocate less variables.


If you come up with a more efficient formula, please let me know. I'm sure there are other ways to do this, but this has worked very well for me.

Good luck with it.