Sum of squares complete algo