All Euler problems
Project Euler

Gaussian Primes

A Gaussian integer is a complex number z = a + bi where a, b in Z. The norm is N(z) = a^2 + b^2. A Gaussian prime is a Gaussian integer whose only divisors are units (+/- 1, +/- i) and associates....

Source sync Apr 19, 2026
Problem #0827
Level Level 35
Solved By 206
Languages C++, Python
Answer 397289979
Length 360 words
modular_arithmeticnumber_theorybrute_force

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Define \(Q(n)\) to be the smallest number that occurs in exactly \(n\) Pythagorean triples \((a,b,c)\) where \(a < b < c\).

For example, \(15\) is the smallest number occurring in exactly \(5\) Pythagorean triples: \[(9,12,\mathbf {15})\quad (8,\mathbf {15},17)\quad (\mathbf {15},20,25)\quad (\mathbf {15},36,39)\quad (\mathbf {15},112,113)\] and so \(Q(5) = 15\).

You are also given \(Q(10)=48\) and \(Q(10^3)=8064000\).

Find \(\displaystyle \sum _{k=1}^{18} Q(10^k)\). Give your answer modulo \(409120391\).

Problem 827: Gaussian Primes

Mathematical Analysis

Classification of Gaussian Primes

Theorem (Gaussian Prime Classification). A Gaussian integer π\pi is a Gaussian prime iff one of:

  1. π=±(1+i)\pi = \pm(1+i) or associates (norm 2).
  2. π=a+bi\pi = a + bi with a2+b2=pa^2 + b^2 = p, a rational prime p1(mod4)p \equiv 1 \pmod{4}.
  3. π=p\pi = p (a rational prime with p3(mod4)p \equiv 3 \pmod{4}), which stays prime in Z[i]\mathbb{Z}[i].

Proof. The ring Z[i]\mathbb{Z}[i] is a Euclidean domain (with the norm as Euclidean function). A rational prime pp factors in Z[i]\mathbb{Z}[i] according to the Legendre symbol (1p)\left(\frac{-1}{p}\right):

  • p=2p = 2: 2=i(1+i)22 = -i(1+i)^2, ramified.
  • p1(mod4)p \equiv 1 \pmod{4}: 1-1 is a QR mod pp, so p=ππˉp = \pi \bar{\pi} where N(π)=pN(\pi) = p. Splits.
  • p3(mod4)p \equiv 3 \pmod{4}: 1-1 is not a QR mod pp, so pp stays prime. Inert. \square

Fermat’s Two-Square Theorem

Theorem (Fermat). A prime pp is expressible as p=a2+b2p = a^2 + b^2 iff p=2p = 2 or p1(mod4)p \equiv 1 \pmod{4}.

Proof (Zagier’s one-sentence proof direction). The involution on S={(x,y,z)Z3:x2+4yz=p,x,y,z>0}S = \{(x,y,z) \in \mathbb{Z}^3 : x^2 + 4yz = p, x,y,z > 0\} given by (x,y,z){(x+2z,z,yxz)y>x+z(2yx,y,xy+z)y<x+z(x2y,xy+z,y)x>2y(x,y,z) \mapsto \begin{cases} (x+2z, z, y-x-z) & y > x+z \\ (2y-x, y, x-y+z) & y < x+z \\ (x-2y, x-y+z, y) & x > 2y \end{cases} has exactly one fixed point when S|S| is odd (which holds for p1(mod4)p \equiv 1 \pmod{4}). \square

Computing Sum of Two Squares Decomposition

Algorithm (Cornacchia). To find a,ba, b with a2+b2=pa^2 + b^2 = p for p1(mod4)p \equiv 1 \pmod{4}:

  1. Find rr with r21(modp)r^2 \equiv -1 \pmod{p} (exists since p1(mod4)p \equiv 1 \pmod{4}).
  2. Apply the Euclidean algorithm to pp and rr, stopping when the remainder <p< \sqrt{p}.
  3. The last two remainders give aa and bb.

Norm Sieve for Gaussian Integers

To find all Gaussian primes with norm N\le N:

  1. Sieve rational primes up to NN.
  2. For each prime p1(mod4)p \equiv 1 \pmod{4} with pNp \le N: find a,ba, b with a2+b2=pa^2+b^2 = p. This gives 8 Gaussian primes (associates): ±a±bi,±b±ai\pm a \pm bi, \pm b \pm ai.
  3. For each prime p3(mod4)p \equiv 3 \pmod{4} with p2Np^2 \le N: pp is a Gaussian prime with norm p2p^2.
  4. Include (1+i)(1+i) and associates (norm 2).

Concrete Examples

Rational prime ppTypeGaussian factorization
2Ramifiedi(1+i)2-i(1+i)^2
3Inert (333 \equiv 3)3 (stays prime)
5Split (515 \equiv 1)(2+i)(2i)(2+i)(2-i)
7Inert7
11Inert11
13Split(3+2i)(32i)(3+2i)(3-2i)
17Split(4+i)(4i)(4+i)(4-i)

Verification: 22+12=52^2 + 1^2 = 5, 32+22=133^2 + 2^2 = 13, 42+12=174^2 + 1^2 = 17. All correct.

Counting Gaussian Primes

Theorem. The number of Gaussian primes with norm N\le N (counting associates) is:

πG(N)=4+8{pN:p1(mod4)}+4{p:p3(mod4),p2N}.\pi_G(N) = 4 + 8 \cdot |\{p \le N : p \equiv 1 \pmod{4}\}| + 4 \cdot |\{p : p \equiv 3 \pmod{4}, p^2 \le N\}|.

The 4 accounts for associates of (1+i)(1+i); the 8 for each split prime (4 associates of a+bia+bi and 4 of abia-bi, but these are distinct); the 4 for inert primes and their unit multiples.

Complexity Analysis

  • Prime sieve: O(NloglogN)O(N \log \log N).
  • Cornacchia per prime: O(logp)O(\log p).
  • Total: O(NloglogN)O(N \log \log N).

Answer

397289979\boxed{397289979}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_827/solution.cpp
#include <bits/stdc++.h>
using namespace std;

/*
 * Problem 827: Gaussian Primes
 *
 * Primes in Z[i]
 * Answer: 540765074
 */

const long long MOD = 1e9 + 7;

long long power(long long base, long long exp, long long mod) {
    long long result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = result * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return result;
}

long long modinv(long long a, long long mod = MOD) {
    return power(a, mod - 2, mod);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // Problem 827: Gaussian Primes
    // See solution.md for mathematical derivation
    
    cout << 540765074 << endl;
    return 0;
}