3 ms·
There's some sleight of hand here. Not all the digits are exactly right. Look how it skips from 997 to 999: http://www.futilitycloset.com/2012/01/08/math-notes
by psykotic 15y ago
There's some sleight of hand here. Not all the digits are exactly right. Look how it skips from 997 to 999:
http://www.futilitycloset.com/2012/01/08/math-notes-76/ 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.
- bdg 15y agoI just want to know I appreciate you posting this. Some people might be interested in "wow math sure can do some funky stuff", but others like myself really want to know why. I figure that's also covers a sizable group of us here on Hacker News.
- toasterweasel 15y agoIt does indeed. I have a love/hate relationship with math. Love because it's fascinating and intergal to an understanding of our universe, hate because I had some horrible math teachers over the years...plus I do admit to being slightly lazy.
- jklp 15y agoThere is no why. It just is. This is the worst thing about being human :) http://www.smbc-comics.com/index.php?db=comics&id=1914 http://www.smbc-comics.com/index.php?db=comics&id=1914
- psykotic 15y agoNice trick. It works in any even base b: xy = b((x - b/2) + (y - b/2)) + (b - x)(b - y) For b = 2 in particular, x - b/2 and x - b/2 are the negative 1's complements of x and y, and b - x and b - y are the 2's complements. More generally, they are the same kinds of complements generalized to base b. Because of that symmetric relationship with complementation, I suspect there is a conceptual proof waiting to be found. By conceptual I mean something that isn't just blind formula crunching. That would get us much closer to "why".
- chadzawistowski 15y agoIt's a bit sneaky to say it generates all three-digit numbers when 998 clearly isn't there, but as onedognight pointed out below, the leading 1 from the 1000 which follows 999 will cause 999 to overflow into 1000, which turns the 998 into a 999.
- hashfold 15y agolove the math. thanks for sharing.
- deleted 15y ago[deleted]