1
$\begingroup$

This question already has an answer here:

I have 2 lists of numbers and I want to quickly compute the array of answers to the LCM function for the 2 lists and a 3rd static number. I tried the following

list1 = {2,3,5,10};
list2 = {5,10,30,70};
LCM[37, #1, #2] & /@ [list1, list2]

but got a cryptic error message that I don't understand. Can anyone help or point me to the correct area?

Many thanks in advance

$\endgroup$

marked as duplicate by MarcoB, m_goldberg, Mr.Wizard Feb 4 '16 at 0:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 5
    $\begingroup$ Take a look at MapThread. $\endgroup$ – Kuba Feb 3 '16 at 21:01
  • $\begingroup$ Table came to the rescue. lcmResults = ParallelTable[LCM[x, y, 37], {x, list1}, {y, list2}]; $\endgroup$ – Alan Woodward Feb 3 '16 at 21:12
2
$\begingroup$

You can use Outer along with Map.

Map[LCM[37, Sequence @@ #] &, Outer[List, list1, list2], {2}]
(*
{{370, 370, 1110, 2590}, 
 {555, 1110, 1110, 7770}, 
 {185, 370, 1110, 2590}, 
 {370, 370, 1110, 2590}}
*)

Hope this helps.


Or as @SimonWood has pointed out in the comments.

Outer[LCM[37, ##] &, list1, list2]
$\endgroup$
  • 3
    $\begingroup$ Why not just Outer[LCM[37, ##] &, list1, list2] ? $\endgroup$ – Simon Woods Feb 3 '16 at 21:27
  • $\begingroup$ @SimonWoods Yes. You are completely correct. My solution-by-parts needs a convolution boost today. LOL!! $\endgroup$ – Edmund Feb 3 '16 at 21:30
  • $\begingroup$ LOL. I wondered if you were just determined to use Map as specified in the question title... $\endgroup$ – Simon Woods Feb 3 '16 at 21:33
  • $\begingroup$ @SimonWoods Perhaps subconsciously. :-P $\endgroup$ – Edmund Feb 3 '16 at 21:37
2
$\begingroup$

Example:

LCM[37, #[[1]], #[[2]]] & /@ Transpose[{list1, list2}]

or

MapThread[LCM[37, ##] &, {list1, list2}]

Output:

{370, 1110, 1110, 2590}

Reference:

& # /@ etc.
MapThread
Transpose

$\endgroup$

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