Repeating Decimal
INTRODUCTION
To convert a fraction to a decimal, you can, for example:
- extpand the denominator to 10, 100, 1000, etc.:
$\frac{1}{4}=\frac{25}{100}=0.25$
$\frac{7}{8}=\frac{875}{1000}=0.875$
$\frac{3}{5}=\frac{6}{10}=0.6$
- divide the numerator by the denominator
Tu ma byÄ grafika: https://mat.przemek.edu.pl/wp-content/uploads/2024/08/image-3.png
However, there are common fractions whose:
- the denominator cannot be expanded to 10, 100, 1000 âŚ,
- there is no end to dividing the numerator by the denominator
EXAMPLE
Fraction $\frac{2}{11}$ after dividing the numerator by the denominator, it has the form: 0,1818181818âŚ
We say about such fractions that:
- May infinite decimal expansion,
- And period of this expansion is a repeating sequence of digits â in the example above, the period is (18)
PYTHON CODE
HOW THE CODE WORKS?
- The program defines a function named âconvert_fractionâ that takes one argument, a string representing a fraction in the format of ânumerator/denominatorâ.
- Inside the function, it splits the input string into numerator and denominator variables using the â/â character.
- The numerator and denominator variables are then converted to integers.
- The program calculates the decimal value of the fraction by dividing the numerator by the denominator and storing it in a variable named âdecimalâ.
- The program calculates the remainder of the fraction by calculating the modulus of the numerator and denominator, and stores it in a variable named âremainderâ.
- The program initializes an empty dictionary called âremainders_seenâ and an empty list called ârepeating_digitsâ.
- The program enters a while loop that continues until the remainder is found in the âremainders_seenâ dictionary.
- Inside the while loop, the program adds the current remainder to the âremainders_seenâ dictionary with its index in the ârepeating_digitsâ list as the value.
- The program appends a string representation of the next digit in the repeating decimal sequence to the ârepeating_digitsâ list.
- The program calculates a new remainder by multiplying the current remainder by 10 and taking the modulus of the denominator.
- Once the while loop has completed, the length of the repeating sequence is determined by subtracting the index of the first occurrence of the remainder in the âremainders_seenâ dictionary from the length of the ârepeating_digitsâ list.
- If the remainder is zero, the program returns the decimal value of the fraction as a string.
- Otherwise, the program creates a string representation of the repeating decimal sequence and returns it along with the decimal value and the number of digits in the repeating sequence as a formatted string.
- The program prompts the user to enter a fraction in the specified format using the âinputâ function and stores the result in a variable named âfractionâ.
- The program calls the âconvert_fractionâ function with the user input as the argument and stores the result in a variable named âresultâ.
- The program prints the result to the console using the âprintâ function