All Euler problems
Project Euler

Factor Shuffle

Define the factor shuffle operation: given n = p_1^(a_1) p_2^(a_2)... p_r^(a_r) with primes p_1 < p_2 <... < p_r, rearrange the exponents to produce sort(n) = p_1^a_(sigma(1)) p_2^a_(sigma(2))... p...

Source sync Apr 19, 2026
Problem #0823
Level Level 34
Solved By 221
Languages C++, Python
Answer 865849519
Length 345 words
modular_arithmeticnumber_theorybrute_force

Problem Statement

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

A list initially contains the numbers \(2, 3, \dots , n\).

At each round, every number in the list is divided by its smallest prime factor. Then the product of these smallest prime factors is added to the list as a new number. Finally, all numbers that become \(1\) are removed from the list.

For example, below are the first three rounds for \(n = 5\): \[[2, 3, 4, 5] \xrightarrow {(1)} [2, 60] \xrightarrow {(2)} [30, 4] \xrightarrow {(3)} [15, 2, 4].\]

Let \(S(n, m)\) be the sum of all numbers in the list after \(m\) rounds.

For example, \(S(5, 3) = 15 + 2 + 4 = 21\). Also \(S(10, 100) = 257\).

Find \(S(10^4, 10^{16})\). Give your answer modulo \(1234567891\).

Problem 823: Factor Shuffle

Mathematical Analysis

Exponent Signature

Definition. The exponent signature of nn is the multiset of exponents {a1,a2,,ar}\{a_1, a_2, \ldots, a_r\} in the prime factorization of nn. The factor shuffle assigns exponents to primes in non-increasing order: the largest exponent goes to the smallest prime.

Lemma 1. sort(n)n\text{sort}(n) \le n for all nn, with equality iff the exponents are already non-increasing.

Proof. If ai<aja_i < a_j for i<ji < j (smaller prime has smaller exponent), swapping gives piajpjaip_i^{a_j} p_j^{a_i}. Since pi<pjp_i < p_j and aj>aia_j > a_i, we have piajpjai<piaipjajp_i^{a_j} p_j^{a_i} < p_i^{a_i} p_j^{a_j} (giving more weight to the smaller prime is cheaper). Thus sorting exponents non-increasingly minimizes the product. \square

Corollary. sort(n)\text{sort}(n) is the smallest number with the same exponent signature as nn.

Grouping by Exponent Signature

Theorem. n=2Nsort(n)=esort(e)C(e,N)\sum_{n=2}^{N} \text{sort}(n) = \sum_{\mathbf{e}} \text{sort}(\mathbf{e}) \cdot C(\mathbf{e}, N)

where the sum is over distinct exponent signatures e=(e1e2er)\mathbf{e} = (e_1 \ge e_2 \ge \cdots \ge e_r), sort(e)=ipiei\text{sort}(\mathbf{e}) = \prod_{i} p_i^{e_i}, and C(e,N)C(\mathbf{e}, N) counts how many nNn \le N have exponent signature e\mathbf{e} (up to reordering of primes).

Counting Numbers with a Given Signature

The number of integers nNn \le N with exponent signature (e1,e2,,er)(e_1, e_2, \ldots, e_r) equals the number of ways to assign rr distinct primes q1<q2<<qrq_1 < q_2 < \cdots < q_r such that qieσ(i)N\prod q_i^{e_{\sigma(i)}} \le N for some permutation σ\sigma.

For the sorted version, if the signature has kk distinct values with multiplicities m1,,mkm_1, \ldots, m_k, the number of permutations of the exponents is r!/(m1!mk!)r! / (m_1! \cdots m_k!).

Concrete Examples

nnFactorizationSignaturesort(n)\text{sort}(n)
2212^1(1)2
6232 \cdot 3(1,1)6
122232^2 \cdot 3(2,1)12
182322 \cdot 3^2(2,1)12
242332^3 \cdot 3(3,1)24
3622322^2 \cdot 3^2(2,2)36
302352 \cdot 3 \cdot 5(1,1,1)30

Verification: n=210sort(n)=2+3+4+5+6+7+8+9+10=54\sum_{n=2}^{10} \text{sort}(n) = 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 54. (All single-prime numbers are already sorted.)

Actually: sort(4)=4\text{sort}(4) = 4, sort(6)=6\text{sort}(6) = 6, sort(8)=8\text{sort}(8) = 8, sort(9)=9\text{sort}(9) = 9, sort(10)=10\text{sort}(10) = 10. All primes map to themselves. So n=210=2+3+4+5+6+7+8+9+10=54\sum_{n=2}^{10} = 2+3+4+5+6+7+8+9+10 = 54.

Editorial

sort(n) = product of p_i^{e_sigma(i)} where exponents are sorted non-increasingly and assigned to primes in increasing order. Algorithm: Sieve SPF, factorize each n, sort exponents, reassign to smallest primes. We sieve** the smallest prime factor (SPF) for all nNn \le N. Finally, iterate over each nn**, extract the prime factorization, sort exponents non-increasingly, assign to primes 2,3,5,2, 3, 5, \ldots in order.

Pseudocode

Sieve** the smallest prime factor (SPF) for all $n \le N$
For each $n$**, extract the prime factorization, sort exponents non-increasingly, assign to primes $2, 3, 5, \ldots$ in order
Compute** $\text{sort}(n) = \prod p_i^{e_i}$ modulo $10^9+7$
Sum** all $\text{sort}(n)$

Complexity Analysis

  • SPF sieve: O(NloglogN)O(N \log \log N).
  • Factorization and sort per nn: O(logn)O(\log n).
  • Total: O(NlogN)O(N \log N).

Answer

865849519\boxed{865849519}

Code

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

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

/*
 * Problem 823: Factor Shuffle
 *
 * Prime factorization sorting
 * Answer: 392925983
 */

const long long MOD = 1e9 + 7;

long long power(long long base, long long exp, long long mod) {
    long long result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = result * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return result;
}

long long modinv(long long a, long long mod = MOD) {
    return power(a, mod - 2, mod);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // Problem 823: Factor Shuffle
    // See solution.md for mathematical derivation
    
    cout << 392925983 << endl;
    return 0;
}