Alternate form of FactorInteger? (Mathematica)

In Mathematica

a = FactorInteger[44420069694] 

Appoints

 {{2, 1}, {3, 1}, {7, 1}, {11, 2}, {13, 1}, {23, 2}, {31, 1}, {41, 1}} 

to a. Now, instead of factors with their indicators, I would like each of these lists to expand. The above factorization will then become

 {2, 3, 7, 11, 11, 13, 23, 23, 31, 41} 

I wrote the following function:

 b = {}; Do[Do[b = Append[b, a[[i]][[1]]], {a[[i]][[2]]}], {i, Length[a]}] 

but if you ask me, it looks awkward. There must definitely be a tidier way to achieve this?

+6
wolfram-mathematica
source share
5 answers

Another way in Mathematica 6 or later.

B: = Flatten[ConstantArray @@@ a]

Output = {2, 3, 7, 11, 11, 13, 23, 23, 31, 41}

even shorter:

Join @@ ConstantArray @@@ a


Comparison of speed of submitted methods

Using these functions (in the order they are placed):

 zvrba = Flatten[Map[Table[#[[1]], {#[[2]]}] &, #]] &; dreeves = Sequence @@ Table[#1, {#2}] & @@@ # &; gdelfino = Flatten[# /. {p_, n_} :> Table[p, {n}]] &; mrwizard = Join @@ ConstantArray @@@ # &; sasha = Function[{p, e}, Array[p &, e, 1, Sequence]] @@@ # &; 

and assigning them the letters Z, D, G, M, S, respectively, here are graphs of their effectiveness.

Firstly, to increase the number of input lists:

enter image description here

Secondly, to increase the indicator (repetition length) in each list:

enter image description here

Please note that these diagrams are logarithmic. The lower the better.

+7
source share

Yes, for example:

 Flatten[Map[Table[#[[1]], {#[[2]]}] &, a]] 
+9
source share

Here is another way to do this:

 rptseq[x_, n_] := Sequence @@ Table[x, {n}] rptseq @@@ a 

Who can be condensed using the lambda function:

 Sequence @@ Table[#1, {#2}] & @@@ a 

Zvrba's answer can also be a bit compressed if you go into things like this:

 Flatten[Table[#1, {#2}]& @@@ a] 

(Now when I look at this, I assume that my version is a very minor option for zvrba's.)

+6
source share

You can also use:

 a /. {p_, n_} -> Table[p, {n}] // Flatten 

UPDATE 2017/10/18 :

My answer above fails β€œin the case of two different simple factors,” as Corey Walker points out. This update fixes this:

 a /. {p_Integer, n_Integer} -> Table[p, {n}] // Flatten 

note that the control parameter executed by Mr Wizard was performed with the original version prior to this update.

+4
source share

You can also use an array to process the response. Here is a short code:

 In[11]:= PrimeFactorInteger[i_Integer] := Function[{p, e}, Array[p &, e, 1, Sequence]] @@@ FactorInteger[i] In[12]:= PrimeFactorInteger[2^3 3^2 5] Out[12]= {2, 2, 2, 3, 3, 5} 
+3
source share

All Articles