Improved algorithm for calculating purchase prices