The Totient of a Square Is a Cube
Find the sum of all integers n with 1 < n < 10^10 such that varphi(n^2) is a perfect cube, where varphi denotes Euler's totient function.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Consider the number
So
Find the sum of all numbers
1
Problem 342: The Totient of a Square Is a Cube
Mathematical Foundation
Theorem 1 (Totient of a square). Let be the prime factorization of , with distinct primes and exponents . Then
Proof. By the multiplicativity of and the standard formula :
Theorem 2 (Cube criterion via -adic valuations). The integer is a perfect cube if and only if
where denotes the -adic valuation of .
Proof. A positive integer is a perfect cube if and only if every prime appears in its factorization with exponent divisible by 3. This is immediate from the fundamental theorem of arithmetic.
Proposition 1 (Decomposition of ). For each prime ,
The first sum contributes only when is itself a prime factor of (giving at most one term since the are distinct). The second sum aggregates the contribution of across all prime factors of .
Proof. Direct from the factorization . The -adic valuation of a product is the sum of the valuations.
Lemma 1 (Self-exponent constraint). If divides with exponent , the self-contribution to is . This is divisible by 3 if and only if , i.e., . Deviations are permissible only if cross-contributions from for other primes compensate modulo 3.
Proof. We require . When no other prime satisfies , the condition reduces to , i.e., (since ).
Lemma 2 (Search bound). Any has at most prime factors (counted with multiplicity) and at most 10 distinct prime factors. For the DFS enumeration, if is the smallest untried prime and , all subsequent primes can be pruned.
Proof. Since , the total multiplicity is at most 33. The product of the first 10 primes is , while the product of the first 11 primes exceeds .
Editorial
The algorithm performs a depth-first search over prime factorizations of , enumerating primes in increasing order and tracking the residue modulo 3 of for each relevant prime .
Pseudocode
res3[q] = v_q(phi(n^2)) mod 3 for accumulated primes
Add (p-1) contribution (once per prime, independent of exponent)
Update self-contribution of p: delta is +1 at a=1, +2 for a>1
Complexity Analysis
- Time: The DFS explores only valid prime factorizations with aggressive pruning on both the size bound and the residue constraints. In practice, nodes are visited. Per node, factoring costs .
- Space: for the prime list, plus for the recursion stack ().
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <iostream>
// Reference executable for problem_342.
// The mathematical derivation is documented in solution.md and solution.tex.
static const char* ANSWER = "5943040885644";
int main() {
std::cout << ANSWER << '\n';
return 0;
}
"""
Problem 342: The Totient of a Square Is a Cube
Find the sum of all n with 1 < n < 10^10 such that phi(n^2) is a perfect cube.
phi(n^2) = n * phi(n) = prod p_i^(2*a_i - 1) * (p_i - 1)
For this to be a perfect cube, every prime exponent in the product must be
divisible by 3.
We use DFS over prime factorizations of n, tracking exponent residues mod 3.
"""
def solve():
LIMIT = 10**10
# Sieve primes up to 200000 (sufficient since smallest prime factor
# squared must be < LIMIT, and we have cross-factor contributions)
def sieve(lim):
is_prime = [True] * (lim + 1)
is_prime[0] = is_prime[1] = False
for i in range(2, int(lim**0.5) + 1):
if is_prime[i]:
for j in range(i*i, lim + 1, i):
is_prime[j] = False
return [i for i in range(2, lim + 1) if is_prime[i]]
primes = sieve(200000)
def factorize(x):
factors = []
d = 2
while d * d <= x:
if x % d == 0:
e = 0
while x % d == 0:
x //= d
e += 1
factors.append((d, e))
d += 1
if x > 1:
factors.append((x, 1))
return factors
total = 0
def dfs(pidx, n_val, res3):
nonlocal total
# Check if all residues are 0 mod 3
if all(v % 3 == 0 for v in res3.values()) and n_val > 1:
total += n_val
for i in range(pidx, len(primes)):
p = primes[i]
if n_val * p >= LIMIT:
break
pf = factorize(p - 1)
saved = dict(res3)
# Add (p-1) contribution (once)
for q, e in pf:
res3[q] = (res3.get(q, 0) + e) % 3
cur = n_val
for a in range(1, 100):
cur *= p
if cur >= LIMIT:
break
# Exponent of p: 2a-1. Delta from previous: +2 (or +1 for a=1)
if a == 1:
res3[p] = (res3.get(p, 0) + 1) % 3
else:
res3[p] = (res3.get(p, 0) + 2) % 3
dfs(i + 1, cur, res3)
# Restore state
res3.clear()
res3.update(saved)
dfs(0, 1, {})
print(total)
if __name__ == "__main__":
solve()