Computing the Greatest Common Divisor

Category: Python Basics

Write a Python program to compute the greatest common divisor (GCD) of two numbers. The program should take two integers as input and output the GCD.

Example:

Input: 54, 24 

Output: 6

The GCD of two numbers is the largest number that divides both of them without leaving a remainder. One way to compute it is by using the Euclidean algorithm.

def gcd(a, b):
while(b):
a, b = b, a % b
return a

print(gcd(54, 24)) # Outputs: 6

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!