Simple algorithm to get the square of an integer using only addition?

7.5k Views Asked by At

This problem was mentioned in passing in a reading and it piqued my curiosity.

I'm not sure where to start. Any pointers? (perhaps square root was meant?)

2

There are 2 best solutions below

2
On BEST ANSWER

An algorithm to do this for integers would be (in pseudo-code):

 input a
 b = abs a
 s = 0
 while b > 0 do
      s = s + a
      b = b - 1
 return s
2
On

$(n+1)^2 = n^2 + n + n + 1 $

recursion rocks:

$f(0)=0,\,f(n+1)=f(n)+n+n+1,\,n\geq0$