Cubic Permutations
The cube 41063625 = 345^3 can be obtained by permuting the digits of 56623104 = 384^3. Find the smallest cube for which exactly five permutations of its digits are also cubes.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
The cube, \(41063625\) (\(345^3\)), can be permuted to produce two other cubes: \(56623104\) (\(384^3\)) and \(66430125\) (\(405^3\)). In fact, \(41063625\) is the smallest cube which has exactly three permutations of its digits which are also cube.
Find the smallest cube for which exactly five permutations of its digits are cube.
Problem 62: Cubic Permutations
Mathematical Foundation
Definition 1. For a positive integer , define the canonical digit signature as the string obtained by sorting the decimal digits of in non-decreasing order. For example, .
Theorem 1 (Permutation equivalence). Two positive integers and are digit permutations of each other if and only if .
Proof. () If is a digit permutation of , there exists a bijection on digit positions such that the -th digit of equals the -th digit of . Hence and share the same multiset of digits, and sorting both multisets yields the same string.
() If , the multisets of digits coincide. Since any two sequences with the same multiset are permutations of each other, is a digit permutation of .
Lemma 1 (Digit count preservation). If and are digit permutations of each other (with ), then and have the same number of digits.
Proof. Two numbers sharing the same digit multiset necessarily have the same total digit count, since neither has a leading zero (both being positive cubes ).
Theorem 2 (Digit-count finality). Let denote the number of decimal digits of . For a fixed digit count , define the equivalence classes
where . Then no cube with a different digit count can belong to any class in . Consequently, once all cubes with digits have been enumerated, the class sizes within are final.
Proof. By Lemma 1, any cube that is a digit permutation of a -digit cube must itself have digits. Therefore, -digit cubes with cannot enter any class in . When all with have been processed, no future cube can modify the classes.
Lemma 2 (Range of -digit cubes). The cubes with exactly digits are where . In particular, .
Proof. The condition is equivalent to . The interval length is .
Corollary. The algorithm can process cubes in order of and finalize all classes of digit count as soon as the first -digit cube is encountered.
Editorial
We generate cubes in increasing order and group them by their canonical digit signatures, but only within a fixed digit length. This digit-length partition is important because once the number of digits increases, no later cube can belong to any group from the previous layer. At each such transition we inspect the completed groups from the previous digit length, and if one of them contains exactly five cubes, the smallest cube in that group is the answer. Otherwise we discard the old groups and continue with the next digit length.
Pseudocode
Initialize the grouping structure for the current digit length.
Start with no active digit length.
For each positive integer n:
compute the cube c = n^3
determine the number of digits of c
if this is the first cube of a new digit length:
inspect every group from the previous digit length
if one of them contains exactly five cubes, return its smallest element
reset the grouping structure for the new digit length
place c into the group indexed by its sorted digit signature
Complexity Analysis
Time: Let be the value of at termination. For each , we compute in (or for arbitrary-precision integers), sort its digits in , and perform a hash-map lookup in expected time. The answer occurs at with digits, giving total cost .
Space: for the groups hash map within the current digit count, since completed digit counts are discarded.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
map<string, vector<long long>> groups;
int prev_digits = 0;
long long answer = 0;
for (long long n = 1; ; n++) {
long long cube = n * n * n;
string s = to_string(cube);
int d = s.size();
if (d > prev_digits && prev_digits > 0) {
// Finalize all groups of the previous digit count
for (auto& [key, vec] : groups) {
if ((int)key.size() == prev_digits && vec.size() == 5) {
if (answer == 0 || vec[0] < answer)
answer = vec[0];
}
}
if (answer) {
cout << answer << endl;
return 0;
}
prev_digits = d;
}
if (prev_digits == 0) prev_digits = d;
string key = s;
sort(key.begin(), key.end());
groups[key].push_back(cube);
}
}
"""
Problem 62: Cubic Permutations
Find the smallest cube for which exactly five permutations of its digits are also cubes.
"""
from collections import defaultdict
def solve():
groups = defaultdict(list)
prev_digits = 0
for n in range(1, 100000):
cube = n ** 3
s = str(cube)
d = len(s)
if d > prev_digits and prev_digits > 0:
# Finalize all groups with prev_digits digits
candidates = []
for key, cubes in groups.items():
if len(key) == prev_digits and len(cubes) == 5:
candidates.append(min(cubes))
if candidates:
return min(candidates)
prev_digits = d
if prev_digits == 0:
prev_digits = d
key = ''.join(sorted(s))
groups[key].append(cube)
return None
answer = solve()
assert answer == 127035954683
print(answer)