Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative form of FactorInteger? (Mathematica)

In Mathematica

a = FactorInteger[44420069694]

assigns

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

to a. Now instead of the factors with their exponents I would like each of those lists expanded. The above factorization would 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 fugly. There sure must be a neater way to do achieve this?

like image 831
stevenvh Avatar asked Apr 18 '09 18:04

stevenvh


4 Answers

Yes, for example:

Flatten[Map[Table[#[[1]], {#[[2]]}] &, a]]
like image 176
zvrba Avatar answered Oct 25 '22 20:10

zvrba


Yet another way in Mathematica 6 or later.

In:= Flatten[ConstantArray @@@ a]

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

even shorter:

Join @@ ConstantArray @@@ a


A speed comparison of methods posted

Using the these functions (in the order they were posted):

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 Timing charts of their efficiency.

First, for increasing number of lists in the input:

enter image description here

Second, for increasing exponent (length of repetition) in each list:

enter image description here

Note that these charts are logarithmic. Lower is better.

like image 40
Mr.Wizard Avatar answered Oct 25 '22 21:10

Mr.Wizard


Here's another way to do it:

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

Which can be condensed with a lambda function to:

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

zvrba's answer can also be condensed a bit, if you're into that sort of thing:

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

(Now that I look at that, I guess my version is a very minor variant on zvrba's.)

like image 28
dreeves Avatar answered Oct 25 '22 19:10

dreeves


You could also use:

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

UPDATE 2017/10/18:

My answer above fails "in the case of two distinct prime factors" as pointed out by Cory Walker. This update fixes it:

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

notice that the benchmark done by Mr Wizard was done with the original version before this update.

like image 30
gdelfino Avatar answered Oct 25 '22 19:10

gdelfino