PHP math algorithms