Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

how to find gcd of two numbers in python

import math
# Using the built-in gcd method
gcd_built_in = math.gcd(10, 5)
print("GCD:", gcd_built_in)  # GCD: 5
# Creating a new gcd method
# implementing Euclid's algorithm

def gcd_euclid(num1, num2):
    if(num1 == 0):
        return num2
    if(num2 == 0):
        return num1
    while(num1 != num2):
        if(num1 > num2):
            num1 = num1-num2
        else:
            num2 = num2 - num1
    return num1

gcd_euclid = gcd_euclid(3, 7)
print("GCD:", gcd_euclid)  # GCD: 1
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #find #gcd #numbers #python
ADD COMMENT
Topic
Name
6+5 =