All Euler problems
Project Euler

Sum of Squares II

For a positive integer n, define g(n) to be the maximum perfect square that divides n. For example, g(18) = 9, g(19) = 1. Define S(N) = sum_(n=1)^N g(n). Given: S(10) = 24, S(100) = 767. Find S(10^...

Source sync Apr 19, 2026
Problem #0745
Level Level 12
Solved By 1,519
Languages C++, Python
Answer 94586478
Length 279 words
modular_arithmeticnumber_theorybrute_force

Problem Statement

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

For a positive integer, \(n\), define \(g(n)\) to be the maximum perfect square that divides \(n\).

For example, \(g(18) = 9\), \(g(19) = 1\).

Also define \[\displaystyle S(N) = \sum _{n=1}^N g(n)\] For example, \(S(10) = 24\) and \(S(100) = 767\).

Find \(S(10^{14})\). Give your answer modulo \(1\,000\,000\,007\).

Problem 745: Sum of Squares II

Mathematical Foundation

Theorem 1 (Unique Squarefree Decomposition). Every positive integer nn can be written uniquely as n=a2bn = a^2 b where bb is squarefree. In this decomposition, g(n)=a2g(n) = a^2.

Proof. Write n=piein = \prod p_i^{e_i}. Set a=piei/2a = \prod p_i^{\lfloor e_i / 2 \rfloor} and b=pieimod2b = \prod p_i^{e_i \bmod 2}. Then n=a2bn = a^2 b, bb is squarefree, and a2a^2 is the largest perfect square dividing nn. Uniqueness follows from the uniqueness of prime factorization. \square

Theorem 2 (Jordan Totient Reformulation). Define the Jordan totient function of order 22:

J2(e)=aea2μ(e/a)=e2pe(11p2)J_2(e) = \sum_{a \mid e} a^2 \mu(e/a) = e^2 \prod_{p \mid e} \left(1 - \frac{1}{p^2}\right)

Then:

S(N)=e=1NJ2(e)Ne2S(N) = \sum_{e=1}^{\lfloor \sqrt{N} \rfloor} J_2(e) \left\lfloor \frac{N}{e^2} \right\rfloor

Proof. From Theorem 1:

S(N)=a=1Na2bN/a2b squarefree1=a=1Na2d=1N/a2μ(d)Na2d2S(N) = \sum_{a=1}^{\lfloor \sqrt{N} \rfloor} a^2 \sum_{\substack{b \le N/a^2 \\ b \text{ squarefree}}} 1 = \sum_{a=1}^{\lfloor \sqrt{N} \rfloor} a^2 \sum_{d=1}^{\lfloor \sqrt{N/a^2} \rfloor} \mu(d) \left\lfloor \frac{N}{a^2 d^2} \right\rfloor

where we used the standard Mobius inversion formula Q(m)=d=1mμ(d)m/d2Q(m) = \sum_{d=1}^{\lfloor \sqrt{m} \rfloor} \mu(d) \lfloor m/d^2 \rfloor for the squarefree counting function. Substituting e=ade = ad:

S(N)=e=1NNe2aea2μ(e/a)=e=1NJ2(e)Ne2S(N) = \sum_{e=1}^{\lfloor \sqrt{N} \rfloor} \left\lfloor \frac{N}{e^2} \right\rfloor \sum_{a \mid e} a^2 \mu(e/a) = \sum_{e=1}^{\lfloor \sqrt{N} \rfloor} J_2(e) \left\lfloor \frac{N}{e^2} \right\rfloor

The identity aea2μ(e/a)=J2(e)\sum_{a \mid e} a^2 \mu(e/a) = J_2(e) is the definition of the Jordan totient as the Dirichlet convolution id2μ\mathrm{id}_2 * \mu. The multiplicative formula J2(e)=e2pe(11/p2)J_2(e) = e^2 \prod_{p \mid e}(1 - 1/p^2) follows from multiplicativity and evaluation at prime powers: J2(pk)=p2kp2(k1)J_2(p^k) = p^{2k} - p^{2(k-1)}. \square

Lemma 1 (Sieve for J2J_2). The values J2(e)J_2(e) for 1eL1 \le e \le L can be computed by a multiplicative sieve analogous to the Euler totient sieve:

  1. Initialize J2(e)=e2J_2(e) = e^2 for all ee.
  2. For each prime pLp \le L: for each multiple mm of pp, set J2(m)J2(m)(p21)/p2J_2(m) \leftarrow J_2(m) \cdot (p^2 - 1) / p^2.

This runs in O(LloglogL)O(L \log \log L) time.

Proof. The sieve correctly computes J2(e)=e2pe(11/p2)J_2(e) = e^2 \prod_{p \mid e}(1 - 1/p^2) by multiplying in the factor (11/p2)=(p21)/p2(1 - 1/p^2) = (p^2-1)/p^2 for each prime divisor. The time complexity is the same as the Euler sieve. The division by p2p^2 is exact in modular arithmetic since we track the factor (p21)/p2(p^2-1)/p^2 as a modular inverse multiplication. \square

Editorial

For a positive integer n, g(n) = max perfect square dividing n. S(N) = sum of g(n) for n=1..N. Find S(10^14) mod 10^9+7. Uses Jordan totient function J_2 sieve approach. We sieve: for each prime, multiply in (1 - 1/p^2) factor. Finally, compute S(N) = sum_{e=1}^{L} J2[e] * floor(N / e^2) mod p.

Pseudocode

Sieve J_2(e) mod p for e = 1..L
Sieve: for each prime, multiply in (1 - 1/p^2) factor
Compute S(N) = sum_{e=1}^{L} J2[e] * floor(N / e^2) mod p

Complexity Analysis

  • Time: O(NloglogN)O(\sqrt{N} \log \log \sqrt{N}) for the sieve, O(N)O(\sqrt{N}) for the final summation. Total: O(NloglogN)O(\sqrt{N} \log \log \sqrt{N}). For N=1014N = 10^{14}, N=107\sqrt{N} = 10^7.
  • Space: O(N)O(\sqrt{N}) for the J2J_2 array and the prime sieve.

Answer

94586478\boxed{94586478}

Code

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

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

const long long MOD = 1000000007;
const long long N = 100000000000000LL; // 10^14
const int L = 10000000; // sqrt(N) = 10^7

long long j2[L + 1]; // Jordan totient J_2
bool is_composite[L + 1];
vector<int> primes;

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;
}

int main() {
    // Initialize J_2(e) = e^2 mod MOD
    for (int i = 1; i <= L; i++) {
        j2[i] = (1LL * i % MOD) * (i % MOD) % MOD;
    }

    // Sieve: for each prime p, multiply J_2(m) by (1 - 1/p^2) = (p^2-1)/p^2
    // We need modular inverse of p^2
    for (int p = 2; p <= L; p++) {
        if (!is_composite[p]) {
            primes.push_back(p);
            long long p2 = (1LL * p * p) % MOD;
            long long factor = (p2 - 1 + MOD) % MOD * power(p2, MOD - 2, MOD) % MOD;
            for (long long m = p; m <= L; m += p) {
                is_composite[m] = (m != p);
                j2[m] = j2[m] * factor % MOD;
            }
        } else {
            // Mark composites for sieve
        }
    }

    // Actually we need a proper sieve. Let me redo using smallest prime factor.
    // Reset and use proper Euler sieve approach
    // The above loop already handles it: for each prime p, we iterate through
    // all multiples and multiply by (p^2-1)/p^2. This correctly computes J_2
    // since J_2 is multiplicative and J_2(n) = n^2 * prod_{p|n} (1 - 1/p^2).

    // Compute S(N) = sum_{e=1}^{L} J_2(e) * floor(N/e^2) mod MOD
    long long ans = 0;
    for (int e = 1; e <= L; e++) {
        long long q = (N / ((long long)e * e)) % MOD;
        ans = (ans + j2[e] * q) % MOD;
    }

    cout << ans << endl;
    return 0;
}