Saturday 5 November 2022

gcd in Python

 The math.gcd() method returns the greatest common divisor of the two integers int1 and int2.

GCD is the largest common divisor that divides the numbers without a remainder.

GCD is also known as the highest common factor (HCF).

Tip: gcd(0,0) returns 0.


Example :

#Import math Library
import math

#find the  the greatest common divisor of the two integers
print (math.gcd(3, 6))
print (math.gcd(6, 12))
print (math.gcd(12, 36))
print (math.gcd(-12, -36))
print (math.gcd(5, 12))
print (math.gcd(10, 0))
print (math.gcd(0, 34))
print (math.gcd(0, 0))


Output :

  3
  6
  12
  12
  1
  10
  34
  0

Source : https://www.w3schools.com/python/ref_math_gcd.asp#:~:text=gcd()%20method%20returns%20the,the%20numbers%20without%20a%20remainder.

No comments:

Post a Comment