-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecimal_to_fraction.py
32 lines (24 loc) · 1 KB
/
decimal_to_fraction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def gcd(a, b):
while b:
a, b = b, a % b
return a
def decimal_to_fraction(decimal_str):
decimal_str = decimal_str.replace(',', '.')
if '.' not in decimal_str:
return f"The input '{decimal_str}' is not valid."
integer_part, decimal_part = decimal_str.split('.')
num_decimal_digits = len(decimal_part)
numerator = int(integer_part + decimal_part) - int(integer_part)
denominator = 10 ** num_decimal_digits - 1
# Simplifica a fração o máximo possível
divisor = gcd(numerator, denominator)
simplified_numerator = numerator // divisor
simplified_denominator = denominator // divisor
return f"fThe recurring fraction of the repeating decimal {decimal_str} is: {simplified_numerator}/{simplified_denominator}"
# Recebe o input do usuário
user_input = input("Digite uma dízima: ")
# Calcula a fração geratriz e imprime o resultado
fraction_result = decimal_to_fraction(user_input)
print(fraction_result)
print('')
input("Pressione Enter para sair...")