Mathematica中切割股票的变种

时间:2013-03-06 09:55:49

标签: wolfram-mathematica

所以我对Mathematica很新,我正在努力学习以功能的方式解决问题。我正在解决的问题是列出我可以从列表中对元素求和的方式(重复),因此总和是leq到某个值。下面的代码解决了这个问题。

i = {7.25, 7.75, 15, 19, 22};
m = 22;
getSum[l_List, n_List] := Total[Thread[{l, n}] /. {x_, y_} -> x y];
t = Prepend[Map[Range[0, Floor[m/#]] &, i], List];
Outer @@ %;
Flatten[%, ArrayDepth[%] - 2];
Map[{#, getSum[i, #]} &, %];
DeleteCases[%, {_, x_} /; x > m || x == 0];
TableForm[Flatten /@ SortBy[%, Last], 0, 
 TableHeadings -> {None, Append[i, "Total"]}]

然而,代码检查了很多不必要的情况,如果m更高的列表更长,这可能是一个问题。我的问题就是解决这个问题最简单的Mathematica方式,即效率和代码优势。

2 个答案:

答案 0 :(得分:1)

一种简单但不是最佳的方式是:

sol = Reduce[Dot[i, {a, b, c, d, e}] <= m, {a, b, c, d, e}, Integers];

首先尝试使用较小的i,说i = {7.25, 7.75},以了解您是否可以使用此功能。

您可以通过提供系数的上限来提高速度,例如

sol = Reduce[And @@ {Dot[i, {a, b, c, d, e}] <= m, 
                     Sequence @@ Thread[{a, b, c, d, e} <= Quotient[m, i]]}, 
        {a, b, c, d, e}, Integers]

答案 1 :(得分:0)

怎么样

recurr[numbers_, boundary_] :=

  Reap[memoryRecurr[0, {}, numbers, boundary]][[2, 1]];

memoryRecurr[_, _, {}, _] := Null;

memoryRecurr[sum_, numbers_, restNumbers_, diff_] :=
  (
   Block[
     {presentNumber = First[restNumbers], restRest = Rest[restNumbers]}
     ,
     If[
      presentNumber <= diff
      ,
      Block[{
        newNumbers = Append[numbers, presentNumber],
        newSum = sum + presentNumber
        },
       Sow[{newNumbers, newSum}];

       memoryRecurr[
        newSum,
        newNumbers,
        restRest,
        diff - presentNumber
        ];
       ]
      ];
     memoryRecurr[sum, numbers, restRest, diff]
     ];

   );

那样

recurr[{1, 2, 3, 4, 5}, 7]

- &GT;

{{{1}, 1}, {{1, 2}, 3}, {{1, 2, 3}, 6}, {{1, 2, 4}, 7}, {{1, 3}, 
  4}, {{1, 4}, 5}, {{1, 5}, 6}, {{2}, 2}, {{2, 3}, 5}, {{2, 4}, 
  6}, {{2, 5}, 7}, {{3}, 3}, {{3, 4}, 7}, {{4}, 4}, {{5}, 5}}