title: Express a given number as a sum of four squares
body: <p>I am looking for an algorithm that expresses a given number as a sum of (up to) four squares.</p>
  <h3>Examples</h3>
  <p>       120 = 8<sup>2</sup> + 6<sup>2</sup> + 4<sup>2</sup> + 2<sup>2</sup><br />
         6 = 0<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> + 2<sup>2</sup><br />
         20 = 4<sup>2</sup> + 2<sup>2</sup> + 0<sup>2</sup>+ 0<sup>2</sup></p>
  <h3>My approach</h3>
  <p>Take the square root and repeat this repeatedly for the remainder:</p>
  <pre><code>while (count != 4) {
      root = (int) Math.sqrt(N)
      N -= root * root
      count++
  } 
  </code></pre>
  <p>But this fails when <em>N</em> is 23, even though there is a solution:</p>
  <p>       3<sup>2</sup> + 3<sup>2</sup>+ 2<sup>2</sup> + 1<sup>2</sup></p>
  <h3>Question</h3>
  <ol>
  <li><p>Is there any other algorithm to do that?</p>
  </li>
  <li><p>Is it always possible?</p>
  </li>
  </ol>