Skip to content

Instantly share code, notes, and snippets.

@telliott99
Created April 17, 2021 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telliott99/383c06f4721e6a6c8c8d99bd1942cef8 to your computer and use it in GitHub Desktop.
Save telliott99/383c06f4721e6a6c8c8d99bd1942cef8 to your computer and use it in GitHub Desktop.
import math
from operator import itemgetter
t = math.pi
N = 1000
def find_bounds(d):
p = d*t
m = int(p)
n = m + 1
assert n/d > t > m/d
return (m,n)
rL = list()
for d in range(3,N):
m,n = find_bounds(d)
if math.gcd(m,d) == 1:
e = m/d - t
rL.append((m,d,e,abs(e)))
if math.gcd(n,d) == 1:
e = n/d-t
rL.append((n,d,e,abs(e)))
def show(rL, j=len(rL)):
i = 0
for n,d,e,a in rL[:j]:
i += 1
print('%5d: %5d %5d %11.8f' % (i,n,d,e))
rL.sort(key=itemgetter(3))
show(rL,j=48)
'''
> p3 rational_approx.py
1: 355 113 0.00000027
2: 2818 897 -0.00000960
3: 2862 911 0.00000998
4: 2463 784 -0.00001102
5: 2507 798 0.00001136
6: 2108 671 -0.00001292
7: 2152 685 0.00001319
8: 1753 558 -0.00001559
9: 1797 572 0.00001574
10: 1442 459 0.00001955
11: 1398 445 -0.00001962
12: 2529 805 0.00002225
13: 2441 777 -0.00002251
14: 1087 346 0.00002584
15: 1043 332 -0.00002639
16: 2906 925 0.00002897
17: 2774 883 -0.00002980
18: 1819 579 0.00003084
19: 1731 551 -0.00003186
20: 2551 812 0.00003296
21: 2419 770 -0.00003421
22: 3107 989 -0.00003553
23: 732 233 0.00003825
24: 688 219 -0.00004014
25: 2573 819 0.00004349
26: 3085 982 -0.00004479
27: 1841 586 0.00004557
28: 2397 763 -0.00004613
29: 2950 939 0.00004739
30: 1709 544 -0.00004854
31: 1109 353 0.00005041
32: 2730 869 -0.00005065
33: 2595 826 0.00005384
34: 1021 325 -0.00005419
35: 1486 473 0.00005640
36: 2375 756 -0.00005826
37: 1863 593 0.00005996
38: 1354 431 -0.00006133
39: 2240 713 0.00006233
40: 3041 968 -0.00006373
41: 2617 833 0.00006401
42: 2994 953 0.00006527
43: 1687 537 -0.00006565
44: 2020 643 -0.00006855
45: 2353 749 -0.00007062
46: 2686 855 -0.00007219
47: 3019 961 -0.00007340
48: 377 120 0.00007401
>
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment