MathGroup Archive 1995

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: FactorInteger Print Formatting

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg700] Re: [mg650] FactorInteger Print Formatting
  • From: Count Dracula <lk3a at kelvin.seas.virginia.edu>
  • Date: Mon, 10 Apr 1995 14:55:23 -0400

 "Powell" == Alan Powell <POWELLA at delphi.com> writes:

    Powell> Could anyone suggest a neat way to reformat the output of
    Powell> FactorInteger into a more usable print format?

    Powell> FactorInteger[n] gives a list of prime factors of the integer n
    Powell> in ascending order, together with their exponents viz:

  FactorInteger[900]    produces the list   {{2,2},{3,2},{5,2}}

    Powell> What I need is a function called say factorform[list] such
    Powell> that:

  Print[ factorform[{{2,2},{3,2},{5,2}}] ]  produces  2^2*3^2*5^2

    Powell> 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

    Powell> A procedural solution is possible, but I was unable to create a
    Powell> simple function which would give the desired result.

The function fa gives a String that will be printed as required:

      fa[1] = "1"
 
      fa[k_Integer] := StringDrop[StringJoin[FactorInteger[k] /. 
           {{n_, 1} :> StringJoin[ToString[n], "*"], 
            {n_Integer, m_} :> StringJoin[ToString[n], "^", ToString[m], "*"]}], -1]

For example:

In[3]:= fa[900]

Out[3]= 2^2*3^2*5^2

In[4]:= fa[997]

Out[4]= 997

In[5]:= fa[-98]

Out[5]= -1*2*7^2

In[6]:= fa[9]

Out[6]= 3^2

In[7]:= fa[842]

Out[7]= 2*421

In[8]:= fa[9735]

Out[8]= 3*5*11*59




 ___________________________________________________________________________________
 Levent Kitis           lk3a at cars.mech.virginia.edu    lk3a at kelvin.seas.virginia.edu
 University of Virginia  Department of Mechanical, Aerospace and Nuclear Engineering  
 ___________________________________________________________________________________



  • Prev by Date: Re: FactorInteger Print Formatting
  • Next by Date: Re: FactorInteger Print Formatting
  • Previous by thread: Re: FactorInteger Print Formatting
  • Next by thread: Re: FactorInteger Print Formatting