17
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. 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
Can we return
2017
instead of2017/1
? – JungHwan Min – 2017-12-17T01:13:51.377Yes, if you're doing the rational number thing. (If you're doing the pair-of-integers thing, then I'm not sure what else you'd return except the pair
(2017,1)
.) – Misha Lavrov – 2017-12-17T01:19:56.643May the input be reducible (not fully simplified)? For example, can
2/4
happen in the input? – user202729 – 2017-12-17T07:25:23.6171If input is
120/11
is the correct answer111/11
or210/11
? – kasperd – 2017-12-17T13:04:29.277@user202729 You may assume that the input is a fully simplified rational number. – Misha Lavrov – 2017-12-17T15:11:03.383
2@kasperd Huh, that's a case I hadn't thought of... I'd want to say
111/11
except that the most highly upvoted answer at the moment returns210/11
, so I will let you pick to avoid invalidating existing answers. – Misha Lavrov – 2017-12-17T15:14:58.580@MishaLavrov alternatively, you could specify that all inputs will be between 0 and 1. (It should only affect the
2017/1
case) – JungHwan Min – 2017-12-17T18:39:43.270@JungHwanMin There is an infinite number of rational numbers between 0 and 1 for which the question would also apply. I think the simplest example would be
10/11
. – kasperd – 2017-12-17T21:57:40.543@kasperd The zero (ones place) in
0.(90)
isn't significant, so including it in the repeating sequence wouldn't really make sense. – JungHwan Min – 2017-12-18T00:32:41.403