Kassidi Turner
Kassidi Turner

Reputation: 11

Euclidean algorithm - What am I doing? python john zelle

The greatest common divisor (GCD) of two values can be computed using Euclid's algorithm. Starting with the values m and n, we repeatedly apply the formula: n, m = m, n%m until m is 0. At that point, n is the GCD of the original m and n. Write a program that finds the GCD of two numbers using this algorithm.

I am using- What am I doing wrong?

def gcd(m, n):
  m=int(input("Enter the number for m:"))
  n=int(input("Enter the number for n:"))
    while m!=n 
     if m>n:
        m=m−n
     else:
        n=n−m
     return m

Upvotes: 0

Views: 425

Answers (1)

pho
pho

Reputation: 25489

Your problem is that you return m at the end of the first iteration of the while loop. You need to wait until the while m != n ends before returning.

While you're at it, lose the input() statements. You already gave your function the values of m and n that you want to use as arguments to the function.

def gcd(m, n):
    while m!=n 
        if m>n:
            m=m−n
        else:
            n=n−m
    return m

To run this, you'd ask for the values of m and n before you call the function, and then use those values.

a=int(input("Enter the first number:"))
b=int(input("Enter the second number:"))

print(gcd(a, b))

Upvotes: 1

Related Questions