Project Euler Problem 190

Let Sm = (x1, x2, dots , xm) be the m-tuple of positive real numbers with x1 + x2 + cdots + xm = m for which Pm = x1 cdo

Project Euler Problem 190

Solution

Answer: 371048281

We must maximize

$$P_m=x_1x_2^2x_3^3\cdots x_m^m$$

subject to

$$x_1+x_2+\cdots+x_m=m, \qquad x_i>0.$$

We then compute

$$\sum_{m=2}^{15}\lfloor P_m\rfloor.$$


1. Mathematical analysis

We seek the maximum of the weighted product

$$P_m=\prod_{k=1}^m x_k^k.$$

It is usually easier to maximize the logarithm:

$$\log P_m=\sum_{k=1}^m k\log x_k.$$

The constraint is

$$\sum_{k=1}^m x_k=m.$$

This is a standard constrained optimization problem.

Using Lagrange multipliers

Define

$$F(x_1,\dots,x_m,\lambda) = \sum_{k=1}^m k\log x_k -\lambda\left(\sum_{k=1}^m x_k-m\right).$$

Differentiate with respect to $x_k$:

$$\frac{\partial F}{\partial x_k} = \frac{k}{x_k}-\lambda.$$

At the optimum,

$$\frac{k}{x_k}=\lambda,$$

so

$$x_k=\frac{k}{\lambda}.$$

Now impose the constraint:

$$\sum_{k=1}^m x_k = \sum_{k=1}^m \frac{k}{\lambda} = \frac{1}{\lambda}\cdot \frac{m(m+1)}2 = m.$$

Hence

$$\lambda=\frac{m+1}{2}.$$

Therefore the maximizing tuple is

$$x_k=\frac{2k}{m+1}.$$

So

$$P_m = \prod_{k=1}^m \left(\frac{2k}{m+1}\right)^k.$$

That is the exact maximal value.


Small check: $m=10$

The problem states:

$$\lfloor P_{10}\rfloor=4112.$$

Using the formula:

$$P_{10} = \prod_{k=1}^{10} \left(\frac{2k}{11}\right)^k.$$

Numerically this is approximately

$$4112.085\dots$$

thus

$$\lfloor P_{10}\rfloor=4112,$$

matching the statement.


2. Python implementation

from math import prod

total = 0

for m in range(2, 16):

    # Compute the maximal value:
    # P_m = product((2k/(m+1))^k for k=1..m)
    Pm = prod((2 * k / (m + 1)) ** k for k in range(1, m + 1))

    total += int(Pm)   # floor for positive numbers

print(total)

3. Code walkthrough

Import

from math import prod

prod() multiplies all values from an iterable.


Initialize total

total = 0

We will accumulate

$$\sum_{m=2}^{15}\lfloor P_m\rfloor.$$


Loop over $m$

for m in range(2, 16):

The upper bound in Python ranges is excluded, so this iterates from $2$ through $15$.


Compute $P_m$

Pm = prod((2 * k / (m + 1)) ** k for k in range(1, m + 1))

This directly implements

$$P_m = \prod_{k=1}^m \left(\frac{2k}{m+1}\right)^k.$$

For each $k$:

  • compute $\frac{2k}{m+1}$,
  • raise it to the $k$-th power,
  • multiply all terms together.

Take the floor

total += int(Pm)

Since $P_m>0$, converting to int is equivalent to taking the floor.


Output

print(total)

This prints the required sum.


4. Final verification

The values grow rapidly with $m$, but not astronomically for $m\le15$, so standard floating-point arithmetic is sufficient.

Checking the provided example:

$$\lfloor P_{10}\rfloor=4112,$$

which our formula reproduces exactly.

Evaluating all terms from $m=2$ to $15$ gives:

$$371048281.$$

This is positive and of reasonable magnitude given the exponential growth of the products.


Answer: 371048281