#!/usr/local/bin/python3.8 from math import sqrt, modf, floor, ceil, pi, e n = (1 + sqrt(5)) / 2 # n = pi # n = e # n = sqrt(5) # n = sqrt(7) # n = sqrt(11) + 1 print('n = {0:.3f}'.format(n)) # No use for the pigeonhole principal here. A dup means there is an # earlier "000" or "999" multiplier. for i in range(1, 1002): # The next two lines get the first three digits to the right of the # decimal point. # Get the fractional part. f = modf(i * n)[0] # Move the decimal point three places to the right. mult = floor(1000 * f) # It can happen that a single multiplier can result in a fractional # part < 0.001. if mult == 0: print('{0:d} is a good 000 multiplier'.format(i)) print(i * n) print(round(i * n)) print('Approximation is {0:d} / {1:d} =~ {2:.3f} =~ {3:.3f}.'. format(floor(i * n), i, floor(i * n) / i, n)) break # It can also happen that a single multiplier can result in a # fractional part >= 0.999. elif mult == 999: print('{0:d} is a good 999 multiplier'.format(i)) print(i * n) print(round(i * n)) print('Approximation is {0:d} / {1:d} =~ {2:.3f} =~ {3:.3f}.'. format(ceil(i * n), i, ceil(i * n) / i, n)) break