How to improve this Mathematica 9 code with Dynamic <p>How to improve this Mathematica 9 code with Dynamic to speed up the calculation. On my PC this code working with "n" less then 13 otherwise the result is "$Aborted", but I need n=20. You can use, for example, rho=0.64 and phi=1.107 I want that in opened html file when you type parameters automatically calculates, and even better, that was a button "calculate".</p> <p>view in browser</p> <p><img src="https://i.stack.imgur.com/W8QVx.jpg" alt=""></p> <p>at first I need solve </p> <pre><code>Solve[(xx == Sin[\[Rho]] Cos[\[Phi]]) &amp;&amp; (yy == Sin[\[Rho]] Sin[\[Phi]]) &amp;&amp; (zz == Cos[\[Rho]]), {xx, yy, zz}] </code></pre> <p>then do "Replace" three times fo each variable </p> <p>"1"</p> <pre><code>Replace[xx, First[Solve[(xx == Sin[\[Rho]] Cos[\[Phi]]) &amp;&amp; (yy == Sin[\[Rho]] Sin[\[Phi]]) &amp;&amp; (zz == Cos[\[Rho]]), {xx, yy, zz}]][[1]]] </code></pre> <p>"2"</p> <pre><code>Replace[yy, First[Solve[(xx == Sin[\[Rho]] Cos[\[Phi]]) &amp;&amp; (yy == Sin[\[Rho]] Sin[\[Phi]]) &amp;&amp; (zz == Cos[\[Rho]]), {xx, yy, zz}]][[2]]] </code></pre> <p>"3"</p> <pre><code>Replace[zz, First[Solve[(xx == Sin[\[Rho]] Cos[\[Phi]]) &amp;&amp; (yy == Sin[\[Rho]] Sin[\[Phi]]) &amp;&amp; (zz == Cos[\[Rho]]), {xx, yy, zz}]][[3]]] </code></pre> <p>In fact, I write it all in one "Dynamic" as I do not know how to make it right - so that in the html file automatically calculates all after entering the values ​​of the variables </p> <p>"1","2","3" in code are acronyms for the above</p> <pre><code>Dynamic[ Cases[ Drop[Tuples[Range[-n, n], 3], {( Length[Tuples[Range[-n, n], 3]] + 1)/2}], {h_, k_, l_} /; ("1" -"1"/10) &lt;= h/( GCD[h, k, l] Sqrt[ h^2 + k^2 + l^2]) &lt;= ("1" +"1"/10) \[And] ("2" -"2"/10) &lt;= k/( GCD[h, k, l] Sqrt[ h^2 + k^2 + l^2]) &lt;= ("2" +"2"/10) \[And] ("3" -"3"/10) &lt;= l/( GCD[h, k, l] Sqrt[ h^2 + k^2 + l^2]) &lt;= ("3" +"3"/10)]] </code></pre> <p>if denote</p> <p>"11" - </p> <pre><code>h/(GCD[h, k, l] Sqrt[h^2 + k^2 + l^2]) </code></pre> <p>"22" - </p> <pre><code>k/(GCD[h, k, l] Sqrt[h^2 + k^2 + l^2]) </code></pre> <p>"33" - </p> <pre><code>l/(GCD[h, k, l] Sqrt[h^2 + k^2 +l^2]) </code></pre> <p>then code are:</p> <pre><code> Dynamic[ Cases[ Drop[ Tuples[ Range[-n, n], 3], {(Length[Tuples[Range[-n, n], 3]] + 1)/2}], {h_, k_, l_} /; ("1" -"1"/10) &lt;= "11" &lt;= ("1" +"1"/10) \[And] ("2" -"2"/10) &lt;= "22" &lt;= ("2" +"2"/10) \[And] ("3" -"3"/10) &lt;= "33" &lt;= ("3" +"3"/10)]] </code></pre> <p>This code works, as I need. Thank you for your interest!</p> <pre><code>Column[{Style["Определить hkl", Bold, 16], Labeled[InputField[Dynamic[\[Rho]]], "\[Rho]", Left, LabelStyle -&gt; Directive[Bold, FontSize -&gt; 18]], Labeled[InputField[Dynamic[\[Phi]]], "\[Phi]", Left, LabelStyle -&gt; Directive[Bold, FontSize -&gt; 18]], Labeled[InputField[Dynamic[n]], "n", Left, LabelStyle -&gt; Directive[Bold, FontSize -&gt; 18]]}] SetAttributes[redoButton, HoldRest] redoButton[str_, fun_] := DynamicModule[{result = Null}, Column[{Button[str, result = fun]}]] Column[{redoButton[ "x,y,z", {px = Replace[xx, First[Solve[(xx == Sin[\[Rho]] Cos[\[Phi]]) &amp;&amp; (yy == Sin[\[Rho]] Sin[\[Phi]]) &amp;&amp; (zz == Cos[\[Rho]]), {xx, yy, zz}]][[1]]], py = Replace[yy, First[Solve[(xx == Sin[\[Rho]] Cos[\[Phi]]) &amp;&amp; (yy == Sin[\[Rho]] Sin[\[Phi]]) &amp;&amp; (zz == Cos[\[Rho]]), {xx, yy, zz}]][[2]]], pz = Replace[zz, First[Solve[(xx == Sin[\[Rho]] Cos[\[Phi]]) &amp;&amp; (yy == Sin[\[Rho]] Sin[\[Phi]]) &amp;&amp; (zz == Cos[\[Rho]]), {xx, yy, zz}]][[3]]]}], {Dynamic[px], Dynamic[py], Dynamic[pz]}, redoButton["ppp", ppp = Part[ Nearest[ReplaceAll[ Drop[Tuples[Range[-n, n], 3], {( Length[Tuples[Range[-n, n], 3]] + 1)/2}], {h_, k_, l_} -&gt; {h/ Sqrt[h^2 + k^2 + l^2], k/Sqrt[h^2 + k^2 + l^2], l/Sqrt[ h^2 + k^2 + l^2]}], {px, py, pz}], 1]], Dynamic[ppp], redoButton["hkl", hkl = MatrixForm[ Cases[Drop[ Tuples[Range[-n, n], 3], {( Length[Tuples[Range[-n, n], 3]] + 1)/2}], {h_, k_, l_} /; h/(GCD[h, k, l] Sqrt[h^2 + k^2 + l^2]) == Part[ppp, 1] \[And] k/(GCD[h, k, l] Sqrt[h^2 + k^2 + l^2]) == Part[ppp, 2] \[And] l/(GCD[h, k, l] Sqrt[h^2 + k^2 + l^2]) == Part[ppp, 3]]]], Style[Dynamic[hkl], Bold, 18]}] </code></pre>