The longest repeating decimal that can be created from a simple fraction What's the longest possible repeating decimal (repetend) that can be created from a fraction if:


*

*The numerator has to be less than or equal to 9,999

*The denominator has be less than or equal to 9,999?


I know the repeating decimal part can't exceed the denominator - 1. So the longest possible repeating decimal part has to be 9,998 or less.
The reason I want to know is to test an algorithm that I wrote which accepts fractions with numerators and denominators up to 9,999. The largest repeating decimal part I was able to create so far was 1/97 which equaled 0.[01030927 83505154 63917525 77319587 62886597 93814432 98969072 16494845 36082474 22680412 37113402 06185567] (96 repeating digits).
 A: This question is a variation of Problem 26 on Project Euler. Given the fact that the absolute value of the denominator can be no greater than 10,000, all irreducible fractions of the form n/9967 will have a digital representation with the longest possible repetend  of 9966 digits. This is because 9967 is the largest full repetend prime smaller than 10,000.
The following C++ program can be used find the denominator, that is no greater than a certain value, of an irreducible fraction whose decimal expansion will have the longest possible repetend:
#include <iostream>
#include <tuple>
using std::cout;

std::tuple<int, int> maxRepetendDenom(int dMax)
{
    int d, rep = 0;
    int i, j, value, counter;
    for (i = 3; i <= dMax; i += 2) {
        counter = 1;
        value = 10%i;
        j = dMax;
        while (value != 1 && j > 0) {
            value *= 10;
            value %= i;
            counter++;
            j--;
        }
        if (counter > rep && j > 1) {
            rep = counter;
            d = i;
        }
    }
    return {d, rep};
}

int main()
{
    while (true) {
        int d, rep, dMax;
        cout << "Enter the maximum allowed absolute value of the denominator: ";
        std::cin >> dMax;
        if (dMax <= 0) {
            return 0;
        }
        if (dMax < 3) {
            cout << "No fraction with an absolute value of the denominator"
                 << " less than 3 can have a repetend.\n\n";
        } else {
            std::tie(d, rep) = maxRepetendDenom(dMax);
            std::string digits = rep > 1 ? " digits)" : " digit)";
            cout << "Fractions with an absolute value of the denominator no greater"
                 << " than " << dMax << " will have the longest possible repetend "
                 << "(of " << rep << digits << " in their decimal expansion if they"
                 << " are in lowest terms and their denominator is " << d << ".\n\n";
        }
    }
    return 0;
}

You can use GDB Online to run the program.
A: Several answer indicate that you must look for the largest full-repetend prime lower than your upper limit for the denominator. Since every number that is coprime to the base (but not necessarily prime) has a repetend and not every prime has a full repetend it is possible that a non-prime will hold the distinction of having the longest repetend in that range.
In base 10 only the squares 289 and 361 have the longest repetend up to their value...at least up to $10^8$. So for limits above 361 (and certainly below $10^8$ but only with diminishing probability at higher values) the "full repetend prime" is the right answer.
But in general it appears that one must check all numbers that are coprime to the base and not only primes. For example, in base 8 (below 11467) the repetend for $107^2$ with length 5671 is the longest.
A: The numerator doesn't matter (for this question), so you might as well let it be $1$. The denominator should be the largest prime under $10000$ which has $10$ as a primitive root.  I don't know offhand what that prime is, but I'm sure such primes are tabulated and shouldn't be hard to locate. 
The table at the Online Encyclopedia doesn't go far enough. There is an applet which claims to find these primes, but I couldn't make it work --- maybe you'll have better luck.  
A: You can find more details on on my main web page at “engert.us/erwin/Miscellaneous.html” where I have a listing of repeating digits for all odd numbers under 28,000. I also have a listing of prime numbers with how many repeating digits hey have in “engert.us/erwin/miscellaneous/Reciprocals%20of%20prime%20numbers.pdf. For your interest here a just a few of the numbers under 9,999 they are:
1/9871, 4935 
1/9883, 4941 
1/9887, 9886 
1/9901, 12 
1/9907, 4953 
1/9923, 4961 
1/9929, 1241 
1/9931, 9930 
1/9941, 1988 
1/9949, 9948 
1/9967, 9966 
1/9973, 554 
Erwin
