Re: FactorInteger Print Formatting
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg725] Re: FactorInteger Print Formatting
- From: rubin at msu.edu (Paul A. Rubin)
- Date: 11 Apr 1995 20:49:02 GMT
- Organization: Michigan State University
In article <3m4q93$e47 at news0.cybernetics.net>,
Alan Powell <POWELLA at delphi.com> wrote:
->
->
->Could anyone suggest a neat way to reformat the output of
->FactorInteger into a more usable print format?
->
->FactorInteger[n] gives a list of prime factors of the integer n
->in ascending order, together with their exponents viz:
->
-> FactorInteger[900] produces the list {{2,2},{3,2},{5,2}}
->
->What I need is a function called say factorform[list] such that:
->
-> Print[ factorform[{{2,2},{3,2},{5,2}}] ] produces 2^2*3^2*5^2
->
->Ideally exponents equal to 1 would be suppressed so that when
->
-> ff = FactorInteger[30] produces {{2,1},{3,1},{5,1}} then
->
-> Print[factorform[ff]] would give 2*3*5 instead of 2^1*3^1*5^1 and
->
-> Print[factorform[FactorInteger[2^6*3*5^2*7]]] would give 2^6*3*5^2*7
->
->A procedural solution is possible, but I was unable to create a simple
->function which would give the desired result.
->
->
->Alan Powell POWELLA at delphi.com
If you don't need your "factorform[ff]" to reduce to a list for purposes of
subsequent arithmetic, etc., you can do it with Format.
In[1]:= faclist[ x_Integer ] := ff[ FactorInteger[ x ] ]
In[2]:= Format[ ff[ {n_Integer, 1} ] ] := n
In[3]:= Format[ ff[ {n_Integer, m_Integer} ] ] := SequenceForm[ n, "^", m ]
In[4]:= Format[ ff[ x_List ] ] :=
SequenceForm[ ff[ x[[1]] ], "*", ff[ Rest[ x ] ] ]
In[5]:= Format[ ff[ {x_List} ] ] := ff[ x ]
This defines a function faclist that wraps the output of FactorInteger in a
dummy head "ff." It would be a good idea to Protect[ ff ] so that the
symbol ff does not inadvertantly get assigned a value someplace else. Now
faclist prints in the desired format automatically:
In[6]:= faclist[ 2400 ]
Out[6]= 2^5*3*5^2
Paul
**************************************************************************
* Paul A. Rubin Phone: (517) 432-3509 *
* Department of Management Fax: (517) 432-1111 *
* Eli Broad Graduate School of Management Net: RUBIN at MSU.EDU *
* Michigan State University *
* East Lansing, MI 48824-1122 (USA) *
**************************************************************************
Mathematicians are like Frenchmen: whenever you say something to them,
they translate it into their own language, and at once it is something
entirely different. J. W. v. GOETHE