This is my program to determine the "greatest common denominator of two numbers"
integer1 = int(input("Enter an integer:"))
integer2 = int(input("Enter an integer:"))
if integer1 > integer2:
x = integer1
while integer1%x and integer2%x !=0:
x = x - 1
elif integer2 > integer1:
x = integer2
while integer1%x and integer2%x !=0:
x = x - 1
print("the gcd of",integer1,"and",integer2,"is",x)
for example, when I input the values "25" and "50", my program will say the GCD is 50, which is incorrect.
x = min(integer1, integer2)
while integer1 % x or integer2 % x:
x = x - 1
x
then apply your logic which doesn't change x
can start at the minimum value rather than the maximum x
to be accepted it should satisfy both integer1 % x == 0
and integer2 % x == 0
, so the stop condition of your while
should be integer1 % x != 0 or integer2 % x != 0
(and the !=0
are here redundant with int to bool conversion so you can skip them) a and b == 0
type of expression don't behave the way you probably think, the "correct" way is a == 0 and b == 0
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.