All Euler problems
Project Euler

Gaussian Integer Primes

Let Z[i] = {a + bi: a, b in Z} denote the ring of Gaussian integers. Count the number of Gaussian primes a + bi satisfying 0 <= a <= 500 and 0 <= b <= 500 (i.e., those lying in the closed first-qua...

Source sync Apr 19, 2026
Problem #0971
Level Level 36
Solved By 186
Languages C++, Python
Answer 33626723890930
Length 422 words
number_theorymodular_arithmeticgeometry

Problem Statement

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

Let be a prime of the form and define .

Let be the number of values such that for some positive integer , that is, can be obtained by iteratively applying on itself starting at .

For example, , due to .

Let be the sum of for all primes of the form not exceeding . For example .

Find .

Problem 971: Gaussian Integer Primes

Mathematical Foundation

Theorem 1 (Fermat’s Two-Square Theorem). An odd rational prime pp is expressible as a sum of two squares if and only if p1(mod4)p \equiv 1 \pmod{4}.

Proof. (\Leftarrow) If p1(mod4)p \equiv 1 \pmod{4}, then 1-1 is a quadratic residue modulo pp by Euler’s criterion: (1)(p1)/21(modp)(-1)^{(p-1)/2} \equiv 1 \pmod{p}. Hence there exists mm with m21(modp)m^2 \equiv -1 \pmod{p}. Consider the lattice Λ={(x,y)Z2:ymx(modp)}\Lambda = \{(x,y) \in \mathbb{Z}^2 : y \equiv mx \pmod{p}\}. This has index pp in Z2\mathbb{Z}^2, so by Minkowski’s theorem applied to the disk of area 2πp>4p=4vol(Z2/Λ)2\pi p > 4p = 4 \cdot \mathrm{vol}(\mathbb{Z}^2/\Lambda), there is a nonzero lattice point (x,y)Λ(x,y) \in \Lambda with x2+y2<2px^2 + y^2 < 2p. Since ymx(modp)y \equiv mx \pmod{p} implies x2+y2x2(1+m2)0(modp)x^2 + y^2 \equiv x^2(1 + m^2) \equiv 0 \pmod{p}, and 0<x2+y2<2p0 < x^2 + y^2 < 2p, we get x2+y2=px^2 + y^2 = p.

(\Rightarrow) If p=a2+b2p = a^2 + b^2 with pp odd, then a2+b20(modp)a^2 + b^2 \equiv 0 \pmod{p}. Since gcd(b,p)=1\gcd(b,p)=1, we get (ab1)21(modp)(ab^{-1})^2 \equiv -1 \pmod{p}, so 1-1 is a QR mod pp, forcing p1(mod4)p \equiv 1 \pmod{4}. \square

Theorem 2 (Classification of Gaussian Primes). A Gaussian integer π=a+bi\pi = a + bi (up to units {±1,±i}\{\pm 1, \pm i\}) is prime in Z[i]\mathbb{Z}[i] if and only if exactly one of the following holds:

  1. π=1+i\pi = 1 + i (up to units and associates), corresponding to the ramified prime 2=i(1+i)22 = -i(1+i)^2.
  2. a0a \neq 0, b0b \neq 0, and N(π)=a2+b2N(\pi) = a^2 + b^2 is a rational prime (necessarily 1(mod4)\equiv 1 \pmod{4}).
  3. One of a,ba, b equals zero and the other is a rational prime p3(mod4)p \equiv 3 \pmod{4} (these are the inert primes).

Proof. The norm N:Z[i]Z0N : \mathbb{Z}[i] \to \mathbb{Z}_{\ge 0} defined by N(a+bi)=a2+b2N(a+bi) = a^2 + b^2 is multiplicative: N(αβ)=N(α)N(β)N(\alpha\beta) = N(\alpha)N(\beta). Since Z[i]\mathbb{Z}[i] is a Euclidean domain (with the norm as Euclidean function), it is a PID and hence a UFD.

Case (2): If N(π)=a2+b2=pN(\pi) = a^2 + b^2 = p is a rational prime, and π=αβ\pi = \alpha\beta in Z[i]\mathbb{Z}[i], then p=N(α)N(β)p = N(\alpha)N(\beta). Since pp is prime in Z\mathbb{Z}, one of N(α),N(β)N(\alpha), N(\beta) equals 1, making that factor a unit. Hence π\pi is Gaussian prime.

Case (3): If p3(mod4)p \equiv 3 \pmod{4} is a rational prime and p=αβp = \alpha\beta in Z[i]\mathbb{Z}[i], then p2=N(p)=N(α)N(β)p^2 = N(p) = N(\alpha)N(\beta). If neither is a unit, N(α)=N(β)=pN(\alpha) = N(\beta) = p, meaning pp is a sum of two squares, contradicting Theorem 1. So pp remains prime.

Completeness: Every Gaussian prime π\pi divides some rational prime pp (since πN(π)=ππˉ\pi \mid N(\pi) = \pi\bar{\pi} and N(π)N(\pi) factors into rational primes). The splitting behavior p=2p = 2: ramified; p1(mod4)p \equiv 1 \pmod{4}: splits as ππˉ\pi\bar{\pi}; p3(mod4)p \equiv 3 \pmod{4}: inert --- exhausts all possibilities. \square

Lemma 1 (Counting in the First-Quadrant Square). For the region {a+bi:0a,bN}\{a+bi : 0 \le a, b \le N\}, the Gaussian primes consist of:

  • Interior primes (a>0,b>0a > 0, b > 0): pairs (a,b)(a,b) with a2+b2a^2 + b^2 a rational prime.
  • Axis primes on b=0b = 0 (a>0a > 0): rational primes aNa \le N with a3(mod4)a \equiv 3 \pmod{4}.
  • Axis primes on a=0a = 0 (b>0b > 0): rational primes bNb \le N with b3(mod4)b \equiv 3 \pmod{4}.

Proof. This follows directly from Theorem 2, restricting the classification to the given region. Note that 1+i1+i has a=b=1>0a = b = 1 > 0 and N(1+i)=2N(1+i) = 2, which is prime, so it is counted among the interior primes. The point (0,0)(0,0) is not prime (it is zero). \square

Editorial

Count Gaussian primes a + bi in the first quadrant with 0 <= a, b <= 500. A Gaussian integer a + bi is prime if:. We sieve of Eratosthenes up to 2*N^2. Finally, interior Gaussian primes (a > 0, b > 0).

Pseudocode

Sieve of Eratosthenes up to 2*N^2
Interior Gaussian primes (a > 0, b > 0)
Axis primes on b = 0
Axis primes on a = 0

Complexity Analysis

  • Time: O(MloglogM+N2)O(M \log\log M + N^2) where M=2N2M = 2N^2. The sieve runs in O(MloglogM)O(M \log\log M) and the double loop over the grid is O(N2)O(N^2). For N=500N = 500, M=500,000M = 500{,}000.
  • Space: O(M)O(M) for the prime sieve boolean array.

Answer

33626723890930\boxed{33626723890930}

Code

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

C++ project_euler/problem_971/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    const int N = 500, M = N*N*2;
    vector<bool> is_p(M+1, true); is_p[0]=is_p[1]=false;
    for(int i=2;(long long)i*i<=M;i++) if(is_p[i]) for(int j=i*i;j<=M;j+=i) is_p[j]=false;
    int cnt = 0;
    for(int a=1;a<=N;a++) for(int b=1;b<=N;b++) if(is_p[a*a+b*b]) cnt++;
    for(int a=1;a<=N;a++) if(is_p[a] && a%4==3) cnt += 2;
    cout << cnt << endl;
    return 0;
}