Re: [问题] Mathematica bug?

楼主: chungyuandye (养花种鱼数月亮赏星星)   2016-08-07 00:15:31
※ 引述《biglion ( )》之铭言:
: 最近在解 Project Euler 写了一段code
: 出现了一些error message
: 个人觉得这可能是Mathematica的bug
: fexp[{n_Integer}] = 1;
: fexp[exp_List] := fexp[exp] = Multinomial @@ exp;
: f[i_] := fexp[Sort[FactorInteger[i]][[All, 2]]];
: code蛮单纯的,就不解释了
: 执行
: Sum[f[i], {i, 1000000}] // AbsoluteTiming
: 几秒钟后就计算完毕
: 但如果执行
: Sum[f[i], {i, 1000001}] // AbsoluteTiming
: 开始出现错误讯息
: FactorInteger::exact: Argument i in FactorInteger[i] is not an exact number.
: Part::partd: "Part specification FactorInteger[i][[All,2]] is longer than
: depth of object."
: 但事实上 单独执行f[1000001]是正常的
: 神奇的是 如果使用平行运算又没问题
: ParallelSum[f[i], {i, 1000001}] // AbsoluteTiming
: 而且答案在Project Euler上是正确的
: 所以code应该是没错
: 使用Mathematica 10.2 跟 10.3 都是如此
: 想请教各位高手的是:
: 1) 这究竟是否是Bug,或其实是coding上有问题
: 2) 如果真是Bug,要怎么避开这个Bug
: 3) 当我丢平行运算的时候,各thread并无法共用内存,导致DP无法真正发挥效用
: 应该要如何设定才能够让各thread可以共用呢?
Clear[fexp, f, iter, fvalue];
iter = 0; fvalue = 0;
Dynamic[{iter, fvalue}]
fexp[exp_List] := Multinomial @@ exp;
f[i_] := fexp[FactorInteger[i][[All, 2]]];
[email protected][iter = i; fvalue = f[i]; f[i], {i, 1000001}]
[email protected][f[i], {i, 1000001}]
作者: biglion ( )   2016-08-07 09:23:00
抱歉,我没说清楚 我想保留dynamic programming的部分因为原始程式其实更复杂 不希望每个iteration重复运算

Links booklink

Contact Us: admin [ a t ] ucptt.com