Trying to write out the prime factorization of a number with CenterDot and Superscript - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-22T11:12:46Z https://mathematica.stackexchange.com/feeds/question/60651 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/60651 7 Trying to write out the prime factorization of a number with CenterDot and Superscript Sultan of Swing https://mathematica.stackexchange.com/users/19987 2014-09-25T18:49:39Z 2014-10-22T21:18:05Z <p>I am trying to use CenterDot and Superscript, in conjunction with Apply to write out the prime factorization of any given integer. Everything works well UNLESS the prime factorization of the number contains only one prime (to some power). This is because CenterDot must have two arguments I guess.</p> <p>So let's get going here...</p> <pre><code>primeFactorForm[n_] := CenterDot[Apply[Superscript, FactorInteger[n], {1}]] primeFactorForm </code></pre> <p>gives me</p> <pre><code>CenterDot[3^2] </code></pre> <p>and I don't want that obviously. So I created another function to avoid this problem, and put it before my primeFactorForm function that I created....</p> <pre><code>myCenterDot[{a_^b_}] := a^b myCenterDot[x_] := Apply[CenterDot, x] </code></pre> <p>and then I changed my original function</p> <pre><code>primeFactorForm[n_] := myCenterDot[Apply[Superscript, FactorInteger[n], {1}]] </code></pre> <p>So now it SHOULD just return a^b IF the input is in the form {a^b}, but it still returns</p> <pre><code>CenterDot[3^2] </code></pre> <p>instead of just writing it out like i want it to. What have I done wrong? Just some minor syntax errors I hope! Thanks very much in advance!</p> <p><strong>EDIT: Sorry guys, I should have also mentioned this: when I suggested to my instructor that I add an If statement, he said that doing what I did (creating two myCenterDot functions) would create the same end result without the need of creating an If statement. So he basically said that I can avoid the use of an If statement by doing this that way....hmmm....</strong></p> https://mathematica.stackexchange.com/questions/60651/-/60653#60653 5 Answer by Bob Hanlon for Trying to write out the prime factorization of a number with CenterDot and Superscript Bob Hanlon https://mathematica.stackexchange.com/users/9362 2014-09-25T19:20:38Z 2014-09-25T19:20:38Z <pre><code>primeFactorForm[n_Integer] := Module[{fact = FactorInteger[n]}, If[Length[fact] == 1, Superscript @@ fact[], CenterDot @@ Superscript @@@ fact]] primeFactorForm </code></pre> <p><img src="https://i.stack.imgur.com/dlQ1W.jpg" alt="enter image description here"></p> <pre><code>primeFactorForm </code></pre> <p><img src="https://i.stack.imgur.com/N1aFR.jpg" alt="enter image description here"></p> https://mathematica.stackexchange.com/questions/60651/-/60655#60655 2 Answer by DumpsterDoofus for Trying to write out the prime factorization of a number with CenterDot and Superscript DumpsterDoofus https://mathematica.stackexchange.com/users/9697 2014-09-25T19:29:49Z 2014-09-25T19:36:09Z <p>Another almost identical way:</p> <pre><code>primeFactorForm[n_] := If[Length@# == 1, First@#, CenterDot @@ #] &amp;[ Superscript @@@ FactorInteger[n]]; </code></pre> <p>Example output:</p> <pre><code>Column[primeFactorForm /@ RandomInteger[{0, 10000}, 10]] </code></pre> <p><img src="https://i.stack.imgur.com/uywKM.png" alt="enter image description here"></p> https://mathematica.stackexchange.com/questions/60651/-/60686#60686 2 Answer by ubpdqn for Trying to write out the prime factorization of a number with CenterDot and Superscript ubpdqn https://mathematica.stackexchange.com/users/1997 2014-09-26T07:31:02Z 2014-09-26T07:31:02Z <pre><code>stf[{_, 1}] := StringTemplate["1"]; stf[{_, _}] := StringTemplate["\!$$\*SuperscriptBox[\(1$$, $$2$$]\)"]; funcn[u_] := StringJoin[ Riffle[TemplateApply[stf[#], #] &amp; /@ FactorInteger[u], "\[CenterDot]"]] </code></pre> <p>Testing:</p> <pre><code>Grid[Style[#, 20] &amp; /@ ({#, "=", funcn[#]}) &amp; /@ Range] </code></pre> <p><img src="https://i.stack.imgur.com/XR4iU.png" alt="enter image description here"></p> https://mathematica.stackexchange.com/questions/60651/-/61211#61211 6 Answer by Mr.Wizard for Trying to write out the prime factorization of a number with CenterDot and Superscript Mr.Wizard https://mathematica.stackexchange.com/users/121 2014-10-02T17:32:14Z 2014-10-22T21:18:05Z <p>The existing answers work but I offer two improvements:</p> <ul> <li><p>terse code via pattern replacement</p></li> <li><p>making an actual formatting wrapper</p></li> </ul> <p>This replacement rule strips the heads of any expressions with only one argument:</p> <pre><code>foo /. _[x_] :&gt; x foo[1, 2] /. _[x_] :&gt; x </code></pre> <blockquote> <pre><code>1 foo[1, 2] </code></pre> </blockquote> <p><a href="http://reference.wolfram.com/mathematica/ref/Format.html" rel="nofollow noreferrer"><code>Format</code></a> is used to describe the output format without losing the original expression.</p> <h1>Proposal:</h1> <pre><code>Format[primeFactorForm[n_Integer]] := CenterDot @@ Superscript @@@ FactorInteger[n] /. _[x_] :&gt; x </code></pre> <p>Now:</p> <pre><code>primeFactorForm </code></pre> <blockquote> <p>$3^2$</p> </blockquote> <pre><code>out = primeFactorForm </code></pre> <blockquote> <p>$3^3\cdot 5^8\cdot 11^3\cdot 19^2$</p> </blockquote> <p>Look at the <a href="http://reference.wolfram.com/mathematica/ref/FullForm.html" rel="nofollow noreferrer"><code>FullForm</code></a> of <code>out</code>:</p> <pre><code>FullForm[out] </code></pre> <blockquote> <pre><code>primeFactorForm </code></pre> </blockquote> <p>You can therefore get the original integer with:</p> <pre><code>First @ out </code></pre> <blockquote> <pre><code>5067678515625 </code></pre> </blockquote> <hr> <h3>Exponent one</h3> <p>If you wish to eliminate exponent one then you may use this instead:</p> <pre><code>Format[primeFactorForm[n_Integer]] := CenterDot @@ Superscript @@@ FactorInteger[n] //. _[x_] | _[x_, 1] :&gt; x </code></pre> <p>Example:</p> <pre><code>primeFactorForm </code></pre> <blockquote> <p>$2^3\cdot 3^2\cdot 41\cdot 131\cdot 307\cdot 5077$</p> </blockquote> <hr> <h3>Performance with large integers</h3> <p>Since I put the actual factorization in the formatting function my method will result in the same delay every time the expression is displayed. If you would prefer to optimize any re-display by recalling the earlier result we can add memorization of the <code>Format</code> function. (I discovered a small problem in that my <a href="https://mathematica.stackexchange.com/a/2676/121">usual</a> <code>mem :</code> syntax does not work here as it interferes with formatting.) For example:</p> <pre><code>Format[primeFactorForm[n_Integer]] := Format[primeFactorForm[n]] = CenterDot @@ Superscript @@@ FactorInteger[n] //. _[x_] | _[x_, 1] :&gt; x </code></pre> <p>Now the first display of <code>primeFactorForm[int]</code> takes a few seconds:</p> <pre><code>SeedRandom; int = RandomInteger[1*^55]; primeFactorForm[int] </code></pre> <p>But a second display is instantaneous.</p> <p>This of course consumes memory and it should not be used if a great many integers are to be displayed in this format in a given session.</p> https://mathematica.stackexchange.com/questions/60651/-/61216#61216 3 Answer by Tobias Hagge for Trying to write out the prime factorization of a number with CenterDot and Superscript Tobias Hagge https://mathematica.stackexchange.com/users/5523 2014-10-02T18:42:41Z 2014-10-02T18:42:41Z <p>One option is to define the formatting of <code>CenterDot</code> for the one argument case:</p> <pre><code>Format[CenterDot[a_]] := Format[a] In:= CenterDot @@ Apply[Superscript, FactorInteger, {1}] </code></pre> <p>Out= $3^2$</p> <p>Note that the <code>FullForm</code> of this output is <code>CenterDot[Subscript[3,2]]</code>.</p> https://mathematica.stackexchange.com/questions/60651/-/61251#61251 2 Answer by Simon Woods for Trying to write out the prime factorization of a number with CenterDot and Superscript Simon Woods https://mathematica.stackexchange.com/users/862 2014-10-03T10:41:05Z 2014-10-03T10:41:05Z <p>I prefer Mr Wizard's answer, but here is a method using the properties of <code>Power</code> and <code>Times</code> to deal with exponent 1 and single prime factors. Specifically:</p> <ul> <li><p><code>Power[x, 1]</code> gives <code>x</code></p></li> <li><p><code>Times[x]</code> gives <code>x</code></p></li> </ul> <p>The prime factors are wrapped in <code>Defer</code> to prevent any further evaluation.</p> <p>So:</p> <pre><code>primeFactorForm = Times @@ Power @@@ MapAt[Defer, FactorInteger@#, {All, 1}] /. Times -&gt; CenterDot &amp; primeFactorForm primeFactorForm </code></pre> <blockquote> <p>$11^2$</p> <p>$2\cdot 3\cdot 5^3$</p> </blockquote>