chyanog
发表于 2013-4-11 18:42:37
9# hujunhua
1-21和1-28的穷举后发现似乎不存在
chyanog
发表于 2013-4-11 19:21:31
这个稍快
n = 5;
Join @@ NestList
table = Table[%, {a, #}, {b, #}, {c, #}, {d, #}, {e, #}] &[ n (n + 1)/2]~Flatten~(n - 1);
fmt = MatrixForm@Internal`PartitionRagged] &;
Do], {i,table}] // Timing
Clear["`*"]
gxqcn
发表于 2013-4-11 21:25:11
今天白天本想跟帖说:这是一个非常适合编程擂台的题目,
现在打开居然已经有代码了。:)
hujunhua
发表于 2013-4-11 21:42:57
12#的代码中第3行人工生成的table大小为15^5, 用排列函数,代码和结果都可略有优化。n = 5; m = n (n + 1)/2;
f = Abs@Differences@# &;
Flatten@NestList &
table =% /@ Permutations, {n}];
fmt = MatrixForm@Internal`PartitionRagged] &;
Do, {i, table}] // Timing试了一下,第3行用Flatten比Join约快1倍。
chyanog
发表于 2013-4-11 21:49:14
14# hujunhua
不过这个总运行时间反而长了,应该还能够优化
hujunhua
发表于 2013-4-11 22:11:01
在我的电脑上,
12#的时间最快的为2.230814
14#的时间最快的为0.998406
--------------------------------------------------------
是的,上述时间都不是总时间,总时间14#慢些。14#生成排列表很快,但随后又对排列表进行操作生成table耗费了大量时间。
chyanog
发表于 2013-4-11 22:46:13
这个版本速度有了较大提升,不过牺牲一些可读性
t1 = AbsoluteTime[];
n = 5;
k = n (n + 1)/2;
cf = Compile[{{ls, _Integer, 1}},
#,
RuntimeAttributes -> {Listable}
] &[Evaluate[
Flatten@NestList,
n - 1]] & /. Slot :> #[]];
table = cf@Permutations, {n}];
fmt = MatrixForm@Internal`PartitionRagged] &;
Do, {i, table}]
AbsoluteTime[] - t1
Clear["`*"]
chyanog
发表于 2013-4-12 15:54:38
13# gxqcn
我写的C代码太难看了,中间比较长的部分还是mathematica生成的,
wayne
发表于 2013-4-13 22:49:43
18# chyanog
mathematica生成?
有才,我还没这么玩过呢
chyanog
发表于 2013-4-14 15:58:01
本帖最后由 chyanog 于 2013-4-14 15:59 编辑
19# wayne
其实没什么技术含量的。1-21和1-28的差三角似乎不存在,更大的就不清楚了
xx = Table], {i, 15}]
StringReplace[
xx == (ToLowerCase@ToString@CForm@# & /@
Join @@ NestList) //
Thread // ToString, {"," -> ";", "{" | "}" -> "", "==" -> "="}]
LogicalExpand
And @@ Thread
"" <> Table["%d ", {15}]