We all know that whenever a rational number is written in decimal, the result is either terminating or (eventually) periodic. For example, when 41/42 is written in decimal, the result is
0.9 761904 761904 761904 761904 761904 761904 761904 ...
with an initial sequence of digits 0.9
followed by the sequence 761904
repeated over and over again. (A convenient notation for this is 0.9(761904)
where the parentheses surround the block of repeating digits.)
Your goal in this challenge is to take a positive rational number, delete the first digit that's part of the repeating sequence, and return the resulting rational number. For example, if we do this to 41/42, we get
0.9 61904 761904 761904 761904 761904 761904 761904 ...
or 0.9(619047)
for short, which is 101/105.
If the rational number has a terminating decimal expansion, like 1/4 = 0.25
does, nothing should happen. You can think of 1/4 either as 0.250000000...
or as 0.249999999...
but in either case, deleting the first digit of the repeating part leaves the number unchanged.
Details
- The input is a positive rational number, either as a pair of positive integers representing the numerator and denominator, or (if your language of choice allows it and you want to) as some sort of rational-number object.
- The output is also a rational number, also in either form. The rational number you return must be fully simplified: the numerator and denominator should be relatively prime. If the result is an integer, you may return the integer instead of a rational number.
- If taking a pair of numbers as input, you may assume they're relatively prime; if producing a pair of numbers as output, you must make them be relatively prime.
- Be careful that you find the first digit that starts a repeating block. For example, one could write 41/42 as
0.97(619047)
but that doesn't make 2041/2100 (with the decimal expansion0.97(190476)
) a valid answer. - You may assume that in the input you get, the first periodic digit is after the decimal point, making
120/11
=10.909090909...
invalid input: (its first periodic digit could be considered the0
in10
). You may do anything you like on such input. - This is code-golf: the shortest solution wins.
Test cases
41/42 => 101/105
101/105 => 193/210
193/210 => 104/105
104/105 => 19/21
1/3 => 1/3
1/4 => 1/4
2017/1 => 2017/1
1/7 => 3/7
1/26 => 11/130
1234/9999 => 2341/9999