All Euler problems
Project Euler

Sum of Squares of Unitary Divisors

A unitary divisor d of a number n is a divisor such that gcd(d, n/d) = 1. The unitary divisors of 4! = 24 are 1, 3, 8, 24. The sum of their squares is 1^2 + 3^2 + 8^2 + 24^2 = 650. Let S(n) represe...

Source sync Apr 19, 2026
Problem #0429
Level Level 09
Solved By 2,939
Languages C++, Python
Answer 98792821
Length 243 words
modular_arithmeticnumber_theorybrute_force

Problem Statement

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

A unitary divisor \(d\) of a number \(n\) is a divisor of \(n\) that has the property \(\gcd (d, n/d) = 1\).

The unitary divisors of \(4! = 24\) are \(1, 3, 8\) and \(24\).

The sum of their squares is \(1^2 + 3^2 + 8^2 + 24^2 = 650\).

Let \(S(n)\) represent the sum of the squares of the unitary divisors of \(n\). Thus \(S(4!)=650\).

Find \(S(100\,000\,000!)\) modulo \(1\,000\,000\,009\).

Problem 429: Sum of Squares of Unitary Divisors

Mathematical Analysis

Unitary Divisors and Multiplicative Functions

If n=p1a1p2a2pkakn = p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k}, then a unitary divisor of nn must be of the form d=p1b1p2b2pkbkd = p_1^{b_1} p_2^{b_2} \cdots p_k^{b_k} where each bi{0,ai}b_i \in \{0, a_i\} (either take the full prime power or nothing, to ensure gcd(d,n/d)=1\gcd(d, n/d) = 1).

The sum of squares of unitary divisors is therefore:

σ2(n)=i=1k(1+pi2ai)\sigma_2^*(n) = \prod_{i=1}^{k} (1 + p_i^{2a_i})

Applying Legendre’s Formula

To find the prime factorization of N!=(108)!N! = (10^8)!, we use Legendre’s formula for the exponent of prime pp in N!N!:

vp(N!)=i=1Npiv_p(N!) = \sum_{i=1}^{\infty} \left\lfloor \frac{N}{p^i} \right\rfloor

Editorial

Key insight: If n = p1^a1 * p2^a2 * … then sigma_2(n) = prod(1 + p_i^(2a_i)) Use Legendre’s formula for exponents in factorial, and sieve for primes. We generate all primes up to N=108N = 10^8 using a sieve of Eratosthenes. We then iterate over each prime pp, compute ap=vp(N!)a_p = v_p(N!) using Legendre’s formula. Finally, compute the product p(1+p2ap)(mod109+9)\prod_p (1 + p^{2a_p}) \pmod{10^9 + 9} using modular exponentiation.

Pseudocode

Generate all primes up to $N = 10^8$ using a sieve of Eratosthenes
For each prime $p$, compute $a_p = v_p(N!)$ using Legendre's formula
Compute the product $\prod_p (1 + p^{2a_p}) \pmod{10^9 + 9}$ using modular exponentiation

Correctness

Theorem. The method described above computes exactly the quantity requested in the problem statement.

Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer. \square

Complexity Analysis

  • Time: O(N)O(N) for the sieve, plus O(π(N)logN)O(\pi(N) \cdot \log N) for the Legendre computations and modular exponentiations, where π(N)N/lnN\pi(N) \approx N/\ln N.
  • Space: O(N)O(N) for the prime sieve.

Answer

98792821\boxed{98792821}

Code

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

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

const long long MOD = 1000000009;
const int N = 100000000;

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() {
    // Sieve of Eratosthenes
    vector<bool> is_prime(N + 1, true);
    is_prime[0] = is_prime[1] = false;
    for (long long i = 2; i * i <= N; i++) {
        if (is_prime[i]) {
            for (long long j = i * i; j <= N; j += i)
                is_prime[j] = false;
        }
    }

    long long result = 1;
    for (long long p = 2; p <= N; p++) {
        if (!is_prime[p]) continue;
        // Legendre's formula: exponent of p in N!
        long long a = 0;
        long long pk = p;
        while (pk <= N) {
            a += N / pk;
            if (pk > N / p) break; // prevent overflow
            pk *= p;
        }
        // Multiply by (1 + p^(2a)) mod MOD
        result = result % MOD * ((1 + power(p, 2 * a, MOD)) % MOD) % MOD;
    }

    cout << result << endl;
    return 0;
}