我对以下数学模型有一个优化问题。它类似于在给定周长的情况下找到矩形的最大面积,但在此示例中,我们没有 2 个变量。
我有X
若干个总和为 的正整数Y
。我怎样才能找到给我最大乘法的整数集Y
?
例子:
鉴于Y = 8
答案应该是X[1] = 2; x[2] = 3; x[3] = 3
因为这会给我最大的乘法。
此类问题的任何python代码/逻辑?
我对以下数学模型有一个优化问题。它类似于在给定周长的情况下找到矩形的最大面积,但在此示例中,我们没有 2 个变量。
我有X
若干个总和为 的正整数Y
。我怎样才能找到给我最大乘法的整数集Y
?
例子:
鉴于Y = 8
答案应该是X[1] = 2; x[2] = 3; x[3] = 3
因为这会给我最大的乘法。
此类问题的任何python代码/逻辑?
设 n 为项目数,s 为总和。填充大小为 n 的列表,s // n
并将 1 添加到最后一个s % n
元素。这为您提供了最大产品的列表。
max_list = [s//n] * (n - s%n) + [s//n + 1] * (s%n)
It is true that the “maximum of multiplications will be when all the values are nearest to equal values”, as stated in a previous answer and implemented via
max_list = [s//n] * (n - s%n) + [s//n + 1] * (s%n)
in another answer. This can be justified by techniques similar to those used in proving the arithmetic-mean to geometric-mean inequality, as in (for example) Proof by Pólya.
When the sum Y is given but not the number of terms X, and it's desired to compute X, observe that pow(W,Y/W)
is maximal when W = e ≃ 2.71828
. The integer nearest to e
is 3, so to maximize the product, include mostly 3's, and one or two 2's. In general, include two 2's when Y%3 is 1, and one 2 when Y%3 is 2, and none when Y%3 is 0, and make up the difference with 3's. Examples (in the form, [Y:a b...] for sum Y and terms a,b,...) include [3: 3], [4: 2 2], [5: 3 2], [6:3 3], [7: 3 2 2], [8: 3 3 2], [9:3 3 3], [10: 3 3 2 2] and so forth.
乘法的最大值将是所有值最接近相等值时。
X = [Y // 3 for i in range(3)]
difference = Y - X[0] * 3
if difference == 2:
x[0] += 1
X[1] += 1
elif difference == 1:
X[0] += 1
print (X)
Output:
Y = 8
X = [3, 3, 2]
Y = 9
X = [3,3,3]
Y = 10
X = [4,3,3]