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...
// Quiet
after the whole thing. $\endgroup$Check[]
? (In the Mathematica paradigm, you would at least useDo[]
instead ofFor[]
here, but also take advantage of listability/vectorization to reduce the number of loops and increase speed. But that's a side issue.) $\endgroup$