1

In the last few days, I have been wondering about the following equation in $x,y \in N$: $$x^2+y^2 = M$$ with $M \in N$. More precisley, I don't understand how can we compute the number of solutions that this equation have and especially how can we find it.

I've tried to use the formula: $(a^2+b^2)(c^2+d^2)=(ac-bd)^2+(ad+bc)^2$, but it looks me like as another complication, especially because I've to do this calculations for large value of $M$. I've also read the post Integer solutions to $x^2+y^2=N$?, but it hasn't really helped me alot.

Any ideas or suggestions? Are there also sites which I can consult on this topic? Thanks.

ANOTHER QUESTION (not already answered)

If using the above formula, we can find the solutions to $x^2+y^2=M$, what if M is a very large prime (such as $M=10000000103$): do I have to do only bruteforcing?

EDIT: As suggested, I have read the page on Wolphram Alpha linked above in the comments, but I still not uderstand in what way we can find the solutions of $x^2+y^2=M$. For example take the case: $x^2+y^2=85$ has $r_2(85)=16$, buit how can we obtain the value of $(x,y)$?

Matteo
  • 6,581
  • If $M$ is large and we do not know the factorization, we won't be able to find such a representation in general. – Peter Jan 12 '20 at 13:33
  • @Peter: $0\leq M \leq 10^8$, so it isn't completely impossible to find the factorization. – Matteo Jan 12 '20 at 13:36
  • OK, for so small $M$, this is absolutely routine. – Peter Jan 12 '20 at 13:46
  • I've read the page on Wolphram Alpha, but I'd like to have some examples? Can someone provide them? Thanks. – Matteo Jan 12 '20 at 13:49
  • The mathworld page that lulu linked to has examples for $M=4,5$. It also has an explicit formula for the number of representations if you know how to factor $M$. If you search this site for sum of two squares you get a lot of hits. I can't find an exact duplicate among them – Ross Millikan Jan 12 '20 at 14:07
  • 2
    If $x^2+y^2=M\le10^8$, and $0\le x\le y$, then $x<x_{max}=\sqrt{5\times10^7}$, so you can just ask your computer to test, for each $x$ from zero to $x_{max}$, whether $M-x^2$ is a square. – Gerry Myerson Jan 12 '20 at 15:31
  • @Meyerson: yes, I can do so, but it would be extremely slow. – Matteo Jan 12 '20 at 15:43
  • 1
    How hard is it to test $7000$ numbers to see which ones are squares? – Gerry Myerson Jan 13 '20 at 02:07
  • In the case that $M$ is a prime of the form $4k+3$ then it is not a sum of two integer squares. A Fermat theorem. – Somos Jan 13 '20 at 02:33
  • 1
    @Matteo "Extremely slow" ? I would agree if you would deal with , lets say , $20$ digit numbers. But in the case of $10^8$, I fully agree with Gerry Myerson. – Peter Jan 13 '20 at 08:40

2 Answers2

2

Given $x^2+y^2=85$, and the well-known factorization formula [which you already obviously know] $$(a^2+b^2)(c^2+d^2) = (ac\pm bd)^2+(ad \mp bc)^2.$$

Now just work backwards:

  1. Factor $85=5 \cdot 17.$
  2. Note that each factor (i.e., $5$ and $17$) have representations as the sum of two squares: $5=4+1=(\pm 2)^2+(\pm 1)^2$ and $17=16+1=(\pm 4)^2+(\pm 1)^2$.
  3. Determine $x$ and $y$ from $(a,b,c,d)$, going through all possible permutations (including multiple signs).
Kieren MacMillan
  • 7,889
  • 2
  • 28
  • 70
0

This is in general a very hard problem because any method which finds solutions to $\, n = a^2+b^2 = c^2+d^2\,$ allows us to factor $\,n.\,$ However, factoring large numbers is a very hard problem according to Wikipedia Integer factorization.

Using Gaussian integers we have that $\,a^2 + b^2 = (a+bi)(a-bi)\,$ and if we can find that $\,n = a^2 + b^2 = c^2 + d^2,\,$ then we must have that $\, n = (a+bi)(a-bi)=(c+di)(c-di).\,$ Find the $\,g := \gcd(a+bi,c+di) = e+fi\,$ using the Euclidean algorithm. Now we have that $\,(e+fi)|(a+bi)\,$ and hence $\,(e^2+f^2)|(a^2+b^2).\,$ But now $\,e^2+f^2\,$ is a factor of $\,n.$

Somos
  • 35,251
  • 3
  • 30
  • 76