1
$\begingroup$

What is/are common way/s to handle what would be ArithmeticExceptions in Java in Mathematica/ Wolfram Language? The ugly code is the If...Continues in the innermost For Loop. How should I address this with the Wolfram Language programming paradigm?

c=0;
best={0,{}};
operators={Plus,Subtract,Times,Divide};
numbersets=Subsets[{0,1,2,3,4,5,6,7,8,9},{4}];
For[i=1,i<= Length[numbersets],i++,
 results=Table[False,9^4];
 currentSet=numbersets[[i]];
 permutations=Permutations[currentSet];
  For[j=1,j<= Length[permutations],j++,
   For[op1=1,op1<= Length[operators],op1++,
    For[op2=1,op2<= Length[operators],op2++,
     For[op3=1,op3<= Length[operators],op3++,
      c=c+1;
      If[operators[[op1]]==Divide && permutations[[j]][[2]]==0,Continue[]];
      If[operators[[op2]]==Divide && permutations[[j]][[3]]==0,Continue[]];
      If[operators[[op3]]==Divide && permutations[[j]][[4]]==0,Continue[]];
      r=operators[[op1]][permutations[[j]][[1]],permutations[[j]][[2]]];
      r=operators[[op2]][r,permutations[[j]][[3]]];
      r=operators[[op3]][r,permutations[[j]][[4]]];
      If[r>0 && IntegerQ[r],results[[r]]=True;]
     ];
    ]
   ]
  ]
 If[FirstPosition[results,False][[1]]-1>best[[1]],
  best={FirstPosition[results,False][[1]]-1,currentSet};
  Print[{FirstPosition[results,False]-1,currentSet}];
 ]
]

and yes it is Eul3r again and it does not work, but that is irrelevant to the question...

$\endgroup$
  • $\begingroup$ Simplest thing to do is to remove the If statements entirely and to add // Quiet after the whole thing. $\endgroup$ – bill s May 5 '17 at 19:08
  • 2
    $\begingroup$ Check[]? (In the Mathematica paradigm, you would at least use Do[] instead of For[] here, but also take advantage of listability/vectorization to reduce the number of loops and increase speed. But that's a side issue.) $\endgroup$ – Michael E2 May 23 '17 at 12:40
  • $\begingroup$ Which project Euler problem is it actually? Would make some things easier ^^ $\endgroup$ – Thies Heidecke May 23 '17 at 13:57
1
$\begingroup$

There are some ways you can make your code more idiomatic for the Wolfram language. First thing we can do is representing operators and numbersets with strings (i'll come to the advatage of this a bit later):

operators = {"+", "-", "*", "/"};
numbersets = Subsets[ToString /@ Range[0, 9], {4}];

Then we can apply the same idea you used for numbersets by using Subsets[] to have a similar operatorsets

operatorsets = Tuples[operators, 3];

This replaces all of your nested loops to give us a list of all possible operator combinations. We can now combine this with our numbersets to get all possible combinations of numbers and operators via Outer[]:

RiffleAndJoin = Composition[StringJoin, Riffle];
expressionstrings = Flatten[Outer[RiffleAndJoin, numbersets, operatorsets, 1], 1]
(* {"0+1+2+3", "0+1+2-3", ..., "6/7/8/9"} *)

Outer combines each element of numbersets with each element of operatorsets, which get than fed to a function of our choice, in this case our customly defined function RiffleAndJoin which takes the two lists of one character strings and riffles them, then joins them into one big string which represents the whole expression as a string. Then we call Flatten to get a flat list of string expressions. The reason for representing the expression as strings is, that we don't have to do the work of parsing the expression. We can just call

ToExpression /@ expressionstrings
(* {6, 0, 7, ..., 27/28, 1/84} *)

and let Mathematica do the expression parsing and evaluation!

$\endgroup$
  • $\begingroup$ Sorry, just saw you are also going through every permutation for each numberset, so not 100% equivalent. Maybe you still get some new ideas from my answer. $\endgroup$ – Thies Heidecke May 23 '17 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.