I recently saw someone post [1] that 987654321/123456789 is very nearly 8, specifically 8.0000000729.
I wondered whether there’s anything distinct about base 10 in this. For example, would the ratio of 54321six and 12345six be close to an integer? The ratio is 4.00268, which is pretty close to 4.
What about a larger base? Let’s try base 16. The expression
0xFEDCBA987654321 / 0x123456789ABCDEF
in Python returns 14. The exact ratio is not 14, but it’s as close to 14 as a standard floating point number can be.
For a base b, let denom(b) to be the number formed by concatenating all the digits in ascending order and let num(b) be the number formed by concatenating all the digits in descending order.
Then for b > 2 we have
The following Python code demonstrates that this is true for b up to 1000.
num = lambda b: sum([k*b**(k-1) for k in range(1, b)])
denom = lambda b: sum([(b-k)*b**(k-1) for k in range(1, b)])
for b in range(3, 1001):
n, d = num(b), denom(b)
assert(n // d == b-2)
assert(n % d == b-1)
So for any base the ratio is nearly an integer, namely b − 2, and the fractional part is roughly 1/bb−2.
When b = 16, as in the example above, the result is approximately
14 + 16−14 = 8 + 4 + 2 + 2−56
which would take 60 bits to represent exactly, but a floating point fraction only has 53 bits. That’s why our calculation returned exactly 14 with no fractional part.
[1] I saw @ColinTheMathmo post it on Mastodon. He said he saw it on Fermat’s Library somewhere. I assume it’s a very old observation and that the analysis I did above has been done many times before.