tags:

views:

163

answers:

3

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?

+6  A: 

Yes, for example:

Flatten[Map[Table[#[[1]], {#[[2]]}] &, a]]
zvrba
Thanks, zvrba. You say "for example", that means you have other solutions like this? I'm just learning, so any code is welcome. Thanks again!
stevenvh
+4  A: 

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.)

dreeves
+2  A: 

You could also use:

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