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^...
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 can be written uniquely as where is squarefree. In this decomposition, .
Proof. Write . Set and . Then , is squarefree, and is the largest perfect square dividing . Uniqueness follows from the uniqueness of prime factorization.
Theorem 2 (Jordan Totient Reformulation). Define the Jordan totient function of order :
Then:
Proof. From Theorem 1:
where we used the standard Mobius inversion formula for the squarefree counting function. Substituting :
The identity is the definition of the Jordan totient as the Dirichlet convolution . The multiplicative formula follows from multiplicativity and evaluation at prime powers: .
Lemma 1 (Sieve for ). The values for can be computed by a multiplicative sieve analogous to the Euler totient sieve:
- Initialize for all .
- For each prime : for each multiple of , set .
This runs in time.
Proof. The sieve correctly computes by multiplying in the factor for each prime divisor. The time complexity is the same as the Euler sieve. The division by is exact in modular arithmetic since we track the factor as a modular inverse multiplication.
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: for the sieve, for the final summation. Total: . For , .
- Space: for the array and the prime sieve.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Project Euler Problem 745: Sum of Squares II
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.
"""
def solve():
MOD = 1000000007
N = 10**14
L = int(N**0.5) # 10^7
# Sieve J_2 values
# J_2(n) = n^2 * prod_{p|n} (1 - 1/p^2)
# We work modulo MOD
j2 = [0] * (L + 1)
for i in range(1, L + 1):
j2[i] = (i * i) % MOD
# Sieve of Eratosthenes style
is_prime = bytearray(b'\x01') * (L + 1)
is_prime[0] = is_prime[1] = 0
for p in range(2, L + 1):
if is_prime[p]:
p2 = (p * p) % MOD
inv_p2 = pow(p2, MOD - 2, MOD)
factor = (p2 - 1) * inv_p2 % MOD
for m in range(p, L + 1, p):
if m != p:
is_prime[m] = 0
j2[m] = j2[m] * factor % MOD
# Compute S(N)
ans = 0
for e in range(1, L + 1):
q = (N // (e * e)) % MOD
ans = (ans + j2[e] * q) % MOD
print(ans)
if __name__ == "__main__":
solve()