Tony
Tony

Reputation: 43

Python GCD - errors

I have an issue with my code whose purpose is to find the GCD of two inputs. When I try to run the module it tells me that 'gcd' is not defined.

def GCD(12,4):
    gcd = 1
    for i in range(2, max(12,4)/2):
        if((12 % i == 0) and (4 % i == 0)):
            gcd = i
    return gcd

Upvotes: 1

Views: 1485

Answers (2)

Praveen Dinelka
Praveen Dinelka

Reputation: 91

You can use Euclid division algorithm to find gcd in less time. Take floating point numbers to a and b.

def gcd(a,b):
    c = 1
    a,b = max(a,b),min(a,b)
    while c != 0:
        c = a%b
        a,b = b,c
    return a
print gcd(12,5)

Upvotes: 1

Bhargav Rao
Bhargav Rao

Reputation: 52151

You are not calling the GCF function. You have just defined your function. You need to add a line

gcf = GCF(a,b)

after the place where you accept the input. That is after b = int(input('denomenator: '))

Edit:

Change the input statements to

a = float(input('numerator: '))
b = float(input('denomenator: '))

Upvotes: 2

Related Questions