Posted by Joćo Dias on 1999-05-12
My idea is this one: Let's divide 1 by 14. We can't divide 1 by 14, so we multiply it by 10 and put 0, in the quotient. Hey, we can't also divide it, so we can say: 10-0: remainder 10, we put digit 0 at quotient. By multiplying it by 10,we get 100. Divided by 14, it leaves remainder 2 and quotient 7: 2-7. If we proceed, we get: 10-0 2-7 6-1 4-4 12-2 8-8 10-5 Hey! The last remainder already appeared! What that means? That means that after that 5, the pattern will repeat herself at infinite. 714285714285... or, 1/14=0,0(714285) The general computer algorithm is: 0. Initialize two arrays: remainders and quotients. These can be malloc'ed or fixed, with, let's say... 1000. Call a integer step = 0. 1. Make sure that p/q is not exact, by factoring in primes both p and q. This is done easily with a database of primes. 2. Take off the integer part of the number, and print it plus a decimal point. We'll be working now with the fractionary part. 3. Multiply it by 10, and calculate the remainder and quotient. 4. Check: The remainder is already at the table? 4.Yes. Then call c the index of the repeated element. All digits before c, including c, are from the non-periodic part. The digits that follow c , excluding c, are from the periodic part. 4.No. Then store at remainder[step] and quotients[step] the remainders and quotients. step = step + 1. Jump to 3. I hope it didn't stay too confuse or it doesn't contains any error. If yes, I can translate it to clearer C++, actually, because now I'm sleepy and I have other e-mail to reply. Truly, Fabio
Previous post | Next post | Timeline | Home