TITLE: Integer division: the length of the repetitive sequence after the decimal point 
QUESTION [6 upvotes]: When dividing two integers, there may be an infinite sequence of digits after the decimal point (e.g. in the cases of 1/3, 1/7 etc).
As far as I know, if the two numbers divided are integers, this infinite sequence will be, from some point, a repetition of a some finite sequence (this may be also true to dividing rational numbers and not just integers, but I'm keeping it simple for now).
Take for example 1/7, it is equal to 0.14285714285714285714285714285714... 
The sequence repeating itself is 142857, a sequence of length 6.
Another example is 1/31, which is equal to 0.032258064516129032258064516129032...
The sequence repeating itself is 032258064516129, a sequence of length 15.
Some divisions do not immediately start with the repeating sequence, e.g. 3/14 which equals 0.21428571428571428571428571428571... (starting with 2, then followed by repetitions of 142857, a sequence of length 6).
Finally, after this long speech, here comes a question that bothers me for a long time: is it possible to find out the length of the repeating sequence using the 2 numbers being divided?
I'm looking for a function that receives a numerator and a denominator as parameters and returns the length of the repetitive sequence. Examples for input+output:
F(1,7) = 6
F(1,31) = 15
F(3,14) = 6
F(1,3) = 1

Does anyone know if this is possible? And if so - how to achieve it?

REPLY [2 votes]: The length of the period of a fraction is indifferent to the numerator unless the numerator and the denominator are not in lowest terms (in which case, reduce them to apply the following method).
However, when they are in lowest terms, take the denominator and factor out all factors of the base you're in, as they change nothing (like 2s and 5s in base 10, 3s and 7s in base 21, etc).  Then, the length of the period is equal to n, where n is the smallest integer such that your denominator divides bn-1 (where b is your base).  So, for example, 1/7 = 0.142857..., and the smallest 10n-1 such that 7 is a factor is 7 is 106-1 = 999999, and 999999/7 = 142857, which is, incidentally, your repeating portion.  The same thing even works for, say, 1/2 -- the smallest n is 0, because 100 - 1 = 0, and all non-zero integers divide 0.  This is one of the methods for factoring repunits.  Note that n will always be less than the denominator.
Similarly, 1/147 (which is 1/11 in base 10) = 0.0431162355..., and the smallest n such that 7n-1 is a multiple of 11 is 10 (710 - 1 = 282475248, and 282475248 / 11 = 25679568).
Multiplying by a constant does nothing to change the period, though it will change the numbers themselves.  Interestingly, though, when the period n is equal to 1 less than the denominator (called "max" periodic length), multiplying the fraction by a constant (other than a multiple of the denominator itself) will actually produce a cycle of the period.  2/7 = 0.285714..., 3/7 = 0.428571..., 4/7 = 0.571428..., etc.
Edit: When you divide by factors of the base, let the whole number by which you divide be f.  Then, $\lceil \log_{base}f\rceil$ (ceiling function) is equal to the number of non-repeating places before the repeating portion of the decimal.  Thus, 1/6, divided by 2, yields 1/3 (1/6 has the same repeating length as 1/3), and we divided by 2.  $\lceil\log_{10}2\rceil$ = 1, and indeed, 1/6 = 1.1(6...), with 1 non-repeating decimal.
Furthermore, for fraction m/n, $\lfloor\log_{base}n\rfloor$ is the number of leading zeroes in the repeating portion of the decimal.