2
I’m solving this URI problem which basically calls for the maximum common divide between two values to be found.
I originally resolved with the math.gcd
in half a dozen lines, however, the code was not accepted as the URI uses a version of Python 3 prior to the inclusion of gcd
. Well, so I solved the problem in another way:
N = int(input())
monte, maior = [], 0
for x in range(N):
A, B = input().split(' ')
A, B = int(A), int(B)
for x in range(A, 1, -1):
if A % x == 0:
monte.append(x)
for x in range(len(monte)):
if B % monte[x] == 0:
maior = monte[x]
break
print(maior)
The outputs are all correct, but problem is my code is being refused for exceeding the time limit.
I ask: How can I improve the same? I’ve wiped everything I can, but it still exceeds the time limit. Any suggestions?
See if any of these solutions help you: How to implement a recursive MDC calculation algorithm in Python?
– Woss