RE: Unvaluable help to port SqlHierarchyId to .Net Core (Adam Milazzo on how the SQL Server hierarchyid data type works (kind of))
Unvaluable help to port SqlHierarchyId to .Net Core (anonymous on how the SQL Server hierarchyid data type works (kind of))
.: Converting repeating decimal numbers into proper fractions | 2015-09-17 05:39PM :.
Last night while lying in bed, I was thinking about how to convert repeating decimal numbers into proper fractions. For instance 4/7 = 0.571428. But if you see 0.571428, how can you turn it back into 4/7? (Remember, 0.571428 = 0.571428571428...) So I divided out 1/7 through 6/7 and interestingly they all have the same repeating digits (0.142857, 0.285714, 0.428571, 0.571428, 0.714285, and 0.857142, which are rotations of each other), but that didn't help me. (I've seen that rotation pattern with other numbers as well. It's fairly clear why it happens, but that's a digression.)Comments
So I thought about the simplest case: 0.1. Now if we want to convert this into a fraction p/q = 0.1, then we have q/p = 1 / 0.1. (I just tried this on a hunch.) Doing the division the long way, we figure that the first digit of the quotient is 9. Then we subtract 9 x 0.1 = 0.9, and since 1 - 0.9 = 0, we're done and the quotient is 9. Thus, q/p = 9 and p/q = 1/9. And indeed, 1/9 = 0.1. And since 0.2 = 0.1 x 2, 0.2 must equal 2/9. And in general, 0.X = X/9 for any single digit X.
Then I tried slightly harder cases. What about 0.10? Dividing 1 by 0.10, we again start with 9 in the quotient. That leaves a remainder of 1 - 0.90 = 0.09. (1 - 0.90 = 0.10, but then we have to borrow for the 0.0090, so we get 0.0910, but then we borrow for the 0.000090, and get 0.090910, and this pattern continues.) Then, continuing the division, 0.09 / 0.10 is "obviously" 9/10, so the full quotient is 9 and 9/10ths, or 99/10. That's q/p, and we invert it to get p/q: 10/99. And indeed, 10/99 = 0.10. I also tried 0.12 and got an answer of 4/33, which was correct.
So I had successfully converted several simple repeating numbers into fractions but wasn't confident that I could convert any repeating number through this method. I next tried 0.123 and after dividing got a quotient of 8.12195. That didn't look like an improvement! I could have tried inverting 0.12195, but I really had to get to sleep. I did it after waking up and got 41/5, and that inverts to 5/41, so the full quotient for 0.123 is 8 and 5/41sts, which is 333/41, so 0.123 therefore equals 41/333. I got the answer, but I still wasn't sure that the process would always terminate in a reasonable or even finite amount of time.
Then I saw the pattern! And a quick test through the division method confirmed that 0.01 = 1/99. Thus any two-digit repeating fraction 0.XY = XY/99! That fit: 0.12 = 4/33 = 12/99. And any three-digit repeating fraction is that number over 999, et cetera. (A quick check: 0.123 = 41/333 = 123/999.) So back to the original question, we can see that 0.571428 must simply be 571428/999999. But wait, does that really equal 4/7? 999999 is divisible by 7? Turns out it is. If 571428/999999 = 4/7, then 1/7 = (571428/4)/999999 = 142857/999999, and indeed, 999999/142857 = 7. So it's right. That must mean that every nonzero integer that could be the denominator in a fraction with an infinite decimal expansion evenly divides some number comprising one or more nines followed by zero or more zeros. That in itself is a rather interesting result. For instance 26 and 42 and 54 and 77 and most(?) other two-digit numbers all evenly divide 9999990. (9999990/26 = 384615 and 1/26 = 0.0384615 = 384615/9999990.)
For completeness, there are also repeating decimal numbers with some non-repeating portions. 1/26 = 0.0384615 was given as an example above. But these are easy once you have the repeating part figured out, since in general any nonnegative repeating decimal number W.ABXY equals W + (AB + XY / (10len(XY)-1)) / 10len(AB) (where len(x) is the number of digits in x). That looks complicated, but it's fairly simple. (It follows from the fact that 12.345 = 12 + 3 x 10-1 + 45 x 10-3.) Consider 2.3461538. We have W = 2, AB = 3 and XY = 461538, and len(3) = 1 and len(461538) = 6. So the whole thing is 2 + (3 + 461538 / (106-1)) / 101 = 2 + (3 + 461538 / (1000000-1)) / 10 = 2 + (3 + 461538/999999) / 10 = 2 + 3/10 + 461538/9999990 = 23461515/9999990 = 61/26. So 2.3461538 = 61/26. If the number is negative, use the absolute value and negate the result. And that's how ya dos it!
No comments yet.