• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / math gcd Python – Find Greatest Common Divisor with math.gcd() Function

math gcd Python – Find Greatest Common Divisor with math.gcd() Function

February 22, 2022 Leave a Comment

With Python, we can calculate the greatest common divisor of two numbers with the math gcd() function.

import math

print(math.gcd(10,25))

#Output:
5

The Python math module has many powerful functions which make performing certain calculations in Python very easy.

One such calculation which is very easy to perform in Python is finding the greatest common divisor (GCD) of two numbers.

We can find the GCD of two numbers easily with the Python math module gcd() function. The math.gcd() function takes two integers and returns the GCD of those two integers.

Below are some examples of how to use math.gcd() in Python to find the GCD of two numbers.

import math

print(math.gcd(10,25))
print(math.gcd(90,33))
print(math.gcd(85,1003))
print(math.gcd(74,46))

#Output:
5
3
17
2

How to Get the Greatest Common Divisor of a List in Python with gcd() Function

To find the GCD of a list of numbers in Python, we need to use the fact that the GCD of a list of numbers will be the max of all pairwise GCDs in a list of numbers.

To get the GCD of a list of integers with Python, we loop over all integers in our list and find the GCD at each iteration in the loop.

Below is an example function in Python which will calculate the GCD of a list of integers using a loop and the math gcd() function.

import math

def gcd_of_list(ints):
    gcd = math.gcd(ints[0],ints[1])
    for i in range(2,len(ints)):
        gcd = math.gcd(gcd,ints[i])
    return gcd

print(gcd_of_list([11,33,1100]))
print(gcd_of_list([582,404,1028]))
print(gcd_of_list([990,675,320]))

#Output:
11
2
5

Hopefully this article has been useful for you to understand how to use the gcd() math function in Python to find the greatest common divisors of a list of numbers.

Other Articles You'll Also Like:

  • 1.  Python Even or Odd – Check if Number is Even or Odd Using % Operator
  • 2.  Python Decrement Counter with -= Decrement Operator
  • 3.  How to Sort Numbers in Python Without Sort Function
  • 4.  Difference Between read(), readline() and readlines() in Python
  • 5.  Python Subtract Days from Date Using datetime timedelta() Function
  • 6.  Check if Word is Palindrome Using Recursion with Python
  • 7.  Python sin – Find Sine of Number in Radians Using math.sin()
  • 8.  How to Check if Variable is Defined in Python
  • 9.  Break Out of Function in Python with return Statement
  • 10.  Python Destroy Object – How to Delete Objects with del Keyword

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy