Dead Fraction
Time limit1sMemory limit128 MB
Given a truncated decimal with a repeating tail, find the fraction with the smallest denominator that produces the recorded digits.
- Level
Medium6 of 10
- Topics
- Number theory, Math
- Solved
- No attempts yet
Problem
Mike is racing against the clock to finish his thesis. Over the next three days he has to pull his research notes into some vaguely coherent shape, but he realizes he was extremely sloppy with his arithmetic. Every time he needed to compute something he punched it into a calculator and jotted down as much of the answer as he felt was relevant. Whenever the calculator displayed a repeating decimal, Mike wrote down only the first few digits followed by "...". For example, instead of "1/3" he might have written "0.3333...".
Unfortunately his results require exact fractions, and he has no time to redo every calculation. Write a program that recovers the original fraction from each recorded decimal.
To make this well-defined, assume the original fraction is always the simplest one that produces the recorded sequence of digits, where "simplest" means the one with the smallest denominator. Also assume Mike never dropped a digit of the repeating block: the trailing "..." means that some suffix of the digits he wrote repeats forever, and that repeating suffix was recorded in full (even if it consists entirely of zeros). Because the length of the repeating suffix is unknown, consider every possibility (the last , up to all of the recorded digits repeating) and keep the fraction with the smallest denominator.
Input
The input contains several test cases. Each test case is a single line of the form "0.dddd...", where dddd is a string of to digits that are not all zero. A line containing a single follows the last case and must not be processed.
Output
For each test case, output the recovered fraction on its own line, written as numerator/denominator in lowest terms.
Hint
Note that a terminating decimal has two repeating expansions (for example, ).