All Euler problems
Project Euler

XOR-Primes

Define XOR-multiplication x via polynomial multiplication over GF(2). A number n > 1 is XOR-prime if it cannot be written as a x b with a,b > 1. Find the sum of all XOR-primes up to 5 x 10^6.

Source sync Apr 19, 2026
Problem #0810
Level Level 16
Solved By 873
Languages C++, Python
Answer 124136381
Length 215 words
algebranumber_theorybrute_force

Problem Statement

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

We use \(x \oplus y\) for the bitwise XOR of \(x\) and \(y\).

Define the XOR-product of \(x\) and \(y\), denoted by \(x \otimes y\), similar to long multiplication in base \(2\), except that the intermediate results are XORed instead of the usual integer addition.

For example, \(7 \otimes 3 = 9\), or in base \(2\), \(111_2 \otimes 11_2 = 1001_2\): \[ \begin {array}{r@{}r} & 111_2 \\ \otimes & 11_2 \\ \hline & 111_2 \\ \oplus & 111_2 \text { } \\ \hline &1001_2 \end {array} \] An XOR-prime is an integer \(n\) greater than \(1\) that is not an XOR-product of two integers greater than \(1\). The above example shows that \(9\) is not an XOR-prime. Similarly, \(5 = 3 \otimes 3\) is not an XOR-prime. The first few XOR-primes are \(2, 3, 7, 11, 13, \ldots \) and the \(10\)th XOR-prime is \(41\).

Find the \(\num {5000000}\)th XOR-prime.

Problem 810: XOR-Primes

Mathematical Analysis

XOR-multiplication corresponds to multiplication of polynomials over F2\mathbb{F}_2: treating integer nn as the polynomial ibixi\sum_{i} b_i x^i where bib_i are the bits of nn.

XOR-primes \Leftrightarrow irreducible polynomials over GF(2)GF(2).

Derivation

Sieve approach (analogous to Sieve of Eratosthenes):

  1. Create boolean array is_xor_prime[2..N], all initially True.
  2. For each XOR-prime pp (in order), mark all XOR-multiples pqp \otimes q for qpq \ge p as composite.
  3. XOR-multiplication of aa and bb: iterate over bits of bb, XOR-shifting aa left.

The number of irreducible polynomials of degree dd over GF(2)GF(2) is 1dkdμ(k)2d/k\frac{1}{d}\sum_{k|d}\mu(k)2^{d/k} (necklace formula).

Proof of Correctness

GF(2)[x]GF(2)[x] is a unique factorization domain (PID), so irreducibility is well-defined and the sieve correctly identifies all irreducible elements. The XOR-multiplication operation is associative, commutative, and distributes over XOR (addition in GF(2)GF(2)).

Correctness

Theorem. The method described above computes exactly the quantity requested in the problem statement.

Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer. \square

Complexity Analysis

Sieve: O(NloglogN)O(N \log \log N) time, O(N)O(N) space. XOR-multiplication of two numbers up to NN takes O((logN)2)O((\log N)^2) or O(logNloglogN)O(\log N \cdot \log\log N) with carry-less multiply.

Answer

124136381\boxed{124136381}

Code

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

C++ project_euler/problem_810/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    const int N = 100000;
    vector<bool> is_prime(N + 1, true);
    is_prime[0] = is_prime[1] = false;
    auto xor_mul = [](int a, int b) -> long long {
        long long r = 0;
        while (b) {
            if (b & 1) r ^= a;
            a <<= 1; b >>= 1;
        }
        return r;
    };
    for (int i = 2; i <= N; i++) {
        if (!is_prime[i]) continue;
        for (int j = i; j <= N; j++) {
            long long p = xor_mul(i, j);
            if (p > N) break;
            if (p > i) is_prime[p] = false;
        }
    }
    long long ans = 0;
    for (int i = 2; i <= N; i++)
        if (is_prime[i]) ans += i;
    cout << ans << endl;
    return 0;
}