How would I find the closest perfect square to a number, without using sqrt?

Yup. I should have said more, for clarity. This was the intended point: if y = f(x) is a graph of a straight line, then for any points A and B this equality holds exactly:

f((A+B)/2) = (f(A)+f(B))/2

But if f(x) = x^2, and B = A+1, that equality almost holds (the left side is 0.25 larger, as already shown). It’s in that sense that a straight line is a good approximation to squaring between consecutive integers. Work out all the details, and it’s “why” the closest perfect square to N is the square of the int closest to sqrt(N) (which does not generalize to powers higher than 2).

1 Like