无法从{2,3,4,5,6,7,8}获得的最小整数(Mathematica)

Roy*_*yce 8 wolfram-mathematica

我正在尝试使用Mathematica解决以下问题:

{2,3,4,5,6,7,8}通过算术运算{+,-,*,/},取幂和括号从集合中无法获得的最小正整数是多少.集合中的每个数字必须只使用一次.不允许一元操作(例如,1不能转换为-1而不使用0).

例如,该号码1073741824000000000000000可通过以下方式获得(((3+2)*(5+4))/6)^(8+7).

我是Mathematica的初学者.我编写的代码我相信解决了集合的问题{2,3,4,5,6,7}(我获得了2249作为我的答案),但我的代码效率不高,无法使用集合{2,3,4,5,6,7,8}.(我的代码在设置上运行需要71秒{2,3,4,5,6,7})

我非常感谢使用Mathematica解决这个更难的问题的任何提示或解决方案,或者关于如何加速现有代码的一般见解.

我现有的代码使用蛮力,递归方法:

(*这定义了一组1号码的组合作为该1号码的集合*)

combinations[list_ /; Length[list] == 1] := list
Run Code Online (Sandbox Code Playgroud)

(*这测试是否可以对两个数字进行取幂,包括(有些)任意限制以防止溢出*)

oktoexponent[number1_, number2_] :=

 If[number1 == 0, number2 >= 0,
  If[number1 < 0,
   (-number1)^number2 < 10000 \[And] IntegerQ[number2],
   number1^number2 < 10000 \[And] IntegerQ[number2]]]
Run Code Online (Sandbox Code Playgroud)

(*这需要一个列表并删除分母大于100000*的分数)

cleanup[list_] := Select[list, Denominator[#] < 100000 &]
Run Code Online (Sandbox Code Playgroud)

(*这定义了一组2个数字的组合 - 并返回一组通过应用程序获得的所有可能的数字+ - */由oktoexponent和清理规则*过滤)

combinations[list_ /; Length[list] == 2 && Depth[list] == 2] :=
  cleanup[DeleteCases[#, Null] &@DeleteDuplicates@
    {list[[1]] + list[[2]],
     list[[1]] - list[[2]],
     list[[2]] - list[[1]],
     list[[1]]*list[[2]],
     If[oktoexponent[list[[1]], list[[2]]], list[[1]]^list[[2]],],
     If[oktoexponent[list[[2]], list[[1]]], list[[2]]^list[[1]],],
     If[list[[2]] != 0, list[[1]]/list[[2]],],
     If[list[[1]] != 0, list[[2]]/list[[1]],]}]
Run Code Online (Sandbox Code Playgroud)

(*这扩展了组合以处理集合集*)

combinations[
  list_ /; Length[list] == 2 && Depth[list] == 3] := 
 Module[{m, n, list1, list2},
  list1 = list[[1]];
  list2 = list[[2]];
  m = Length[list1]; n = Length[list2];
  cleanup[
   DeleteDuplicates@
    Flatten@Table[
      combinations[{list1[[i]], list2[[j]]}], {i, m}, {j, n}]]]
Run Code Online (Sandbox Code Playgroud)

(*对于给定的集合,分区将所有分区的集合返回到两个非空子集*)

partition[list_] := Module[{subsets},
  subsets = Select[Subsets[list], # != {} && # != list &]; 
  DeleteDuplicates@
   Table[Sort@{subsets[[i]], Complement[list, subsets[[i]]]}, {i, 
     Length[subsets]}]]
Run Code Online (Sandbox Code Playgroud)

(*最终扩展组合以适用于任何大小的集合*)

combinations[list_ /; Length[list] > 2] := 
 Module[{partitions, k},
  partitions = partition[list];
  k = Length[partitions]; 
  cleanup[Sort@
    DeleteDuplicates@
     Flatten@(combinations /@ 
        Table[{combinations[partitions[[i]][[1]]], 
          combinations[partitions[[i]][[2]]]}, {i, k}])]]

Timing[desiredset = combinations[{2, 3, 4, 5, 6, 7}];]

{71.5454, Null}

Complement[
   Range[1, 3000], #] &@(Cases[#, x_Integer /; x > 0 && x <= 3000] &@
   desiredset)

{2249, 2258, 2327, 2509, 2517, 2654, 2789, 2817, 2841, 2857, 2990, 2998}
Run Code Online (Sandbox Code Playgroud)

小智 0

这没有什么帮助,但我今天的无用胡言乱语已经超出了我的配额:

(* it turns out the symbolizing + * is not that useful after all *) 
f[x_,y_] = x+y 
fm[x_,y_] = x-y 
g[x_,y_] = x*y 
gd[x_,y_] = x/y 

(* power properties *) 
h[h[a_,b_],c_] = h[a,b*c] 
h[a_/b_,n_] = h[a,n]/h[b,n] 
h[1,n_] = 1 

(* expand simple powers only! *) 
(* does this make things worse? *) 
h[a_,2] = a*a 
h[a_,3] = a*a*a 

(* all symbols for two numbers *) 
allsyms[x_,y_] := allsyms[x,y] =  
 DeleteDuplicates[Flatten[{f[x,y], fm[x,y], fm[y,x],  
 g[x,y], gd[x,y], gd[y,x], h[x,y], h[y,x]}]] 

allsymops[s_,t_] := allsymops[s,t] =  
 DeleteDuplicates[Flatten[Outer[allsyms[#1,#2]&,s,t]]] 

Clear[reach]; 
reach[{}] = {} 
reach[{n_}] := reach[n] = {n} 
reach[s_] := reach[s] = DeleteDuplicates[Flatten[ 
 Table[allsymops[reach[i],reach[Complement[s,i]]],  
  {i,Complement[Subsets[s],{ {},s}]}]]] 
Run Code Online (Sandbox Code Playgroud)

这里的总体思想是避免计算幂(这是昂贵的且不可交换的),同时使用加法/乘法的交换性/结合性来减少reach[]的基数。

上面的代码也可以在:

https://github.com/barrycarter/bcapps/blob/master/playground.m#L20

以及其他数十亿字节的无用代码、数据和幽默。