Solve equation with unknown numeric operators?

Posted 9 months ago
1002 Views
|
3 Replies
|
2 Total Likes
|
 How to solve an equation with unknown numeric operators in Wolfram Alpha? 75 ? 4 ? 10 ? 6 ? 2 ? 2 = 286 ? are the basic numeric operators + - * /With a bash shell, I can use the brace expansion to generate all combinations and use the basic calculator (bc) to evaluate it: n=0 for i in 75{+,-,*,/}4{+,-,*,/}10{+,-,*,/}6{+,-,*,/}2{+,-,*,/}2; do n=$((n+1)) j=$(echo $i | bc) if [$j -eq 286 ]; then echo "Result is ${i}=${j} (\${n} iterations)" | sed -r 's/([=+*\/-])/ \1 /g' break fi done The answer is 75 * 4 + 10 - 6 * 2 * 2 = 286 I understand this could be tricky to implement if you also permute all numbers.
3 Replies
Sort By:
Posted 9 months ago
 The answer to you question is YES. You need just to add a on-liner parser to accomplish what you want. I have added such a parser and the new solver. Please find below the updated code. buildLeft[randList_, ratorList_] := Append[Flatten@ Transpose[{Take[randList, Length@randList - 1], ratorList}] , Last@randList]; leftEqBuilder[randList_, ratorList_] := StringJoin[ If[MemberQ[randList, #], ToString@#, #] & /@ buildLeft[randList, ratorList]]; solver[randList_, ratorList_, rhs_] := Module[{ tup = Tuples[ratorList, Length[randList] - 1]}, leftEqBuilder[randList, SelectFirst[tup, ToExpression[leftEqBuilder[randList, #]] == rhs &]]]; solutionQ[str_, rhs_] := ToExpression@str == rhs; parseExpression[expr_, dum_: "?"] := Module[{s = StringSplit[StringReplace[expr, " " -> ""], "="]}, {StringSplit[ First@s, dum], Last@s}]; newSolver[strExpr_, ratorList_, dum_: "?"] := Module[{s = parseExpression[strExpr, dum]}, solver[ToExpression@HoldForm /@ First@s, ratorList, ToExpression@Last@s] <> " = " <> Last@s]; Now let's test it. newSolver["75*4?10?6?2*2=286", {"+", "-", "*", "/"}] (* 75*4+10-6*2*2 = 286 *) newSolver["1?1?1=3", {"+", "-", "*", "/"}] (* 1+1+1 = 3 *) Thanks,
Posted 9 months ago
 Would it be possible to use an infix like "?" (or something else) as new custom operator and call the above solver function?The Wolfram Alpha built-in Infix function is not designed to define custom operator right?(like custom-defined infix operators in Perl 6)Bottom line it would be nice for Wolfram Alpha to guess that ? is for the missing numeric operators. in: 1 ? 1 ? 1 = 3 out: 1 + 1 + 1 = 3 https://www.wolframalpha.com/input/?i=1+%3F+1+%3D+2Extend the InputForm capabilities to understand the meaning of ? or a create a special sign for this purposeREFERENCES
Posted 9 months ago
 Please find below my suggestion about your problem. buildLeft[randList_, ratorList_] := Append[Flatten@ Transpose[{Take[randList, Length@randList - 1], ratorList}] , Last@randList]; leftEqBuilder[randList_, ratorList_] := StringJoin[ If[MemberQ[randList, #], ToString@#, #] & /@ buildLeft[randList, ratorList]]; solver[randList_, ratorList_, rhs_] := Module[{ tup = Tuples[ratorList, Length[randList] - 1]}, leftEqBuilder[randList, SelectFirst[tup, ToExpression[leftEqBuilder[randList, #]] == rhs &]]]; solutionQ[str_, rhs_] := ToExpression@str == rhs; Tests solver[{1, 2, 3, 4}, {"+", "-"}, 10] (* 1+2+3+4 *) solver[{75, 4, 10, 6, 2, 2}, {"+", "-", "*", "/"}, 286] (* 75*4+10-6*2*2 *) Check solutionQ[solver[{1, 2, 3, 4}, {"+", "-"}, 10],10] (* True *) solutionQ[solver[{75, 4, 10, 6, 2, 2}, {"+", "-", "*", "/"}, 286], 286] (* True *) Note that the solution provided is quite general: The operands do not have to be numbers. The operators need only to be compatible with the operands. Thanks,
Community posts can be styled and formatted using the Markdown syntax.