2022-12-20 17:57:02 +00:00
|
|
|
# Calcula o factorial de n, baseado na recorrencia n! = n*(n-1)!.
|
|
|
|
# Mas não termina! Detete a causa e corrija o erro.
|
|
|
|
def fact(n):
|
|
|
|
if n == 0:
|
|
|
|
return 1
|
2023-05-16 20:00:37 +00:00
|
|
|
return n * fact(n - 1)
|
2022-12-20 17:57:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Calcula o maximo divisor comum entre a e b.
|
|
|
|
# Baseia-se no algoritmo de Euclides.
|
|
|
|
# Mas não termina! Detete a causa e corrija o erro.
|
|
|
|
def gcd(a, b):
|
|
|
|
assert a > 0 and b > 0
|
2023-05-16 20:00:37 +00:00
|
|
|
if a % b == 0:
|
2022-12-20 17:57:02 +00:00
|
|
|
return b
|
2023-05-16 20:00:37 +00:00
|
|
|
return gcd(b, a % b)
|
2022-12-20 17:57:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2023-05-16 20:00:37 +00:00
|
|
|
print(fact(4)) # 24
|
|
|
|
print(fact(5)) # 120
|
2022-12-20 17:57:02 +00:00
|
|
|
|
2023-05-16 20:00:37 +00:00
|
|
|
x = 2 * 27 * 53 * 61
|
|
|
|
y = 2 * 2 * 17 * 23 * 53
|
2022-12-20 17:57:02 +00:00
|
|
|
print(x, y, gcd(x, y))
|
2023-05-16 20:00:37 +00:00
|
|
|
assert gcd(x, y) == 2 * 53
|
2022-12-20 17:57:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|