Sort:  

Still not sure where I found it originally, but this post goes over a lot of the math.