Repeating Decimals
Time limit1sMemory limit128 MB
Convert each repeating decimal with its repeating part in parentheses into a reduced fraction printed beside the input.
- Level
Medium4 of 10
- Topics
- Math, Number theory
- Solved
- No attempts yet
Problem
Any fraction can be written as a decimal. For example, is , and is , which is written more compactly as . The digits of stop, while the digits of repeat forever. Here are some fractions written as repeating decimals.
Given a repeating decimal, write a program that finds the fraction with the same value. The numerator and the denominator must be coprime.
Input
The input holds several test cases and continues to the end of the file. Each line contains one repeating decimal. The part that repeats forever is wrapped in parentheses, so 0.25(75) means . Each decimal contains at most 9 digits.
Output
For each test case, print the fraction with the same value as the given decimal on its own line. The format is given decimal = numerator / denominator, with one space on each side of the equals sign and of the slash. The numerator and the denominator must be coprime, and when the value is an integer the denominator is 1.