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!