Hi,

I'm trying to use the built in gcd method to calculate the gcd for two values. Unfortunately I can't seem to import the right module to get the method.

from fractions import Fraction
...
x = gcd(a,b)

Here you go:

# tested with Python 3.1.1

import fractions

a = 10
b = 25
cd = fractions.gcd(a, b)

sf = "the greatest common divisor of the integers %d and %d is %d"
print( sf % (a, b, cd) )

"""my result
the greatest common divisor of the integers 10 and 25 is 5
"""

Hey,

Thanks, worked like a charm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.