I was doing a test, calculating the minimum n such that for 1/a + 1/b = 1/n, there are at least x distinct solutions. (a, b, n, x are positive integers)
I checked b = 1/(1/n - 1/a) and tested b with is.integer()
, and the result surprised me.
x = 2
n = 1
while(1):
x0 = 0
for a in range(n+1,2*n + 1):
print '-------------'
print 'a={}'.format(a)
b = 1.0 / (1.0/n - 1.0/a)
print 'b={}'.format(b)
print 'b is integer: ' + str(b.is_integer())
if b.is_integer():
print 'a={} b={} n={}'.format(a,b,n)
x0 += 1
if x0 * 2 - 1 >= x:
print n
exit(0)
n += 1
the output, so why are b = 6.0 and 12.0 not whole numbers?
-------------
a=2
b=2.0
b is integer: True
a=2 b=2.0 n=1
-------------
a=3
b=6.0
b is integer: False
-------------
a=4
b=4.0
b is integer: True
a=4 b=4.0 n=2
-------------
a=4
b=12.0
b is integer: False
-------------
a=5
b=7.5
b is integer: False
-------------
a=6
b=6.0
b is integer: True
a=6 b=6.0 n=3
...
If you try something like this
print 'b is integer: ' + str(b.is_integer()), repr(b)
You'll see that due to approximations in floating point math, those values are very close to, but not quite integers