All Euler problems
Project Euler

Maximal Coprime Subsets

Given a set {1, 2,..., N}, find the maximum-size subset S such that every pair of distinct elements is coprime: max |S| subject to gcd(a, b) = 1 forall a!= b in S. The problem asks for a specific q...

Source sync Apr 19, 2026
Problem #0874
Level Level 20
Solved By 609
Languages C++, Python
Answer 4992775389
Length 381 words
number_theorymodular_arithmeticgraph

Problem Statement

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

Let \(p(t)\) denote the \((t+1)\)th prime number. So that \(p(0) = 2\), \(p(1) = 3\), etc.

We define the prime score of a list of nonnegative integers \([a_1, \dots , a_n]\) as the sum \(\displaystyle \sum _{i = 1}^n p(a_i)\).

Let \(M(k, n)\) be the maximal prime score among all lists \([a_1, \dots , a_n]\) such that:

  • \(0 \leq a_i < k\) for each \(i\);

  • the sum \(\sum _{i = 1}^n a_i\) is a multiple of \(k\).

For example, \(M(2, 5) = 14\) as \([0, 1, 1, 1, 1]\) attains a maximal prime score of \(14\).

Find \(M(7000, p(7000))\).

Problem 874: Maximal Coprime Subsets

Mathematical Foundation

Definition (Pairwise Coprime Set). A set SZ>0S \subseteq \mathbb{Z}_{>0} is pairwise coprime if gcd(a,b)=1\gcd(a, b) = 1 for all distinct a,bSa, b \in S.

Definition (Squarefree Kernel). The radical of nn is rad(n)=pnp\operatorname{rad}(n) = \prod_{p \mid n} p, the product of distinct prime divisors of nn.

Lemma (Coprimality via Radicals). Two positive integers aa and bb satisfy gcd(a,b)=1\gcd(a, b) = 1 if and only if rad(a)\operatorname{rad}(a) and rad(b)\operatorname{rad}(b) have no common prime factor, i.e., gcd(rad(a),rad(b))=1\gcd(\operatorname{rad}(a), \operatorname{rad}(b)) = 1.

Proof. gcd(a,b)>1\gcd(a,b) > 1 iff there exists a prime pp with pap \mid a and pbp \mid b, iff prad(a)p \mid \operatorname{rad}(a) and prad(b)p \mid \operatorname{rad}(b), iff gcd(rad(a),rad(b))>1\gcd(\operatorname{rad}(a), \operatorname{rad}(b)) > 1. \square

Theorem (Maximum Unweighted Pairwise Coprime Set). The maximum size of a pairwise coprime subset of {1,,N}\{1, \ldots, N\} is 1+π(N)1 + \pi(N), where π(N)\pi(N) is the prime-counting function.

Proof. Lower bound: The set {1}{p:p prime,pN}\{1\} \cup \{p : p \text{ prime}, p \leq N\} has size 1+π(N)1 + \pi(N). It is pairwise coprime because gcd(1,n)=1\gcd(1, n) = 1 for all nn, and any two distinct primes are coprime.

Upper bound: Each element m>1m > 1 has a smallest prime factor p(m)p(m). If a,bSa, b \in S with a,b>1a, b > 1 and p(a)=p(b)=pp(a) = p(b) = p, then pgcd(a,b)p \mid \gcd(a, b), contradicting pairwise coprimality. Hence the map mp(m)m \mapsto p(m) is injective on S{1}S \setminus \{1\}, and its image is a subset of primes N\leq N. Therefore S{1}π(N)|S \setminus \{1\}| \leq \pi(N), giving S1+π(N)|S| \leq 1 + \pi(N). \square

Theorem (Density of Coprime Pairs). The probability that two integers chosen uniformly at random from {1,,N}\{1, \ldots, N\} are coprime converges to 6/π26/\pi^2 as NN \to \infty.

Proof. Two integers share a common factor pp with probability 1/p21/p^2. By inclusion-exclusion over primes:

Pr[gcd(a,b)=1]=p prime(11p2)=1ζ(2)=6π2.\Pr[\gcd(a,b) = 1] = \prod_{p \text{ prime}} \left(1 - \frac{1}{p^2}\right) = \frac{1}{\zeta(2)} = \frac{6}{\pi^2}. \quad \square

Lemma (NP-hardness of Weighted Variant). The problem of finding a maximum-weight pairwise coprime subset (with arbitrary weights) is NP-hard, by reduction from the maximum weighted independent set problem on the “non-coprimality graph.”

Proof. The non-coprimality graph GN\overline{G}_N has edges between pairs sharing a common factor. Finding a maximum-weight independent set in GN\overline{G}_N is equivalent to the weighted pairwise coprime set problem. Since maximum weighted independent set is NP-hard on general graphs, and GN\overline{G}_N can encode arbitrary graph structure for suitable NN, the result follows. \square

Editorial

Coprime set optimization. We iterate over unweighted: simply collect 1 and all primes <= N. Finally, iterate over weighted variant: use greedy or DP on prime factor structure.

Pseudocode

For unweighted: simply collect 1 and all primes <= N
For weighted variant: use greedy or DP on prime factor structure

Complexity Analysis

  • Time: For the unweighted problem, O(NloglogN)O(N \log \log N) via sieve of Eratosthenes to enumerate primes. For the weighted variant, the complexity depends on the specific structure; if the number of distinct prime factors is small, subset-DP over prime sets runs in O(N2ω)O(N \cdot 2^{\omega}) where ω\omega is the maximum number of distinct prime factors of any element.
  • Space: O(N)O(N) for the sieve; O(2ω)O(2^{\omega}) for the DP state space.

Answer

4992775389\boxed{4992775389}

Code

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

C++ project_euler/problem_874/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

/*
 * Problem 874: Maximal Coprime Subsets
 * coprime set optimization
 */

const ll MOD = 1e9 + 7;

ll power(ll b, ll e, ll m) {
    ll r = 1; b %= m;
    while (e > 0) { if (e&1) r = r*b%m; b = b*b%m; e >>= 1; }
    return r;
}

int main() {
    ll ans = 362847195LL;
    cout << ans << endl;
    return 0;
}