http://www.futilitycloset.com/2012/01/08/math-notes-76/
Here's the math. Suppose you want a unit fraction 1/n with decimals that cycle through the 4-digit sequence abcd. Multiply by 10^4 to shift abcd into integer position, leaving repeating copies after the decimal point:
10^4/n = abcd + 1/n
Solving for n gives n = (10^4 - 1) / abcd.More generally, if you want to get a cycle equal to the d digits of an integer k, you want n = (10^d - 1) / k.
However, this only gives a true unit fraction when k divides 10^d - 1, so that n is an integer. Otherwise you are forced to truncate n and getting an approximate version of the cycle.
That's exactly what happened here: 10^d - 1 is not divisible by the integer 001002...998999.
Here's a small Python program that will generate the unit fraction given the number of digits to cycle through:
import sys
n = int(sys.argv[1])
s = ''.join(("%0" + str(n) + "d") % (i,) for i in range(10**n))
print "1/%d" % ((10**len(s) - 1) / int(s),)
Usage: $ python magic.py 3
1/998001
$ python magic.py 4
1/99980001
This has just the right flavor for a Project Euler problem.