Fermat's Theorem On Sums Of Two Squares

8 min read

Ever looked at a prime number and wondered which ones can be written as the sum of two perfect squares? Sounds like a party trick. But it's one of those quiet little corners of math that actually tells you something deep about how numbers behave.

Not obvious, but once you see it — you'll see it everywhere.

Here's the thing — most people hear "Fermat" and immediately think of that impossible-looking last theorem. But fermat's theorem on sums of two squares came decades earlier, and it's way more approachable. It answers a simple question: when can a prime be split into a² + b²?

What Is Fermat's Theorem on Sums of Two Squares

So what are we even talking about? In plain language, fermat's theorem on sums of two squares says this: a prime number p can be expressed as the sum of two squares (so p = a² + b² for some whole numbers a and b) if and only if p is equal to 2 or p leaves a remainder of 1 when you divide it by 4.

Real talk — this step gets skipped all the time.

That's the whole claim. Which means prime 13? That's 1² + 2². Try it. But 3, 7, 11 — none of those can be done. And 2² + 3². Primes like 5? You'll spin your wheels Most people skip this — try not to..

The Prime 2 Is Weird (But Included)

Two is the only even prime, and it gets a free pass. It's 1² + 1². So right away, the theorem isn't just "primes that are 1 mod 4." Two is its own special case, and the theorem quietly covers it That alone is useful..

What "1 Mod 4" Actually Means

When we say a prime is 1 mod 4, we mean if you divide it by 4, the leftover is 1. So no exceptions. Day to day, those are the ones that can't be written as two squares. So 5, 13, 17, 29, 37 — all of those. The others — 3, 7, 11, 19, 23 — leave a remainder of 3. That's the beautiful part It's one of those things that adds up..

Why It Matters / Why People Care

Why does this matter? Even so, they aren't. Because most people skip it and assume primes are just a random bag of atoms. This theorem shows primes have a hidden structure based on how they relate to 4.

In practice, that structure shows up all over the place. Which means cryptography, signal processing, and even the geometry of lattices lean on facts about which numbers split nicely. And if you've ever studied complex numbers, the whole idea of Gaussian integers (numbers like a + bi) rests on exactly this question. A prime that's 1 mod 4 isn't prime anymore in that weird extended number system. It splits. A prime that's 3 mod 4 stays stubbornly prime. Wild, right?

Turns out, this theorem is also a gateway drug. Once you see that primes have personalities — some split, some don't — you start asking better questions about numbers in general. And honestly, this is the part most guides get wrong: they present it as trivia. Plus, it's not. It's a window into algebraic number theory Worth keeping that in mind..

How It Works (or How to Do It)

The meaty middle. Let's actually break down how you'd use fermat's theorem on sums of two squares, and why the proof ideas make sense even if we don't do a full formal proof.

Step One: Check the Prime

Take your prime p. Divide by 4. If the remainder is 3, stop. It can't be done. If it's 1 (or the number is 2), you're good — a representation exists.

Real talk, this alone saves you time. Want to know if 41 works? 41 divided by 4 is 10 with remainder 1. So yes. And sure enough, 41 = 4² + 5² = 16 + 25.

Step Two: Find the Squares (Brute Force for Small Primes)

For small primes, just count up. Take p = 29. Still, squares below 29 are 1, 4, 9, 16, 25. Here's the thing — subtract each from 29: you get 28, 25, 20, 13, 4. Plus, only 25 and 4 are squares. So 29 = 5² + 2². Done Took long enough..

Step Three: The Deeper "Why" — Quadratic Residues

Here's what most people miss. The reason 1 mod 4 primes work comes down to -1 having a square root modulo p. That sounds technical, but it just means: for primes where p = 1 mod 4, there's some number x where x² leaves a remainder of -1 (or p-1) when divided by p.

And yeah — that's actually more nuanced than it sounds It's one of those things that adds up..

For p = 5, x = 2 works: 2² = 4, which is -1 mod 5. Because of that, that's the seed. From there, a clever counting argument (Fermat's own descent method, later cleaned up by Euler) shows you can build actual integers a and b Simple as that..

Step Four: Fermat's Descent

Fermat loved proof by infinite descent. The idea: assume a prime p = 1 mod 4 can't be written as two squares. Which means show that leads to a smaller prime of the same type that also can't. Which means keep going, you hit nonsense. So the original must be writable. It's elegant, and a little sneaky And it works..

Step Five: Extending Past Primes

The theorem isn't just for primes. But a regular number n can be written as a sum of two squares if and only if every prime factor of n that's 3 mod 4 shows up with an even exponent. So 45 = 9 × 5 = 3² × 5. That said, the 3 is 3 mod 4 but its exponent is 2 (even), so 45 works: 36 + 9 = 6² + 3². But 21 = 3 × 7? Practically speaking, both 3 mod 4, both exponent 1. On the flip side, nope. Can't do it.

Common Mistakes / What Most People Get Wrong

I know it sounds simple — but it's easy to miss the fine print Worth keeping that in mind..

First mistake: thinking all odd primes work. They don't. Only the 1 mod 4 ones. That's why the 3 mod 4 primes are permanently excluded. Permanently.

Second: forgetting zero counts as a square. 3² + 0² = 9. Is 9 a sum of two squares? But 9 isn't prime, so the prime theorem doesn't apply — still, when people generalize, they forget 0² is legal.

Third: assuming the representation is unique. Here's the thing — for primes, it basically is (up to order and signs). But for composites, not always. Which means 50 = 1² + 7² = 25 + 49, and also 5² + 5² = 25 + 25. Two different ways The details matter here..

Fourth: confusing this with Fermat's Last Theorem. Different beast entirely. Last theorem is about xⁿ + yⁿ = zⁿ having no solutions for n > 2. This one is just n = 2, and solutions absolutely exist. The name overlap trips up beginners constantly.

Not obvious, but once you see it — you'll see it everywhere.

Practical Tips / What Actually Works

If you're studying this for a class or just for fun, here's what actually works.

Start by memorizing the first few 1 mod 4 primes: 5, 13, 17, 29, 37, 41, 53, 61. Write each as squares. You'll start seeing patterns — like how the two squares are never both even, and for odd primes they're one even one odd.

Use a modular arithmetic cheat sheet. Before trying to find a and b, confirm -1 is a quadratic residue mod p. If you're coding, a one-line loop in Python can test candidates faster than hand math.

And look — if you want to really get it, read Euler's proof. In practice, fermat never published a full one. Euler filled the gap. Seeing how a 1 mod 4 prime splits in Gaussian integers made the whole thing click for me in a way the descent argument never did Most people skip this — try not to..

Most guides skip this. Don't.

Another tip: don't just work with primes. Take composite numbers and use the exponent rule. It's the best way to build intuition for why the prime case matters so much.

FAQ

**Which primes can be written as

the sum of two squares?

Only those congruent to 1 modulo 4, along with the prime 2 itself. So 2 = 1² + 1², 5 = 2² + 1², 13 = 3² + 2², and so on. Primes congruent to 3 modulo 4, such as 3, 7, 11, and 19, cannot be expressed this way under any circumstances Turns out it matters..

Why does the descent argument work without constructing the squares?

Because it proves existence by contradiction. This leads to repeating this gives an infinite chain of smaller and smaller positive primes — impossible. That's why if a 1 mod 4 prime couldn't be written as two squares, you could build a smaller prime of the same type with the same defect. The contradiction forces the original prime to be representable, even if the proof doesn't hand you the actual numbers That's the part that actually makes a difference..

Do negative integers or fractions count as squares here?

No. The theorem lives in the integers. A "square" means the square of an integer (including zero), so fractions and negatives are out. Gaussian integers extend the idea, but the classical sum-of-two-squares result is strictly about whole numbers.

What's the fastest way to check a random number by hand?

Factor it. And if every 3 mod 4 prime in the factorization has an even exponent, it works; if any appears to an odd power, it doesn't. For a prime, just check the remainder modulo 4 Simple, but easy to overlook..


In the end, the sum-of-two-squares theorem is one of those rare results that is easy to state, surprising in its asymmetry, and deep in its connections — from elementary descent to the arithmetic of Gaussian integers. Whether you approach it through Fermat's clever contradictions or Euler's algebraic clarity, the takeaway is the same: number theory keeps drawing clean lines where you'd least expect them, and the 3 mod 4 primes will always sit firmly on the wrong side of this one.

Latest Drops

The Latest

Similar Ground

Other Perspectives

Thank you for reading about Fermat's Theorem On Sums Of Two Squares. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home