Completing the square algorithm