GCD and LCM in Python: Euclidean Algorithm and math Module

The greatest common divisor (GCD) is the largest positive integer dividing both inputs. The least common multiple (LCM) is the smallest nonnegative common multiple; Python defines LCM as 0 when either input is zero. The original page title uses LCD; the standard term here is LCM.

Interactive Companion Jupyter Notebook

Run the Python examples, compare expected outputs, modify the code, and complete guided exercises.

View on GitHub Open in Google Colab
On this page — Show page sections— Hide page sections

First working example

from math import gcd, lcm
a, b = 20, 12
print("GCD:", gcd(a, b))
print("LCM:", lcm(a, b))

Output:

GCD: 4
LCM: 60

Both functions are available in the standard math module in Python 3.9 or newer.

Recursive GCD: the original approach

Euclid’s algorithm repeatedly replaces (a, b) with (b, a % b) until the second value becomes zero.

def gcd_recursive(a, b):
    if b == 0:
        return abs(a)
    return gcd_recursive(b, a % b)
print(gcd_recursive(85, 20))

Output:

5

Iterative Euclidean algorithm

A loop avoids recursion and is the standard handwritten implementation.

def get_gcd(a, b):
    while b:
        a, b = b, a % b
    return abs(a)
for pair in ((85, 20), (54, 24), (-12, 18)):
    print(pair, get_gcd(*pair))

Output:

(85, 20) 5
(54, 24) 6
(-12, 18) 6

GCD of two user inputs

The original example asks for two integers. Here the result is safely positive.

from math import gcd
a = int(input("First number: "))
b = int(input("Second number: "))
print("GCD:", gcd(a, b))

Output:

GCD: 5

LCM using GCD

For nonzero inputs, abs(a*b) // gcd(a,b) computes LCM. Guard zero first to avoid division by zero.

from math import gcd
def get_lcm(a, b):
    if a == 0 or b == 0:
        return 0
    return abs(a * b) // gcd(a, b)
for a, b in ((20, 12), (0, 12), (-6, 8)):
    print(a, b, get_lcm(a, b))

Output:

20 12 60
0 12 0
-6 8 24

Use math.gcd() and math.lcm()

math.gcd() and math.lcm() handle signs and zeros. math.lcm() was introduced in Python 3.9.

from math import gcd, lcm
for a, b in ((0, 0), (0, 12), (-6, 8), (18, 24)):
    print((a, b), "gcd", gcd(a, b), "lcm", lcm(a, b))

Output:

(0, 0) gcd 0 lcm 0
(0, 12) gcd 12 lcm 0
(-6, 8) gcd 2 lcm 24
(18, 24) gcd 6 lcm 72

Multiple numbers

Modern Python can find the GCD and LCM of more than two arguments.

from math import gcd, lcm
print(gcd(18, 24, 30))
print(lcm(4, 6, 10))

Output:

6
60

Common mistakes and troubleshooting

  • Run each example in Python 3.9 or newer. Indentation is significant in loops and functions.
  • Remember that input() returns a string. Convert and validate the value before doing arithmetic.
  • Check boundary values such as zero, negative numbers, small inputs and empty ranges.
  • When comparing outputs, note whether a code sample uses fixed values or user input.

Practice exercises

Try GCD and LCM for (12,18), (0,24), (-6,8) and (0,0). Use the companion practice notebook for runnable examples and self-checks.

Frequently asked questions

Can GCD be negative?

math.gcd() returns a nonnegative result.

What is LCM of 0 and 12?

It is 0 in Python.

Does math.lcm() work in Python 3.8?

No. It was added in Python 3.9; use the guarded GCD formula on older versions.

Plus2Net Python Basics podcast playlist

Other related resources

Back to top ↑




Subscribe to our YouTube Channel here



plus2net.com




Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer