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 May 21, 2026
Problem #0124
Level Level 04
Solved By 15,322
Languages C++, Python
Answer 21417
Length 331 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=p1e1p2e2⋯pmemn = p_1^{e_1} p_2^{e_2} \cdots p_m^{e_m}, the radical is rad⁡(n)=p1p2⋯pm\operatorname{rad}(n) = p_1 p_2 \cdots p_m. Equivalently, rad⁡(n)=∏p∣np\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 k≥1k \ge 1, rad⁡(pk)=p\operatorname{rad}(p^k) = p.

Proof. If n=p1p2⋯pmn = p_1 p_2 \cdots p_m is squarefree, then rad⁡(n)=p1⋯pm=n\operatorname{rad}(n) = p_1 \cdots p_m = n. Conversely, if some prime pp divides nn with exponent e≥2e \ge 2, then n≥p2⋅∏q∣n,q≠pq>p⋅∏q∣n,q≠pq=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 1≤n≤N1 \le n \le N. For each prime p≤Np \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]=∏p∣np\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 q∤nq \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 n1≠n2n_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 n1≠n2n_1 \ne n_2 is decisive.

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

Editorial

The expensive part is not the final selection but computing all radicals efficiently. A sieve handles that cleanly: each time a prime pp is discovered, multiply pp into the radical of every multiple of pp. Because each prime contributes only once to each number, the resulting array is exactly rad⁡(n)\operatorname{rad}(n) for every 1≤n≤1000001 \le n \le 100000.

After that, the rest is a pure ordering problem. Form the pairs (rad⁡(n),n)(\operatorname{rad}(n), n), sort them lexicographically, and read off the entry in position 10000. The secondary key nn automatically resolves ties between equal radicals.

Pseudocode

Initialize every radical value to 1.

Sweep through the integers from 2 up to 100000.
Whenever the current integer is still marked with radical 1, treat it as prime and multiply it into the radical of each of its multiples.

Build the list of pairs (rad(n), n) for all n in the range.
Sort that list in lexicographic order and return the n-value in position 10000.

Complexity Analysis

  • Time: O(Nlog⁡log⁡N)O(N \log \log N) for the sieve (each prime pp visits ⌊N/p⌋\lfloor N/p \rfloor multiples; summing over primes p≤Np \le N gives ∑p≤NN/p=O(Nlog⁡log⁡N)\sum_{p \le N} N/p = O(N \log \log N) by Mertens’ theorem). Sorting requires O(Nlog⁡N)O(N \log N). Total: O(Nlog⁡N)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;
}