# Find the GCD and LCM of a list of numbers [duplicate]

Note: most of the questions that are already in existence about this topic only deal with two numbers as inputs. This question deals with any number (>1) of inputs.

## GCD

The GCD (greatest common divisor) of a list of integers is the largest integer which divides all of the numbers in the list.

For example:

• $$\gcd(9, 12, 15) = 3\$$
• $$\gcd(25, 75, 95) = 5\$$
• $$\gcd(5, 7, 9) = 1\$$

## LCM

The LCM (lowest common multiple) of a list of integers is the smallest integer which can be divided by all of the numbers in the list.

For example:

• $$\lcm(2, 3, 4) = 12\$$
• $$\lcm(5, 7, 9) = 315\$$
• $$\lcm(10, 15, 21) = 210\$$

You need to find the GCD and LCM of a list of integers which you will take as input from the user.

As this is a challenge, you can take the input in any format, and output in any format.

Here are some test cases:

Input                 Output
1 2 3 4               1 12
10 20 30 40           10 120
7 9 11 13             1 9009
2 3 5 7 11 13 17 19   1 9699690
2 4 6 8 10            2 120

• this still seems like a trivial extension of the other challenges. Multiple numbers just adds a loop. Sep 8 at 16:17
• @Razetime - the only question I could find that had both GCD and LCM was GCD / LCM Polyglots!, but that is a Polyglots challenge, so this is a lot different from that, leaving aside the fact that this is about multiple numbers, not just two. Sep 8 at 16:19
• I downvoted because there already is a challenge for the LCM of two numbers and a challenge for the GCD of two numbers. Joining them or having multiple numbers does not make this any more interesting, in my opinion Sep 8 at 16:46
• I've closed this as a duplicate of the LCM and the GCD challenges. I don't believe the challenge is distinct enough just by requiring both, or by applying to a list of numbers instead of a pair Sep 8 at 16:49
• I'd recommend against deleting the challenge: for one, deleting challenges decreases your "helpful questions" rate. But, more importantly, there's nothing wrong with having a question closed. It happens to all of us. For future reference, I'd recommend posting challenge ideas in the Sandbox on meta first, so that people can give feedback before posting Sep 8 at 17:02

# Ruby, 35 bytes

->l{[:gcd,:lcm].map{|x|l.reduce x}}


Try it online!

• Ruby3, -2b, f=->l{[:gcd,:lcm].map{l.reduce _1}} Sep 8 at 17:02
• Ruby3, alt 33b, ->l{%i[gcd lcm].map{l.reduce _1}} Sep 8 at 17:03

# Numlang, 8 bytes

$;Gy;OCy  ### Explanation $;Gy;OCy
\$;          // Take input from user (implicitly store in y)
Gy;       // Get the GCD of y (implicitly store in l)
O      // Output the last used variable (l)
Cy    // Get the LCM of y (implicitly store in l)
// Implicitly output the last used variable (l)


# Python 3.9, 44 bytes

lambda l:(gcd(*l),lcm(*l))
from math import*


In Python 3.8 and lower, gcd and lcm only accept 2 arguments. This was changed in 3.9, so this answer only works on 3.9+. Try it online does not have the needed version, so I could not post a link to it.

• FYI, you can use ATO (Attempt This Online) meant for golfing and has the latest version of most common languages. Sep 8 at 18:55