21
5
In the decimal representation of every rational number p/q, you have a periodic tail, a non-periodic head, and a section before the decimal point in the following format:
(before decimal point).(non-periodic)(periodic)
Some examples include:
1/70 = 0.0142857... = (0).(0)(142857)
10/7 = 1.428571... = (1).()(428571) ## no non-periodic part
1/13 = 0.076923... = (0).()(076923)
3/40 = 0.075 = (0).(075)() ## no periodic part
-2/15 = -0.13... = -(0).(1)(3) ## negative
75/38 = 1.9736842105263157894... = (1).(9)(736842105263157894)
## periodic part longer than float can handle
25/168 = 0.148809523... = (0).(148)(809523)
120/99 = 40/33 = 1.212121... = (1).()(21)
2/1 = 2 = (2).()() ## no periodic, no non-periodic
0/1 = 0 = (0).()()
0/2 = 0 = (0).()()
299/792 = 0.37752... = (0).(377)(52)
95/-14 = -6.7857142... = -(6).(7)(857142)
-95/-14 = 6.7857142... = (6).(7)(857142)
The challenge is to swap the periodic and non-periodic parts, leaving the before decimal point alone, to create a new number. For example:
25/168 = 0.148809523... = (0).(148)(809523)
=> (0).(809523)(148) = 0.809523148148... = 870397/1080000
If a number has no periodic part like 0.25 turn that number into a new periodic number, and vice versa.
1/4 = 0.25 = (0).(25)() => (0).()(25) = 0.252525... = 25/99
4/9 = 0.444444... = (0).()(4) => (0).(4)() = 0.4 = 2/5
5/1 = 5 = (5).()() => (5).()() = 5 = 5/1
The challenge
- Take a fraction
xas input, as a string, two inputs, a rational number, or whatever method suits your language. - Swap the periodic and non-periodic parts of the decimal representation of
xto create a new number, leaving the part before the decimal alone. The periodic part always starts as soon as possible so that the non-periodic part is as short as possible. Examples are below. - Return the swapped number as a new fraction. The input is not necessarily reduced though the output should be. Input format is allowed to differ from output format.
- The numerator
pofxwill be an integer with absolute value of one million or less and the denominatorqofxwill be a non-zero integer with absolute value of one million or less. - The numerator
rand denominatorsof the result is not guaranteed to be less than one million. Given the length of the periodic parts of these numbers, it is recommended that you avoid directly converting to floats. - This is code golf. Shortest answer in bytes wins.
Examples
1/70 = (0).(0)(142857) => (0).(142857)(0) = (0).(142857)() = 0.142857 = 142857/1000000
10/7 = (1).()(428571) => (1).(428571)() = 1.428571 = 1428571/1000000
1/13 = (0).()(076923) => (0).(076923)() = 0.076293 = 76923/1000000
3/40 = (0).(075)() => (0).()(075) = 0.075075... = 75/999 = 25/333
-2/15 = -(0).(1)(3) => -(0).(3)(1) = -0.311111... = -28/90 = -14/45
75/38 = (1).(9)(736842105263157894)
=> (1).(736842105263157894)(9) = (1).(736842105263157895)() ## since 0.999... = 1
= 1.736842105263157895 = 1736842105263157895/1000000000000000000
= 347368421052631579/200000000000000000
25/168 = (0).(148)(809523) => (0).(809523)(148) = 0.809523148148... = 870397/1080000
120/99 = (1).()(21) => (1).(21)() = 1.21 = 121/100
2/1 = (2).()() => (2).()() = 2 = 2/1
0/1 = (0).()() => (0).()() = 0 = 0/1
0/2 = (0).()() => (0).()() = 0 = 0/1
299/792 = (0).(377)(52) => (0).(52)(377) = 0.52377377... = 2093/3996
95/-14 = -(6).(7)(857142) => -(6).(857142)(7) = -6.857142777... = -12342857/1800000
-95/-14 = (6).(7)(857142) => (6).(857142)(7) = 6.857142777... = 12342857/1800000
There is a missing
0at the end of test case 2 (10/7):1428571/100000should be1428571/1000000. – JungHwan Min – 2016-12-11T21:30:54.3971As stated there won't be a unique answer for a given input.
1/7could be represented as(0).()(142857)or(0).(1)(428571),1could be represented as(1).()(),(0).()(9),(0).()(99),(0).(9)(9), etc. – ngenisis – 2016-12-11T23:40:13.257@ngenisis That was implicit in the examples, but I have made it explicit. Thanks for the feedback :) – Sherlock9 – 2016-12-12T02:17:26.117
@R.Kap I have already stated in the challenge that it is best to avoid using floats here. There are ways to find the decimal digits of a number without converting to a float. I hope this answers your question :) – Sherlock9 – 2016-12-12T02:20:37.720
can both p and q be negative? – edc65 – 2016-12-23T13:51:40.383
@edc65 Yes. I'll edit in a test case – Sherlock9 – 2016-12-23T13:54:32.473