All Euler problems
Project Euler

Maximum Product of Parts

For a positive integer N, partition N into k equal real-valued parts of size N/k, yielding product (N/k)^k. Let M(N) be the maximum of (N/k)^k over all positive integers k. Define D(N) = -N & if M(...

Source sync Apr 19, 2026
Problem #0183
Level Level 06
Solved By 5,305
Languages C++, Python
Answer 48861552
Length 310 words
number_theorymodular_arithmeticgeometry

Problem Statement

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

Let \(N\) be a positive integer and let \(N\) be split into \(k\) equal parts, \(r = N/k\), so that \(N = r + r + \cdots + r\).

Let \(P\) be the product of these parts, \(P = r \times r \times \cdots \times r = r^k\).

For example, if \(11\) is split into five equal parts, \(11 = 2.2 + 2.2 + 2.2 + 2.2 + 2.2\), then \(P = 2.2^5 = 51.53632\).

Let \(M(N) = P_{\mathrm {max}}\) for a given value of \(N\).

It turns out that the maximum for \(N = 11\) is found by splitting eleven into four equal parts which leads to \(P_{\mathrm {max}} = (11/4)^4\); that is, \(M(11) = 14641/256 = 57.19140625\), which is a terminating decimal.

However, for \(N = 8\) the maximum is achieved by splitting it into three equal parts, so \(M(8) = 512/27\), which is a non-terminating decimal.

Let \(D(N) = N\) if \(M(N)\) is a non-terminating decimal and \(D(N) = -N\) if \(M(N)\) is a terminating decimal.

For example, \(\displaystyle \sum \limits _{N = 5}^{100} D(N)\) is \(2438\).

Find \(\displaystyle \sum \limits _{N = 5}^{10000} D(N)\).

Problem 183: Maximum Product of Parts

Mathematical Development

Theorem 1 (Continuous Optimum). Define g(t)=tln(N/t)g(t) = t \ln(N/t) for t>0t > 0. Then gg has a unique global maximum at t=N/et^* = N/e.

Proof. Differentiating: g(t)=ln(N/t)1g'(t) = \ln(N/t) - 1. Setting g(t)=0g'(t) = 0 yields ln(N/t)=1\ln(N/t) = 1, hence t=N/et = N/e. The second derivative g(t)=1/t<0g''(t) = -1/t < 0 for all t>0t > 0, confirming strict concavity. A strictly concave function on (0,)(0, \infty) has at most one critical point, which must be the global maximum. \square

Corollary 1 (Discrete Optimum). The optimal integer kk^* satisfying maxkZ>0(N/k)k\max_{k \in \mathbb{Z}_{>0}} (N/k)^k is either N/e\lfloor N/e \rfloor or N/e\lceil N/e \rceil.

Proof. Since ln((N/k)k)=kln(N/k)=g(k)\ln\bigl((N/k)^k\bigr) = k \ln(N/k) = g(k) and gg is strictly concave, the restriction of gg to positive integers attains its maximum at one of the two integers adjacent to the continuous maximizer N/eN/e. \square

Theorem 2 (Terminating Decimal Criterion). A rational number a/ba/b in lowest terms (with gcd(a,b)=1\gcd(a,b) = 1 and b>0b > 0) has a terminating decimal expansion if and only if bb has no prime factors other than 22 and 55.

Proof. The fraction a/ba/b terminates if and only if there exists nZ0n \in \mathbb{Z}_{\geq 0} such that a10n/bZa \cdot 10^n / b \in \mathbb{Z}, equivalently b10nb \mid 10^n. Since 10n=2n5n10^n = 2^n \cdot 5^n, this holds if and only if every prime divisor of bb lies in {2,5}\{2, 5\}. \square

Theorem 3 (Termination of (N/k)k(N/k)^k). Let d=gcd(N,k)d = \gcd(N, k). Then (N/k)k(N/k)^k is a terminating decimal if and only if k/dk/d has no prime factors other than 22 and 55.

Proof. Write N/k=(N/d)/(k/d)N/k = (N/d)/(k/d) where gcd(N/d,k/d)=1\gcd(N/d, k/d) = 1, so N/kN/k is in lowest terms with denominator k/dk/d. By Theorem 2, N/kN/k terminates if and only if k/d=2α5βk/d = 2^\alpha 5^\beta for some α,β0\alpha, \beta \geq 0.

It remains to show that (N/k)k(N/k)^k terminates if and only if N/kN/k terminates. Write N/k=a/bN/k = a/b in lowest terms. Then (N/k)k=ak/bk(N/k)^k = a^k / b^k with gcd(ak,bk)=1\gcd(a^k, b^k) = 1 (since gcd(a,b)=1\gcd(a, b) = 1 implies gcd(ak,bk)=1\gcd(a^k, b^k) = 1). The denominator bk=(2α5β)k=2kα5kβb^k = (2^\alpha 5^\beta)^k = 2^{k\alpha} 5^{k\beta} has only factors 22 and 55 if and only if bb does. Conversely, if bb has a prime factor r{2,5}r \notin \{2, 5\}, then bkb^k has the same prime factor, and (N/k)k(N/k)^k is non-terminating. \square

Editorial

For each N from 5 to 10000, find k maximising (N/k)^k. Then D(N) = -N if the maximum is a terminating decimal, else +N. Compute sum of D(N). We else. Finally, else.

Pseudocode

else
else

Complexity Analysis

  • Time: O(NmaxlogNmax)O(N_{\max} \log N_{\max}). For each NN, we perform O(1)O(1) floating-point comparisons, one GCD computation in O(logN)O(\log N), and trial division by 22 and 55 in O(logN)O(\log N).
  • Space: O(1)O(1).

Answer

48861552\boxed{48861552}

Code

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

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

int main() {
    const double E = exp(1.0);
    long long total = 0;

    for (int N = 5; N <= 10000; N++) {
        int k1 = (int)(N / E);
        int k2 = k1 + 1;
        if (k1 < 1) k1 = 1;

        double v1 = k1 * log((double)N / k1);
        double v2 = k2 * log((double)N / k2);
        int k = (v1 >= v2) ? k1 : k2;

        int g = __gcd(N, k);
        int d = k / g;
        while (d % 2 == 0) d /= 2;
        while (d % 5 == 0) d /= 5;

        if (d == 1)
            total -= N;
        else
            total += N;
    }

    cout << total << endl;
    return 0;
}