Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I count all integer solutions to the equation $$x^2+dy^2=n$$

given $n$ and $d$, where $n$ is composite (or prime)?

By counting I mean any algorithm faster than brute force enumeration.

share|cite|improve this question
up vote 4 down vote accepted

What you want is the Hardy-Muskat-Williams algorithm, link HMW, which solves the Diophantine equation $a x^2 + c y^2 = n$ with the additional constraint that they give only the primitive representations, meaning $\gcd (x,y) = 1.$ If $n$ is not squarefree, you must solve the problem with the same $a,c$ but now $n/t^2$ for all such possible $t.$

It is no joke to program this. I have done it, largely because the first implementation in the language Mathematica was incorrect. It worked for $n$ prime but was unable to solve $x^2 + 14 y^2 = 65,$ which does have the solutions $x=\pm 3, \; y = \pm 2.$ The problem is that the article did not include any examples where some "seed" values (certain square roots $\pmod n$) failed to yield anything useful. So the Mathematica programmers assumed one seed value would be enough, whereas one must check all of them.

As you asked to count, just about the only example where you would be able to find an explicit formula that just counts the solutions, without finding them first, is the case $x^2 + y^2 = n.$ Such explicit counting formulas should be available for class number one, and maybe for the idoneal numbers, I am not sure in the latter case.

share|cite|improve this answer
can you elaborate on "class number one"? – asd Jan 7 '12 at 9:33
@asd the number of reduced forms of your discriminant $-4d.$ For example, with discriminant $-56,$ the four classes give reduced forms $$ x^2 + 14 y^2, \; 2 x^2 + 7 y^2, \; 3 x^2 + 2 x y + 5 y^2, \; 3 x^2 - 2 x y + 5 y^2 $$ so the class number is 4. – Will Jagy Jan 7 '12 at 20:42

I assume $d$ is positive, else there may be infinitely many solutions.

I suspect there's nothing really fast. If you could find all the solutions to $x^2+y^2=n$ fast, then you could factor $n$ fast, and you'd be one wealthy mathematician. $x^2+dy^2=n$ has similar qualities.

EDIT: concerning factorization and counting solutions:

Suppose $r^2+ds^2=m$ and $t^2+du^2=n$. Think of $r^2+ds^2$ as the norm of $r+s\sqrt{-d}$ (or don't - it just makes it easier for me to see where the next formula comes from), then $(r+s\sqrt{-d})(t+u\sqrt{-d})=rt-dsu+(ru+st)\sqrt{-d}$ so $(rt-dsu)^2+d(ru+st)^2=mn$. So representations of a number are related to representations of its factors. This works out very simply in the case $x^2+y^2$, where you can derive a formula for the number of representations of $n$ in terms of the prime divisors that are congruent to 1 modulo 4. It can be more messy for other values of $d$ and I don't know it well enough to write it out on the spot and I don't have access to my references. But there are plenty of books and websites that talk about representations by positive definite binary quadratic forms, so the information is out there, you just have to hunt for it.

share|cite|improve this answer
And if n is a prime? – asd Jan 5 '12 at 22:59
There are useful algorithms for $x^2+y^2=p$, and probably for $x^2+dy^2=p$, but I am away from my references so I can't be as helpful as I'd like to be. Try a websearch for Cornacchia and/or Shanks and/or Tonelli, as I think one or more of those people had something to do with good algorithms for the problem. – Gerry Myerson Jan 5 '12 at 23:05
can you explain how factorisation can be used to count solutions to this equation? – asd Jan 6 '12 at 21:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.