+ 1
Can this code be shortened?? Also any other ideas are welcome.. thanks!!
3 Answers
+ 5
your formula is wrong.
it gives an error message that gcd was not defined.
Even if you initialize
gcd=1
in your code, it is not really computing the gcd.
Try it for 45 and 60. The result should be 15 but yours print 0 .
you could use
a = int(input("Enter a number!\n"))
b = int(input("Enter another number! \n"))
gcd = 1
for i in range(1, max(a,b)):
if a % i ==0 and b % i ==0:
gcd = i
print("The greatest common divisor is:", gcd)
or just use
import math
print(math.gcd(int(input()),int(input()))
+ 2
you can do this
import math
a, b = int (input ()),int (input())
print(math.gcd (a, b))
or
gcd = lambda a,b : a if not b else gcd(b, a%b)
print(gcd(int(input()), int(input())))
+ 2
inp = [int(input()), int(input())]
print(max([int(i) for i in range(1, max(inp[0], inp[1])) if (inp[0]%i+inp[1]%i)==0]))