5
$\begingroup$

As a simple example, there is a list of prime factors.

{{2, 1}, {3, 2}, {43, 5}, {26684839, 1}}

How to combine them to the number 70612139395722186 using Mathematica?

$\endgroup$

6 Answers 6

11
$\begingroup$
Times @@ Power @@@ {{2, 1}, {3, 2}, {43, 5}, {26684839, 1}}
$\endgroup$
1
4
$\begingroup$
Times @@ (#[[1]]^#[[2]] & /@ {{2, 1}, {3, 2}, {43, 5}, {26684839, 1}})
$\endgroup$
4
$\begingroup$

Also

pf = {{2, 1}, {3, 2}, {43, 5}, {26684839, 1}};

Inner[Power, ## & @@ (pf\[Transpose]), Times]
(* 70612139395722186 *)
$\endgroup$
1
  • $\begingroup$ Equivalently, Inner[Power, pf[[All, 1]], pf[[All, 2]], Times]. $\endgroup$ May 4, 2015 at 4:21
4
$\begingroup$

Here is another one

primefactors = {{2, 1}, {3, 2}, {43, 5}, {26684839, 1}};

Fold[Times[#1, Power @@ #2] &, 1, primefactors]

70612139395722186

If the list gets larger enough, you can use ParallelCombine to MapReduce it,

ParallelCombine[Fold[Times[#1, Power @@ #2] &, 1, #]&, primefactors, Times]

For the list of current size, this actually only slows it down. And it can be used with any of the methods show here so far. If the method does it with as a function, f[#]& then:

 ParallelCombine[f[#]&, primefactors, Times]
$\endgroup$
3
$\begingroup$
Table[#1, #2] & @@@ lis // Map[Apply[Sequence]] // Apply[Times]

or

Times @@ (Sequence @@ Table[#1, #2] & @@@ lis)

or

Times @@ (Times @@ Table[#1, #2] & @@@ lis)

or

lis /. {a_, b_} :> a^b // Times @@ # &

Result:

70612139395722186

$\endgroup$
1
$\begingroup$
list = {{2, 1}, {3, 2}, {43, 5}, {26684839, 1}};

Two slot-free variants of Syed's solution:

Using Splice (new in 12.1)

Times @@ Splice @* Table @@@ list

70612139395722186

Using MapApply (new in 13.1)

Times @@ Flatten @ MapApply[Table] @ list

70612139395722186

$\endgroup$
1
  • $\begingroup$ For older versions not featuring MapApply: Times @@ Flatten@Map[Apply[Table]]@list $\endgroup$
    – Syed
    Mar 9 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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