Glasses_
Glasses_

Reputation: 17

Find GCD of 2 numbers

What is wrong here?

The loop conditions should be fulfilled but the program is not entering the loop.

a=100
b=55
c = min(a,b)
while (a % c !=0 // b % c != 0):
    c = c - 1
    gcd = c
print(gcd)

EDIT: I'm dumb.

Upvotes: 2

Views: 136

Answers (2)

Ben Grossmann
Ben Grossmann

Reputation: 4772

Not strictly what you're asking about, but the far more efficient way to compute the gcd is with the Euclidean algorithm. Also, gcd = c does not need to be inside the while loop.

Here's some alternative code.

a = 100
b = 55

while b != 0:
    a, b = b, a % b
gcd = a
print(gcd)

Upvotes: 1

Abhinav Mathur
Abhinav Mathur

Reputation: 8101

// is the floor division operator. You need to use or for the logical OR function. Here is a simple article on basic Python operators.

a=100
b=55
c = min(a,b)
while (a % c !=0) or (b % c != 0):
    c = c - 1
    gcd = c
print(gcd) # prints 5

Upvotes: 3

Related Questions