How to know the repeating decimal in a fraction?

后端 未结 5 1624
庸人自扰
庸人自扰 2020-11-29 09:42

I already know when a fraction is repeating decimals. Here is the function.

public bool IsRepeatingDecimal
{
    get
    {
        if (Numerator % Denominato         


        
5条回答
  •  忘掉有多难
    2020-11-29 10:03

    A very simple algorithm is this: implement long division. Record every intermediate division you do. As soon as you see a division identical to the one you've done before, you have what's being repeated.

    Example: 7/13.

    1. 13 goes into   7 0 times with remainder  7; bring down a 0.
    2. 13 goes into  70 5 times with remainder  5; bring down a 0.
    3. 13 goes into  50 3 times with remainder 11; bring down a 0.
    4. 13 goes into 110 8 times with remainder  6; bring down a 0.
    5. 13 goes into  60 4 times with remainder  8; bring down a 0.
    6. 13 goes into  80 6 times with remainder  2; bring down a 0.
    7. 13 goes into  20 1 time  with remainder  7; bring down a 0.
    8. We have already seen 13/70 on line 2; so lines 2-7 have the repeating part
    

    The algorithm gives us 538461 as the repeating part. My calculator says 7/13 is 0.538461538. Looks right to me! All that remains are implementation details, or to find a better algorithm!

提交回复
热议问题