I need some help to find my bugs in this function:
def is_coprime(num_1, num_2):
"""Function that returns true if 2 integers are coprime. """
if num_1 > num_2:
small = num_2
else:
small = num_1
for i in range(1, small + 1):
if ((num_1 % i == 0) and (num_2 % i != 0)):
gcd = i
if (gcd == 1):
return True
else:
return False
Hope the indentation isn't so bad. I think my mistake is in the if-condition, but I can't figure out what.
I would suggest this approach:
def divisors(x):
return {i for i in range(2, x//2 + 1) if not x % i}
def coprime(x, y):
return divisors(x).isdisjoint(divisors(y))
How does it work? divisors(x) returns the set f all divisors of a number greater than 1. The coprie(x, y) function check, if given sets are disjoint.