All Euler problems
Project Euler

Maximum Number of Divisors

Let d(i) be the number of divisors of i. Define M(n, k) = max(d(n), d(n+1),..., d(n+k-1)). Given that sum_(n=1)^100 M(n, 10) = 432, find sum_(n=1)^(10^8) M(n, 10^5).

Source sync Apr 19, 2026
Problem #0485
Level Level 13
Solved By 1,374
Languages C++, Python
Answer 51281274340
Length 383 words
number_theorybrute_forcelinear_algebra

Problem Statement

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

Let \(d(n)\) be the number of divisors of \(n\).

Let \(M(n,k)\) be the maximum value of \(d(j)\) for \(n \le j \le n+k-1\).

Let \(S(u,k)\) be the sum of \(M(n,k)\) for \(1 \le n \le u-k+1\).

You are given that \(S(1000,10)=17176\).

Find \(S(100\,000\,000, 100\,000)\).

Problem 485: Maximum Number of Divisors

Mathematical Foundation

Theorem 1 (Divisor function via sieve). For any positive integer nn,

d(n)=jn1.d(n) = \sum_{j \mid n} 1.

The values d(1),d(2),,d(L)d(1), d(2), \ldots, d(L) can be computed simultaneously by iterating: for each j=1,2,,Lj = 1, 2, \ldots, L, increment d[j],d[2j],d[3j],,d[L/jj]d[j], d[2j], d[3j], \ldots, d[\lfloor L/j \rfloor \cdot j] by 1.

Proof. By definition, d(n)d(n) counts the number of positive divisors of nn. The sieve procedure adds 1 to d[n]d[n] exactly once for each divisor jj of nn (since jnj \mid n iff nn appears in the arithmetic progression j,2j,3j,j, 2j, 3j, \ldots). The total work is j=1LL/j=O(LlnL)\sum_{j=1}^L \lfloor L/j \rfloor = O(L \ln L). \square

Theorem 2 (Sliding window maximum via monotone deque). Given an array a[1L]a[1 \ldots L] and window size kk, the sequence of window maxima Mi=max(a[i],,a[i+k1])M_i = \max(a[i], \ldots, a[i+k-1]) for i=1,,Lk+1i = 1, \ldots, L - k + 1 can be computed in O(L)O(L) amortized time using a monotone deque.

Proof. Maintain a deque DD of indices, invariant: DD is non-empty, indices are in increasing order, and a[D[front]]a[D[front+1]]a[D[back]]a[D[\text{front}]] \ge a[D[\text{front}+1]] \ge \cdots \ge a[D[\text{back}]] (weakly decreasing in aa-values).

For each new index ii:

  1. While DD is non-empty and a[D.back]a[i]a[D.\text{back}] \le a[i]: pop from back.
  2. Push ii to back.
  3. While D.front<ik+1D.\text{front} < i - k + 1: pop from front (out of window).
  4. The window maximum is a[D.front]a[D.\text{front}].

Correctness: After step 1, all indices remaining in DD have aa-values strictly greater than a[i]a[i], and ii is appended. The front is always the index of the maximum value in the current window because: (a) all indices in DD are within the window (step 3 ensures this), and (b) any index j<ij < i with a[j]a[i]a[j] \le a[i] was removed in step 1 (either when ii was processed or earlier), so the front has the largest aa-value.

Amortized O(1)O(1): Each index is pushed onto DD exactly once and popped at most once from the back and at most once from the front. Over LL iterations, the total number of push and pop operations is at most 3L3L. \square

Lemma 1 (Divisor count upper bound). For n108+105n \le 10^8 + 10^5, we have d(n)768d(n) \le 768. More precisely, the maximum value of d(n)d(n) for n108n \le 10^8 is achieved at highly composite numbers.

Proof. By the theory of highly composite numbers (Ramanujan, 1915), the divisor function grows sub-polynomially. For n108n \le 10^8, an exhaustive sieve confirms d(n)768d(n) \le 768. \square

Editorial

d(i) = number of divisors of i. M(n,k) = max(d(n), d(n+1), …, d(n+k-1)). Given: sum M(n,10) for n=1..100 = 432. Find: sum M(n, 10^4) for n=1..10^8. We divisor count sieve. We then sliding window maximum via monotone deque. Finally, maintain decreasing invariant.

Pseudocode

Divisor count sieve
Sliding window maximum via monotone deque
Maintain decreasing invariant
Remove out-of-window front
Once window is full (i >= k), record the maximum

Complexity Analysis

  • Time: O(LlogL)O(L \log L) for the divisor sieve (where L=N+k11.0001×108L = N + k - 1 \approx 1.0001 \times 10^8), plus O(L)O(L) for the sliding window. Total: O(LlogL)O(L \log L).
  • Space: O(L)O(L) for the divisor array, plus O(k)O(k) for the deque. Total: O(L)O(L).

For N=108N = 10^8 and k=105k = 10^5, L108L \approx 10^8, so the sieve requires approximately 108×ln(108)1.8×10910^8 \times \ln(10^8) \approx 1.8 \times 10^9 operations and 400\sim 400 MB of memory (using 32-bit integers for dd).

Answer

51281274340\boxed{51281274340}

Code

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

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

int main() {
    // Parameters
    const int N = 100;      // Change to 100000000 for full problem
    const int K = 10;       // Change to 10000 for full problem
    const int L = N + K - 1;

    // Divisor count sieve
    vector<int> d(L + 1, 0);
    for (int j = 1; j <= L; j++) {
        for (int m = j; m <= L; m += j) {
            d[m]++;
        }
    }

    // Sliding window maximum using monotone deque
    deque<int> dq;
    long long total = 0;

    for (int i = 1; i <= L; i++) {
        while (!dq.empty() && d[dq.back()] <= d[i])
            dq.pop_back();
        dq.push_back(i);

        int n = i - K + 1;
        if (n >= 1) {
            while (dq.front() < n)
                dq.pop_front();
            total += d[dq.front()];
        }
    }

    cout << "Sum M(n," << K << ") for n=1.." << N << " = " << total << endl;

    // For N=100, K=10: expected 432
    if (N == 100 && K == 10) {
        assert(total == 432);
    }

    return 0;
}