All Euler problems
Project Euler

Ordered Radicals

The radical of n, denoted rad(n), is the product of the distinct prime factors of n. For example, rad(504) = 2 x 3 x 7 = 42. By convention, rad(1) = 1. Sort the integers 1 <= n <= 100,000 by the pa...

Source sync Apr 19, 2026
Problem #0124
Level Level 04
Solved By 15,322
Languages C++, Python
Answer 21417
Length 291 words
number_theorylinear_algebrabrute_force

Problem Statement

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

The radical of \(n\), \(\operatorname {rad}(n)\), is the product of the distinct prime factors of \(n\). For example, \(504 = 2^3 \times 3^2 \times 7\), so \(\operatorname {rad}(504) = 2 \times 3 \times 7 = 42\).

If we calculate \(\operatorname {rad}(n)\) for \(1 \le n \le 10\), then sort them on \(\operatorname {rad}(n)\), and sorting on \(n\) if the radical values are equal, we get:

Unsorted Sorted
\(n\) \(\operatorname {rad}(n)\)
1 1
2 2
3 3
4 2
5 5
6 6
7 7
8 2
9 3
1010
\(n\) \(\operatorname {rad}(n)\) \(k\)
1 1 1
2 2 2
4 2 3
8 2 4
3 3 5
9 3 6
5 5 7
6 6 8
7 7 9
101010

Let \(E(k)\) be the \(k\)-th element in the sorted \(n\) column; for example, \(E(4) = 8\) and \(E(6) = 9\).

If \(\operatorname {rad}(n)\) is sorted for \(1 \le n \le 100000\), find \(E(10000)\).

Problem 124: Ordered Radicals

Mathematical Development

Definition 1. For a positive integer nn with prime factorization n=p1e1p2e2pmemn = p_1^{e_1} p_2^{e_2} \cdots p_m^{e_m}, the radical is rad(n)=p1p2pm\operatorname{rad}(n) = p_1 p_2 \cdots p_m. Equivalently, rad(n)=pnp\operatorname{rad}(n) = \prod_{p \mid n} p.

Theorem 1 (Radical and squarefreeness). rad(n)=n\operatorname{rad}(n) = n if and only if nn is squarefree. For any prime power pkp^k with k1k \ge 1, rad(pk)=p\operatorname{rad}(p^k) = p.

Proof. If n=p1p2pmn = p_1 p_2 \cdots p_m is squarefree, then rad(n)=p1pm=n\operatorname{rad}(n) = p_1 \cdots p_m = n. Conversely, if some prime pp divides nn with exponent e2e \ge 2, then np2qn,qpq>pqn,qpq=rad(n)n \ge p^2 \cdot \prod_{q \mid n, q \ne p} q > p \cdot \prod_{q \mid n, q \ne p} q = \operatorname{rad}(n). For pkp^k: the unique prime divisor is pp, so rad(pk)=p\operatorname{rad}(p^k) = p. \square

Theorem 2 (Sieve computation of radicals). Initialize rad[n]=1\operatorname{rad}[n] = 1 for all 1nN1 \le n \le N. For each prime pNp \le N (identified during the sieve), multiply rad[m]\operatorname{rad}[m] by pp for every multiple mm of pp in {p,2p,,N}\{p, 2p, \ldots, N\}. Upon completion, rad[n]=pnp\operatorname{rad}[n] = \prod_{p \mid n} p for all nn.

Proof. Each prime pp dividing nn contributes exactly one factor of pp to rad[n]\operatorname{rad}[n], since pp is multiplied in when processing multiples of pp, and this happens exactly once per prime (the sieve only processes primes, not composite numbers). No prime qnq \nmid n contributes to rad[n]\operatorname{rad}[n], since nn is not a multiple of qq. Primes are identified as those pp satisfying rad[p]=1\operatorname{rad}[p] = 1 when pp is first reached (i.e., pp has no prime factor smaller than itself). \square

Lemma 1 (Total order). The lexicographic order on pairs (rad(n),n)(\operatorname{rad}(n), n) is a total order on {1,,N}\{1, \ldots, N\}: for distinct n1n2n_1 \ne n_2, either rad(n1)rad(n2)\operatorname{rad}(n_1) \ne \operatorname{rad}(n_2) (ordered by radical) or rad(n1)=rad(n2)\operatorname{rad}(n_1) = \operatorname{rad}(n_2) and the tiebreaker n1n2n_1 \ne n_2 is decisive.

Proof. Immediate from the definition of lexicographic order and the uniqueness of nn. \square

Editorial

Compute rad(n) for 1 <= n <= 100000 via sieve, sort by (rad(n), n), return E(10000). We first generate the primes required by the search, then enumerate the admissible combinations and retain only the values that satisfy the final test.

Pseudocode

    rad[1..N] = 1
    For p from 2 to N:
        if rad[p] == 1: // p is prime
            for m = p, 2p, ..., N:
                rad[m] *= p

    pairs = [(rad[n], n) for n = 1 to N]
    sort pairs lexicographically
    Return pairs[K-1].n // 0-indexed

Complexity Analysis

  • Time: O(NloglogN)O(N \log \log N) for the sieve (each prime pp visits N/p\lfloor N/p \rfloor multiples; summing over primes pNp \le N gives pNN/p=O(NloglogN)\sum_{p \le N} N/p = O(N \log \log N) by Mertens’ theorem). Sorting requires O(NlogN)O(N \log N). Total: O(NlogN)O(N \log N).
  • Space: O(N)O(N) for the radical array and sorted pairs.

Answer

21417\boxed{21417}

Code

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

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

int main() {
    const int N = 100000;
    const int K = 10000;

    vector<int> rad(N + 1, 1);
    for (int i = 2; i <= N; i++) {
        if (rad[i] == 1) {  // i is prime
            for (int j = i; j <= N; j += i)
                rad[j] *= i;
        }
    }

    vector<pair<int, int>> v;
    v.reserve(N);
    for (int i = 1; i <= N; i++)
        v.push_back({rad[i], i});
    sort(v.begin(), v.end());

    cout << v[K - 1].second << endl;
    return 0;
}