Sum of Two Squares Calculator

Which numbers are a^2 + b^2, and in how many ways?

  • Every representation
  • Count r2(n)
  • Large prime steps
A small rounded clay table holding one large soft clay square block beside two smaller soft clay square blocks, and a few loose rounded beads.

The guide

Which numbers are a sum of two squares, and the Gaussian trick that lists every way

A number is a^2 + b^2 exactly when its primes that leave remainder 3 on division by 4 come in pairs, and the Gaussian integers turn that fact into a way of finding and counting every representation.

  • six ways 5525 = 5^2 x 13 x 17 as a^2 + b^2 with a at least b
  • r2 = 48 signed ordered count for 5525, from 4 (d1 - d3)
  • 97 = 9^2 + 4^2 the prime 97 split by a non-residue and Euclid steps
  • 216,341 numbers up to 1,000,000 that are sums of two squares
  • 5.2 percent that count above the Landau-Ramanujan estimate
Read the full guide Fold the guide 6 min · 12 sources

Which numbers are a sum of two squares

Take 5, 13, 17, 29, 37 and 41. They are 1 + 4, 4 + 9, 1 + 16, 4 + 25, 1 + 36 and 16 + 25, each a sum of two squares. Now take 3, 7, 11 and 19. None of them is. The line between the two groups is a remainder: an odd prime is a sum of two squares exactly when it leaves remainder 1 on division by 4.

Albert Girard described which numbers can be written this way in 1625, and Fermat announced the result for primes. Euler found the first proof and reported it to Goldbach in a letter of 12 April 1749, after much effort, using a method of infinite descent.

For numbers that are not prime, the rule is a statement about the prime factors. Write n as a product of primes. Then n is a sum of two squares exactly when every prime that is 3 mod 4 appears with an even exponent. The primes that are 1 mod 4 may appear any number of times, and the prime 2 never matters.

The number 45 shows it. Its factorisation is 3^2 x 5. The prime 3 is 3 mod 4 and has the even exponent 2, and 5 is 1 mod 4, so 45 is a sum of two squares: 9 + 36. The number 21 is 3 x 7, with two primes that are 3 mod 4 and an odd exponent on each, so it is not one.

That also kills a tempting shortcut. The remainder of n on division by 4 does not decide the question. A remainder of 3 does rule a number out, but 21 leaves remainder 1 and still fails. The test has to look at the primes.

How a prime is split

Knowing that 97 is a sum of two squares is not the same as finding them. Stan Wagon described a method in 1990, built on work by Serret and Hermite in 1848 and Cornacchia in 1908. For a prime p that is 1 mod 4 it needs a square root of -1 modulo p.

To get one, find a quadratic non-residue q, a number with q^((p - 1)/2) = -1 mod p, and put x = q^((p - 1)/4) mod p. Then x^2 = -1 mod p. About half of all candidates are non-residues, so a few tries are enough. The tool takes the smallest one, which makes its steps repeatable.

Now run the Euclidean algorithm on p and x, and watch the remainders. The first two that fall below the square root of p are the numbers a and b, and a^2 + b^2 = p. For 97 the page used the non-residue 13, which gives x = 22. The steps are 97 = 22 x 4 + 9, 22 = 9 x 2 + 4, 9 = 4 x 2 + 1 and 4 = 1 x 4. The remainders 9 and 4 are below the square root of 97, which is about 9.85, and 97 = 9^2 + 4^2.

The smallest non-residue of 97 is actually 5, not 13, and it gives the same x = 22. Any non-residue works, and the pair at the end does not depend on the choice.

The method scales to large primes. For p = 1,000,000,009 the smallest non-residue is 11, x = 569,522,298, and the pair is 31,400 and 3,747, because 985,960,000 + 14,040,009 = 1,000,000,009. That is the only representation, as it is for every prime.

Gluing the pieces together

Why does this extend from primes to every number? The Gaussian integers are the numbers a + bi with whole a and b. The norm of a + bi is a^2 + b^2, and the norm of a product is the product of the norms. A sum of two squares is therefore the norm of a Gaussian integer, and the product of two such numbers is another one.

A prime p that is 1 mod 4 splits in the Gaussian integers as (a + bi)(a - bi), with a^2 + b^2 = p. A prime that is 3 mod 4 stays prime and can only contribute through its square, as the real factor. The prime 2 is a unit times the square of (1 + i). Each representation of n is a choice of one such factor for every prime power, multiplied together.

Written without complex numbers, this is the Brahmagupta-Fibonacci identity: (a^2 + b^2)(c^2 + d^2) = (ac - bd)^2 + (ad + bc)^2 = (ac + bd)^2 + (ad - bc)^2. The two forms are not the same, and keeping both is the whole point. They correspond to multiplying by c + di and by its conjugate c - di.

A small case: 65 = 5 x 13 = (1^2 + 2^2)(2^2 + 3^2). The first form gives 4^2 + 7^2 and the second gives 8^2 + 1^2, and those are the two representations of 65. Use only one form and the answer loses one of them.

A larger one is 5525 = 5^2 x 13 x 17. The tool combines the splits of 25, 13 and 17 and finds six ways: 74^2 + 7^2, 73^2 + 14^2, 71^2 + 22^2, 70^2 + 25^2, 62^2 + 41^2 and 55^2 + 50^2. Every one of them adds up to 5525, which is easy to check by hand.

Counting, and the zero case

Counting needs a decision about what counts. If order and signs matter, then 3^2 + 4^2 has eight forms: (3, 4), (4, 3), (-3, 4) and so on. The function that counts them is called r2, and r2(n) = 4 (d1 - d3), where d1 and d3 are the numbers of divisors of n that are 1 and 3 mod 4.

For 5525 the odd divisors are all 1 mod 4, so d1 = 12 and d3 = 0, and r2 = 48. The list agrees: six representations of eight forms each is 48. The tool counts both ways and shows the result, and a disagreement would be displayed, never hidden.

Every extra prime that is 1 mod 4 doubles the count. The product 5 x 13 x 17 x 29 x 37 x 41 x 53 x 61 is 157,163,452,745 and has eight such primes, so it has 2^7 = 128 representations and r2 = 1024. A plain loop over b, written separately from the identity and run outside the page, finds the same 128.

A representation with b = 0 is a legitimate one, and forgetting it is the most common slip. Take 25. It is 4^2 + 3^2 and also 5^2 + 0^2, so it has two representations and r2(25) = 12, made of 8 forms for the first and 4 for the second. Counting only positive a and b would report one.

The same adjustment applies when a = b. The number 50 is 7^2 + 1^2 and 5^2 + 5^2, with r2(50) = 12: 8 forms for the first and 4 for the second. And 9 = 3^2 + 0^2 gives r2(9) = 4, as r2(1) = 4 and r2(2) = 4.

How common, and what this tool does not do

Sums of two squares thin out as numbers grow. Landau proved in 1908 that the count of positive integers below x that are sums of two squares behaves like b x / sqrt(ln x), where b is the Landau-Ramanujan constant, 0.7642236535892206. Ramanujan found it again in 1913 in his first letter to Hardy.

The estimate converges slowly. Counting by machine, 216,341 numbers from 1 to 1,000,000 are sums of two squares, against 205,607 from the formula. The count is 5.2 percent higher, which is typical of how slowly such an asymptotic formula settles down.

The tool handles whole numbers up to 100,000,000,000,000 (1e14). It factors by trial division up to 10,000,000, and because 1e14 is the square of that, any cofactor left over is prime. It does the modular powers in BigInt, which holds integers beyond the largest safe Number, and keeps the rest of the arithmetic below 2^53, where it is exact.

It does not prove anything; it shows the theorem at work. It does not cover sums of three squares, which follow a different rule, or sums of four, where Lagrange showed that every natural number is one. It does not treat other quadratic forms or other number rings. Within its limits it lists every representation, counts them two ways and shows each step.

Sources

Every source consulted for this guide. Links checked November 21, 2026.

  1. Fermat's theorem on sums of two squares Wikipedia (CC BY-SA)
  2. Sum of two squares theorem Wikipedia (CC BY-SA)
  3. Brahmagupta-Fibonacci identity Wikipedia (CC BY-SA)
  4. Gaussian integer Wikipedia (CC BY-SA)
  5. Sum of squares function Wikipedia (CC BY-SA)
  6. Cornacchia's algorithm Wikipedia (CC BY-SA)
  7. Landau-Ramanujan constant Wikipedia (CC BY-SA)
  8. Quadratic residue Wikipedia (CC BY-SA)
  9. Euclidean algorithm Wikipedia (CC BY-SA)
  10. Integer factorization Wikipedia (CC BY-SA)
  11. Lagrange's four-square theorem Wikipedia (CC BY-SA)
  12. BigInt MDN Web Docs (CC BY-SA)