Decomposition into three squares

466 Views Asked by At

Doing a coding assignment. And it's basically having a user enter $n$. Then I need to provide (If it exists) $$n = x^2 + y^2 + z^2.$$

Not really sure how to approach this. Any ideas?

1

There are 1 best solutions below

5
On BEST ANSWER

So following from Lab's link we have $$n=x^2+y^2+z^2\iff n\ne4^a(8b+7)$$ Therefore to test we divide by 4 as much as possible at least once. Then subtract 7 and test if it is divisible by 8. If the divisibilities are true then you have a number that can't be represented as such.


Algorithm

  1. Check divisibility by 4. If it is not divisible then $n$ is such a number.
  2. Keep dividing by 4 until it is no longer divisible.
  3. Subtract 7.
  4. Check divisibility by 8. If it is divisible then $n$ is not such a number else $n$ is such a number.