I have been struggling for a while to try to code a program to convert any fraction 1/n to a repeating decimal. So far, my program works only for numbers that end in 1, 3, 7, or 9 (n cannot divide 2 or 5, since those numbers divide 10, our numeral base). Here is my program: click here

What the program does is it finds a long string of 9s that is divisible by n. So for 1/7, you keep looking through strings of 9s until you get 999999, which is divisible by 7. When you divide that by 7, you get 142857, so thus, 1/7 is equal to 0.142857 with all digits after the 0. repeated. I have found a lot of really cool things with this program. You may know that 1/81 is 0.012345679... without the 8. However, 1/998001 (999^2) is equal to 0.000001002003004005...995996997999 (without the 998). That's really cool!

But there's more. Any fraction of the form 1/(9^n) is exactly 9^(n-1) digits long! So 1/59049 has exactly 6561 digits before repeating. And for 1/(7^n), the decimal expansion has exactly 6*(7^(n-1)) digits. So 1/49 has exactly 42 digits. Cool! Another thing I discovered is that 1/n CANNOT have more than n-1 digits in its decmial (can anyone prove this?)

While some numbers have huge decimal expansions (1/225983 has 225982 digits, that's the largest decimal expansion I have discovered so far), there is something cool. 1/7 is rather long for its size, but numbers like 1/7777777777777777777777777 actually aren't long at all! If you changed a digit in that string of sevens, you would encounter complete and utter chaos, as you could imagine.

The program is really cool and can discover a lot of cool things, but, the program does not work for values of n that are divisible by 2 or 5 since obviously you can never have a long string of 9s divisible by either of those numbers. Obviously, an even number ends in 0, 2, 4, 6, or 8, and numbers divisible by 5 end in 0 or 5. So your long string of 9s does not work!

Can anyone help me improve my program to work for all values of n? I have a feeling that I should divide the value of n until it is not divisible by 2 or 5 anymore. I tried that, and the program terminated, but not with the correct values. Can someone help me make it work?

Thank you!


3 Answers 3


Let the "dividend" $d$ be the number $1$. Write a zero to the right of $d$. Compute $\left\lfloor\dfrac dn\right\rfloor$, the next digit, and $d\bmod n$, the next dividend, which is certainly less than $n$.

Write a zero to the right of the dividend and so on...

$$1:7\\1\to10=1\times7+3\\3\to30=4\times7+2\\2\to20=2\times7+6\\6\to60=8\times 7+4\\\cdots$$

Stop when the same remainder appears.

This will work for any $n$. If at some stage the remainder is $0$, you can also stop: the number has a finite fractional part.



Multiply $\frac1n$ by $10$ several times until you get a number $\frac{10^k}n$ such that, when expressed as $\frac ab$ with $\gcd(a,b)=1$, the number $b$ is prime with both $2$ and $5$. Then divide the result by $10^k$.

For instance, suppose that you have $\frac1{350}$. Then$$\frac{100}{350}=\frac27=0.285714285714\ldots$$and therefore$$\frac1{350}=0.00285714285714\ldots$$


Update: Worked on the program's comments to make it easier to understand.

Joseph Eck pointed out the 'math-is-fun' site in his comment to the OP.

The OP wanted help with his python program, and I say to myself:

What could be more fun than working on python and math together?

Python Program:

import sys
from fractions import Fraction

while 1:
    n = input("Enter numerator (0 to EXIT): ")
    if int(n) == 0: sys.exit()
    d = input("Enter denominator: ")
    if int(d) <= int(n):
        print("numerator must be smaller than denominator!")
    Start_Fraction = Fraction(int(n), int(d))
    r = Start_Fraction
    decExp = ''
    processedFractionList = []
    while 1:
#                                  # r < 1 
#                                  # so check if we've
#                                  #   'been there, done that'
#                                  # decimal now repeating?
            print('Fraction: ', Start_Fraction, '||| Expansion:', '0.' + decExp, "; Exp Length =",len(decExp), '; Repeat Start Index (offset) =', processedFractionList.index(r), "; Repeating length =",len(decExp)-processedFractionList.index(r))
#                                  #   Yes? - then break loop
        except ValueError:
#                                  # We check this 1st entry as 'processed'
#                                  # but we must also check off the
#                                  # "10 * r < 1" block

#                                  # we now process this r
        while 1:
            r = 10 * r
            if r < 1:
#                                  # 2-step update process
#                                  # check off this "10 * r < 1" entry
                decExp = decExp + '0'
#                                  # r >= 1 is True
        d = 0
        while 1:
            if r >= 1: 
                r = r - 1
                d = d + 1
#                                  # r < 1 is True
#                                  # 1-step update processs
        decExp = decExp + str(d)
#                                  # Is the expansion process now terminated?
        if r == 0:
            print('Fraction: ', Start_Fraction, '||| Expansion:', '0.' + decExp, "; Exp Length =",len(decExp))
        if len(decExp) > 5000:
            print("taking too long!")
            print('Fraction: ', Start_Fraction, '||| Expansion:', '0.' + decExp, "; Exp Length =",len(decExp))

I input the OP's $\text{1}$ over $\text{998,001}$ and this came out (use horizontal scrolling):

Enter numerator (0 to EXIT): 1
Enter denominator: 998001
Fraction:  1/998001 ||| Expansion: 0.000001002003004005006007008009010011012013014015016017018019020021022023024025026027028029030031032033034035036037038039040041042043044045046047048049050051052053054055056057058059060061062063064065066067068069070071072073074075076077078079080081082083084085086087088089090091092093094095096097098099100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997999 ; Exp Length = 2997 ; Repeat Start Index (offset) = 0 ; Repeating length = 2997
Enter numerator (0 to EXIT): 

The program just kept running when I entered the OP's $\text{1}$ over $\text{225,983}$. It is still running today! (lol)

  • $\begingroup$ Thank you so much, the program worked! I am very grateful :D And it works for every fraction! But is there any way to make it work faster? since after running both programs a few times i realized that mine was a little bit faster (that's probably why the 1/225983 never terminated). $\endgroup$
    – eners49
    Jul 12, 2018 at 13:50
  • $\begingroup$ Sounds like your program doesn't terminate with $1/225983$ either. If it does, include the code in your question so I can easily copy/paste/experiment with it. $\endgroup$ Jul 12, 2018 at 14:24
  • $\begingroup$ from math import log10 n, i = int(input("Enter a fraction, and it will be converted to a repeating decimal: 1/")), 9 while True: if i % n == 0: break i = (10 * i) + 9 i = i * (2 ** extras[0]) * (5 ** extras[1]) i = "0." + (int(log10(n))) * "0" + str(i // n) print("Your decimal: " + i + ". All " + str(len(i) - 2) + " digits after the \"0.\" are repeated.") you can look at the program by clicking on a link above in the problem description $\endgroup$
    – eners49
    Jul 13, 2018 at 17:09
  • $\begingroup$ My program does terminate with 1/225983, except it takes a really long time. 10^225983, as you can imagine, takes a while to calculate for even a computer, and that kind of number takes up a lot of space! Numbers with under 10,000 decimal places, however, are far easier to compute since 10^10,000 doesn't lag out a computer. $\endgroup$
    – eners49
    Jul 13, 2018 at 17:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .