All Euler problems
Project Euler

Strong Achilles Numbers

A positive integer n is powerful if for every prime p dividing n, we have p^2 | n. An Achilles number is a powerful number that is not a perfect power (i.e., it cannot be expressed as m^k for integ...

Source sync Apr 19, 2026
Problem #0302
Level Level 16
Solved By 896
Languages C++, Python
Answer 1170060
Length 451 words
number_theorysearchlinear_algebra

Problem Statement

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

A positive integer \(n\) is powerful if \(p^2\) is a divisor of \(n\) for every prime factor \(p\) in \(n\).

A positive integer \(n\) is a perfect power if \(n\) can be expressed as a power of another positive integer.

A positive integer \(n\) is an Achilles number if \(n\) if powerful but not a perfect power. For example, \(864\) and \(1800\) are Achilles number: \(864 = 2^5 \cdot 3^3\) and \(1800 = 2^3 \cdot 3^2 \cdot 5^2\).

We shall call a positive integer \(S\) a Strong Achilles number if both \(S\) and \(\phi (S)\) are Achilles numbers.

For example, \(846\) is a Strong Achilles number: \(\phi (864) = 288 = 2^5 \cdot 3^2\). However, \(1800\) isn’t a Strong Achilles number because: \(\phi (1800) = 480 = 2^5 \cdot 3^1 \cdot 5^1\).

There are \(7\) Strong Achilles numbers below \(10^4\) and \(656\) below \(10^8\).

How many Strong Achilles numbers are there below \(10^{18}\)?

Problem 302: Strong Achilles Numbers

Mathematical Development

Definition 1. A positive integer nn is powerful if vp(n)2v_p(n) \ge 2 for every prime pnp \mid n, where vp(n)v_p(n) denotes the pp-adic valuation.

Theorem 1 (Characterization of powerful numbers). A positive integer n>1n > 1 is powerful if and only if every exponent in its canonical factorization n=p1e1pkekn = p_1^{e_1} \cdots p_k^{e_k} satisfies ei2e_i \ge 2. Equivalently, nn can be written as n=a2b3n = a^2 b^3 for some positive integers a,ba, b.

Proof. The first equivalence is immediate from the definition: pinp_i \mid n implies pi2np_i^2 \mid n if and only if vpi(n)=ei2v_{p_i}(n) = e_i \ge 2.

For the second equivalence, suppose every ei2e_i \ge 2. If eie_i is even, write ei=2(ei/2)e_i = 2 \cdot (e_i/2). If eie_i is odd, then ei3e_i \ge 3, so write ei=2ei32+3e_i = 2 \cdot \frac{e_i - 3}{2} + 3. Collecting the contributions from the even parts into a2a^2 and the residual factor of 3 into b3b^3 yields n=a2b3n = a^2 b^3. Conversely, if n=a2b3n = a^2 b^3, then vp(n)=2vp(a)+3vp(b)2v_p(n) = 2 v_p(a) + 3 v_p(b) \ge 2 whenever pnp \mid n. \square

Lemma 1 (Perfect power criterion). A positive integer n=p1e1pkekn = p_1^{e_1} \cdots p_k^{e_k} with k1k \ge 1 is a perfect power if and only if g:=gcd(e1,,ek)2g := \gcd(e_1, \ldots, e_k) \ge 2.

Proof. If g2g \ge 2, then n=(i=1kpiei/g)gn = \bigl(\prod_{i=1}^k p_i^{e_i/g}\bigr)^g, so nn is a gg-th power. Conversely, if n=mdn = m^d with d2d \ge 2, then vpi(n)=dvpi(m)v_{p_i}(n) = d \cdot v_{p_i}(m), whence deid \mid e_i for all ii, giving gd2g \ge d \ge 2. \square

Lemma 2 (Totient of a powerful number). For n=i=1kpiein = \prod_{i=1}^k p_i^{e_i} with all ei1e_i \ge 1, we have φ(n)=i=1kpiei1(pi1)\varphi(n) = \prod_{i=1}^k p_i^{e_i - 1}(p_i - 1).

Proof. This follows from the multiplicativity of φ\varphi and the standard formula φ(pe)=pe1(p1)\varphi(p^e) = p^{e-1}(p - 1). \square

Theorem 2 (Strong Achilles criterion). A positive integer n=p1e1pkekn = p_1^{e_1} \cdots p_k^{e_k} is a Strong Achilles number if and only if all of the following hold:

  1. (Powerful) ei2e_i \ge 2 for all ii.
  2. (Not a perfect power) gcd(e1,,ek)=1\gcd(e_1, \ldots, e_k) = 1.
  3. (φ(n)\varphi(n) is powerful) In the canonical factorization of φ(n)=i=1kpiei1(pi1)\varphi(n) = \prod_{i=1}^k p_i^{e_i - 1}(p_i - 1), every prime appears with exponent 2\ge 2.
  4. (φ(n)\varphi(n) is not a perfect power) The gcd of all exponents in the factorization of φ(n)\varphi(n) equals 11.

Proof. Conditions (1)—(2) are equivalent to nn being Achilles, by Theorem 1 and Lemma 1. Conditions (3)—(4) state that φ(n)\varphi(n) is Achilles. Together, nn is Strong Achilles. \square

Remark. To verify condition (3), one must factor each pi1p_i - 1 and merge exponents with the contributions piei1p_i^{e_i - 1}. For primes pip_i appearing in the factorization of n<1018n < 10^{18} with ei2e_i \ge 2, we have pi109p_i \le 10^9, so factoring pi1p_i - 1 is efficient.

Editorial

A Strong Achilles number n satisfies: (1) n is powerful (all prime exponents >= 2), (2) n is not a perfect power (gcd of exponents == 1), (3) phi(n) is also Achilles. Enumerate powerful numbers via DFS over prime factorizations, checking all four conditions at each node.

Pseudocode

    Set primes <- sieve of primes up to 10^6
    Set count <- 0

        If product has >= 2 prime factors and gcd_exp == 1 then
            If phi_factors is powerful and gcd(phi exponents) == 1 then
                Set count <- count + 1

        for each prime p = primes[idx], primes[idx+1], ... :
            If product * p^2 > N then stop this loop
            for e = 2, 3, ... while product * p^e <= N:
                Set new_gcd <- gcd(gcd_exp, e)
                Set new_phi <- phi_factors merged with factors of p^(e-1)*(p-1)
                dfs(idx + 1, product * p^e, new_gcd, new_phi)

    dfs(0, 1, 0, {})
    Return count

Complexity Analysis

  • Time: The number of powerful numbers below NN is Θ(N)\Theta(\sqrt{N}), approximately cNc\sqrt{N} with c1.94c \approx 1.94 (Golomb, 1970). For N=1018N = 10^{18}, this is 2×109\sim 2 \times 10^9 in the worst case, but the Achilles and Strong Achilles conditions aggressively prune the DFS tree. In practice, the search terminates in manageable time.
  • Space: O(N1/3/lnN)O(N^{1/3} / \ln N) for the prime sieve (primes up to N109\sqrt{N} \approx 10^9, though we need only primes up to 10610^6 since any prime with exponent 3\ge 3 satisfies pN1/3p \le N^{1/3}). Recursion depth is O(logN/log2)=O(60)O(\log N / \log 2) = O(60).

Answer

1170060\boxed{1170060}

Code

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

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

/*
 * Problem 302: Strong Achilles Numbers below 10^18.
 *
 * Enumerate powerful numbers n < 10^18 via DFS over prime factorizations.
 * Check: (1) all exponents >= 2, (2) gcd of exponents = 1 (Achilles),
 * (3)-(4) phi(n) is also Achilles.
 */

typedef long long ll;
typedef __int128 lll;
const ll LIMIT = 1000000000000000000LL;

vector<int> primes;

void gen_primes(int lim) {
    vector<bool> sieve(lim + 1, false);
    for (int i = 2; i <= lim; i++) {
        if (!sieve[i]) {
            primes.push_back(i);
            for (ll j = (ll)i * i; j <= lim; j += i)
                sieve[j] = true;
        }
    }
}

vector<pair<int, int>> factorize(ll n) {
    vector<pair<int, int>> res;
    for (int p : primes) {
        if ((ll)p * p > n) break;
        if (n % p == 0) {
            int e = 0;
            while (n % p == 0) { n /= p; e++; }
            res.push_back({p, e});
        }
    }
    if (n > 1) res.push_back({(int)n, 1});
    return res;
}

unordered_map<int, vector<pair<int,int>>> pm1_cache;

const vector<pair<int,int>>& get_pm1_fac(int p) {
    auto it = pm1_cache.find(p);
    if (it != pm1_cache.end()) return it->second;
    pm1_cache[p] = factorize(p - 1);
    return pm1_cache[p];
}

ll answer = 0;

void dfs(int idx, lll current_n, int num_primes, int gcd_exp,
         unordered_map<int, int>& phi_fac) {
    if (num_primes >= 2 && gcd_exp == 1) {
        bool powerful = true;
        int phi_gcd = 0;
        for (auto& [p, e] : phi_fac) {
            if (e < 2) { powerful = false; break; }
            phi_gcd = __gcd(phi_gcd, e);
        }
        if (powerful && phi_gcd == 1)
            answer++;
    }

    for (int i = idx; i < (int)primes.size(); i++) {
        ll p = primes[i];
        if (current_n * (lll)p * p > LIMIT) break;

        const auto& pm1_fac = get_pm1_fac(p);
        lll power = (lll)p * p;
        for (int e = 2; current_n * power <= LIMIT; e++) {
            int new_gcd = (gcd_exp == 0) ? e : __gcd(gcd_exp, e);
            phi_fac[p] += (e - 1);
            for (auto& [q, f] : pm1_fac) phi_fac[q] += f;

            dfs(i + 1, current_n * power, num_primes + 1, new_gcd, phi_fac);

            for (auto& [q, f] : pm1_fac) {
                phi_fac[q] -= f;
                if (phi_fac[q] == 0) phi_fac.erase(q);
            }
            phi_fac[p] -= (e - 1);
            if (phi_fac[p] == 0) phi_fac.erase(p);

            if (power > (lll)LIMIT / p) break;
            power *= p;
        }
    }
}

int main() {
    gen_primes(1000000);
    unordered_map<int, int> phi_fac;
    dfs(0, 1, 0, 0, phi_fac);
    printf("%lld\n", answer);
    return 0;
}