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(...
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 for . Then has a unique global maximum at .
Proof. Differentiating: . Setting yields , hence . The second derivative for all , confirming strict concavity. A strictly concave function on has at most one critical point, which must be the global maximum.
Corollary 1 (Discrete Optimum). The optimal integer satisfying is either or .
Proof. Since and is strictly concave, the restriction of to positive integers attains its maximum at one of the two integers adjacent to the continuous maximizer .
Theorem 2 (Terminating Decimal Criterion). A rational number in lowest terms (with and ) has a terminating decimal expansion if and only if has no prime factors other than and .
Proof. The fraction terminates if and only if there exists such that , equivalently . Since , this holds if and only if every prime divisor of lies in .
Theorem 3 (Termination of ). Let . Then is a terminating decimal if and only if has no prime factors other than and .
Proof. Write where , so is in lowest terms with denominator . By Theorem 2, terminates if and only if for some .
It remains to show that terminates if and only if terminates. Write in lowest terms. Then with (since implies ). The denominator has only factors and if and only if does. Conversely, if has a prime factor , then has the same prime factor, and is non-terminating.
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: . For each , we perform floating-point comparisons, one GCD computation in , and trial division by and in .
- Space: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Project Euler Problem 183: Maximum Product of Parts
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).
"""
import math
def is_terminating(n, k):
"""Check whether n/k in lowest terms has denominator with only factors 2 and 5."""
d = k // math.gcd(n, k)
while d % 2 == 0:
d //= 2
while d % 5 == 0:
d //= 5
return d == 1
def solve():
E = math.e
total = 0
for N in range(5, 10001):
k1 = int(N / E)
k2 = k1 + 1
if k1 < 1:
k1 = 1
v1 = k1 * math.log(N / k1)
v2 = k2 * math.log(N / k2)
k = k1 if v1 >= v2 else k2
if is_terminating(N, k):
total -= N
else:
total += N
return total
if __name__ == "__main__":
answer = solve()
assert answer == 48861552
print(answer)