DEADFR - Dead Fraction

Mike is frantically scrambling to finish his thesis at the last minute. He needs to assemble all his research notes into vaguely coherent form in the next 3 days. Unfortunately, he notices that he had been extremely sloppy in his calculations. Whenever he needed to perform arithmetic, he just plugged it into a calculator and scribbled down as much of the answer as he felt was relevant. Whenever a repeating fraction was displayed, Mike simply reccorded the first few digits followed by "...". For instance, instead of "1/3" he might have written down "0.3333...". Unfortunately, his results require exact fractions! He doesn't have time to redo every calculation, so he needs you to write a program (and FAST!) to automatically deduce the original fractions.

To make this tenable, he assumes that the original fraction is always the simplest one that produces the given sequence of digits; by simplest, he means the the one with smallest denominator. Also, he assumes that he did not neglect to write down important digits; no digit from the repeating portion of the decimal expansion was left unrecorded (even if this repeating portion was all zeroes).


There are several test cases. For each test case there is one line of input of the form "0.dddd..." where dddd is a string of 1 to 18 digits, not all zero. A line containing 0 follows the last case.


For each case, output the original fraction.




hide comments
Aradhya: 2012-07-08 13:11:28

this is one of the finest prb. of number theory!

:D: 2010-03-23 06:33:37

You're really expecting a lot for somebody to simply guess the problem in your code. Go to the forum->ProblemSet Archive and make a post there with you code pasted.

Somnath Singh :D: 2010-03-21 15:45:38

why am i getting thiss runtime error (SIGFPE) ???? Hw 2 fix it ????theere is no division by 0 problm then wht problm persists?? Help.....

Last edit: 2010-03-21 15:46:33
:D: 2009-09-02 11:11:41

"he the one with smallest denominator"

Vijay: 2009-07-18 01:20:56

If the input is "0.21...", "21" repeats or "1" repeats ?

Added by:Camilo Andrés Varela León
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Waterloo Local Contest Sep. 27, 2003