All Euler problems
Project Euler

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.

Source sync Apr 19, 2026
Problem #0342
Level Level 16
Solved By 938
Languages C++, Python
Answer 5943040885644
Length 354 words
number_theorymodular_arithmeticsearch

Problem Statement

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

Consider the number .
, so . 1
So is a square and is a cube.

Find the sum of all numbers , such that is a cube.

1 denotes Euler's totient function.

Problem 342: The Totient of a Square Is a Cube

Mathematical Foundation

Theorem 1 (Totient of a square). Let n=i=1kpiain = \prod_{i=1}^{k} p_i^{a_i} be the prime factorization of nn, with distinct primes pip_i and exponents ai1a_i \ge 1. Then

φ(n2)=i=1kpi2ai1(pi1).\varphi(n^2) = \prod_{i=1}^{k} p_i^{2a_i - 1}(p_i - 1).

Proof. By the multiplicativity of φ\varphi and the standard formula φ(pm)=pm1(p1)\varphi(p^m) = p^{m-1}(p-1):

φ(n2)=i=1kφ(pi2ai)=i=1kpi2ai1(pi1).\varphi(n^2) = \prod_{i=1}^{k} \varphi(p_i^{2a_i}) = \prod_{i=1}^{k} p_i^{2a_i - 1}(p_i - 1). \quad \square

Theorem 2 (Cube criterion via qq-adic valuations). The integer φ(n2)\varphi(n^2) is a perfect cube if and only if

vq ⁣(φ(n2))0(mod3)for every prime q,v_q\!\bigl(\varphi(n^2)\bigr) \equiv 0 \pmod{3} \quad \text{for every prime } q,

where vq(m)v_q(m) denotes the qq-adic valuation of mm.

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. \square

Proposition 1 (Decomposition of vqv_q). For each prime qq,

vq ⁣(φ(n2))=i=1pi=qk(2ai1)  +  i=1kvq(pi1).v_q\!\bigl(\varphi(n^2)\bigr) = \sum_{\substack{i=1 \\ p_i = q}}^{k} (2a_i - 1) \;+\; \sum_{i=1}^{k} v_q(p_i - 1).

The first sum contributes only when qq is itself a prime factor of nn (giving at most one term since the pip_i are distinct). The second sum aggregates the contribution of (pi1)(p_i - 1) across all prime factors of nn.

Proof. Direct from the factorization φ(n2)=ipi2ai1(pi1)\varphi(n^2) = \prod_i p_i^{2a_i-1}(p_i-1). The qq-adic valuation of a product is the sum of the valuations. \square

Lemma 1 (Self-exponent constraint). If q=piq = p_i divides nn with exponent aia_i, the self-contribution to vq(φ(n2))v_q(\varphi(n^2)) is 2ai12a_i - 1. This is divisible by 3 if and only if ai2(mod3)a_i \equiv 2 \pmod{3}, i.e., ai{2,5,8,}a_i \in \{2, 5, 8, \ldots\}. Deviations are permissible only if cross-contributions from vq(pj1)v_q(p_j - 1) for other primes pjnp_j \mid n compensate modulo 3.

Proof. We require (2ai1)+jivpi(pj1)0(mod3)(2a_i - 1) + \sum_{j \ne i} v_{p_i}(p_j - 1) \equiv 0 \pmod{3}. When no other prime pjp_j satisfies pi(pj1)p_i \mid (p_j - 1), the condition reduces to 2ai10(mod3)2a_i - 1 \equiv 0 \pmod{3}, i.e., ai2(mod3)a_i \equiv 2 \pmod{3} (since 212(mod3)2^{-1} \equiv 2 \pmod{3}). \square

Lemma 2 (Search bound). Any n<1010n < 10^{10} has at most log2(1010)=33\lfloor \log_2(10^{10}) \rfloor = 33 prime factors (counted with multiplicity) and at most 10 distinct prime factors. For the DFS enumeration, if pp is the smallest untried prime and ncurrentp1010n_{\text{current}} \cdot p \ge 10^{10}, all subsequent primes can be pruned.

Proof. Since 233<1010<2342^{33} < 10^{10} < 2^{34}, the total multiplicity is at most 33. The product of the first 10 primes is 23529=6469693230<10102 \cdot 3 \cdot 5 \cdots 29 = 6469693230 < 10^{10}, while the product of the first 11 primes exceeds 101010^{10}. \square

Editorial

The algorithm performs a depth-first search over prime factorizations of nn, enumerating primes in increasing order and tracking the residue modulo 3 of vq(φ(n2))v_q(\varphi(n^2)) for each relevant prime qq.

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 n<1010n < 10^{10} and the residue constraints. In practice, O(105)O(10^5) nodes are visited. Per node, factoring (p1)(p-1) costs O(p)O(\sqrt{p}).
  • Space: O(π(N))O(\pi(\sqrt{N})) for the prime list, plus O(k)O(k) for the recursion stack (k10k \le 10).

Answer

5943040885644\boxed{5943040885644}

Code

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

C++ project_euler/problem_342/solution.cpp
#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;
}