Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>OK, this is not elegant or fast, and it's buggy, but it works (sometimes). It uses a <a href="http://en.wikipedia.org/wiki/Monte_Carlo_method" rel="nofollow noreferrer">monte carlo</a> method, implementing the metropolis algorithm for a weight function that I (arbitrarily) selected just to see if this would work. This was some time ago for a similar problem; I suppose my mathematica skills have improved as it looks ugly now, but I have no time to fix it at the moment.</p> <p>Execute this (it looks more reasonable when you paste it into a notebook):</p> <pre><code>ClearAll[swap]; swap[lst_, {p1_, p2_}] := ReplacePart[ lst, {p1 \[Rule] lst\[LeftDoubleBracket]p2\[RightDoubleBracket], p2 \[Rule] lst\[LeftDoubleBracket]p1\[RightDoubleBracket]}] ClearAll[evalops]; (*first element of opslst is Identity*) evalops[opslst_, ord_, nums_] := Module[{curval}, curval = First@nums; Do[curval = opslst\[LeftDoubleBracket]p\[RightDoubleBracket][curval, nums\[LeftDoubleBracket]ord\[LeftDoubleBracket]p\ \[RightDoubleBracket]\[RightDoubleBracket]], {p, 2, Length@nums}]; curval] ClearAll[randomizeOrder]; randomizeOrder[ordlst_] := swap[ordlst, RandomInteger[{1, Length@ordlst}, 2]] ClearAll[randomizeOps]; (*never touch the first element*) randomizeOps[oplst_, allowedOps_] := ReplacePart[ oplst, {RandomInteger[{2, Length@oplst}] \[Rule] RandomChoice[ops]}] ClearAll[takeMCstep]; takeMCstep[goal_, opslst_, ord_, nums_, allowedops_] := Module[{curres, newres, newops, neword, p}, curres = evalops[opslst, ord, nums]; newops = randomizeOps[opslst, allowedops]; neword = randomizeOrder[ord]; newres = evalops[newops, neword, nums]; Switch[Abs[newres - goal], 0, {newops, neword}, _, (p = Abs[curres - goal]/Abs[newres - goal]; If[RandomReal[] &lt; p, {newops, neword}, {opslst, ord}])]] </code></pre> <p>then to solve your actual problem, do </p> <pre><code>ops = {Times, Plus, Subtract, Divide} nums = {25, 50, 75, 100, 3, 6} ord = Range[Length@nums] (*the first element is identity to simplify the logic later*) oplist = {Identity}~Join~RandomChoice[ops, Length@nums - 1] out = NestList[ takeMCstep[ 99, #\[LeftDoubleBracket]1\[RightDoubleBracket], #\ \[LeftDoubleBracket]2\[RightDoubleBracket], nums, ops] &amp;, {oplist, ord}, 10000] </code></pre> <p>and then to see that it worked,</p> <pre><code>ev = Map[evalops[#\[LeftDoubleBracket]1\[RightDoubleBracket], #\ \[LeftDoubleBracket]2\[RightDoubleBracket], nums] &amp;, out]; ev // Last // N ev // ListPlot[#, PlotMarkers \[Rule] None] &amp; </code></pre> <p>giving</p> <p><img src="https://i.stack.imgur.com/GXiMl.png" alt="enter image description here"></p> <p>thus, it obtained the correct order of operators and numbers after around 2000 tries.</p> <p>As I said, it's ugly, inefficient, and badly programmed as it was a quick-and-dirty adaptation of a quick-and-dirty hack. If you're interested I can clean up and explain the code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload