I changed the FactorInteger output to a format which is more suitable to my needs.

I used:

f[x_] := Array[x[[1]] &, x[[2]]]
fac[n_] := Flatten[Map[f[#] &, FactorInteger[n]]]


fac[24] yields {2,2,2,3}

Now, for presentation purposes only, I would like

{2^3, 3}

as the result of the function fac.

I tried several things such as:

HoldForm[2^3], which yields 2^3 as I would like, but

using the function

g[x_] := HoldForm[x[[1]]^x[[2]]] in fac instead of f

gives unexpected results.

How can I present the results of FactorInteger n as

{p1^a1, p2^a2, ... ,pn^an} i.e. ( 2^2, 5^3, 7 } ?
Inactive[Power] @@@ FactorInteger[24] /. Inactive[Power][a_, 1] :> a

enter image description here

Your fac[24] could be written as

Flatten[ConstantArray @@@ FactorInteger[24]]

enter image description here

To not show the ^ symbol:

Superscript @@@ FactorInteger[24] /. Superscript[a_, 1] :> a

enter image description here

  • $\begingroup$ Much better already but I don't want the ^ symbol in 2^3, I want the 3 lifted to the exponent place $2^3$ ( should have used Latex in question I suppose ). I tried adding //TraditionalForm but that did not work. $\endgroup$ – nilo de roock Jan 11 '16 at 21:53
  • $\begingroup$ See update ------------------------------------- $\endgroup$ – eldo Jan 11 '16 at 22:01

From the docs:

CenterDot @@ Superscript @@@ FactorInteger[7!]

enter image description here

Though CenterDot does not work well for prime powers:

CenterDot @@ Superscript @@@ FactorInteger[5]

enter image description here

If this is a problem, you could just define your own:

myCenterDot[e_] := e
myCenterDot[args__] := CenterDot[args]

Now it works fine:

myCenterDot @@ Superscript @@@ FactorInteger[5]

enter image description here

You could do something similar for Superscript if you don't want to see 1 in the exponents.

  • $\begingroup$ Or without the dot, and using Times only for formatting: Times @@ Map[Superscript[#[[1]], #[[2]]] &, FactorInteger[630]] $\endgroup$ – Christopher Lamb May 26 '19 at 4:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.