# What is the best approximation of Pi?

When I was younger I was taught to approximate Pi by 22/7.

But why 22/7? Maybe there is some better way?

I wrote a little program to explore this question by considering all possible numerators and denominators and ranking them by their percent deviation from the "true value" of Pi.

```#!/usr/bin/env python3
import math

#Let's explore all three digits numerators and divisors
#Obviously, numerator must be &gt; denominator
candidates = []
for numerator in range(1,999):
for denominator in range(1,numerator):
approx      = float(numerator)/float(denominator)
diff        = abs((approx-math.pi)/math.pi)*100
candidates += [(approx,numerator,denominator,diff)]

#Sort candidates by their percent difference from the "true" value of pi. "True"
#is in quotation marks here because math.pi is itself an approximation, albeit a
#good one, of the true value of pi.
candidates.sort(key=lambda x: x[3])

#List the sorted candidates
print("Rank Num / Den = Approx ~Diff")
for rank,candidate in enumerate(candidates):
print("{0:&gt;4d} {2:&gt;3d} / {3:&gt;3d} = {1:&gt;.10f} ~{4:&gt;.10f}".format(rank,candidate[0],candidate[1],candidate[2],candidate[3]))
```

Running the program produces the following list:

```Rank Num / Den = Approx ~Diff
0  22 /   7 = 3.1428571429 ~0.0402499435
1  44 /  14 = 3.1428571429 ~0.0402499435
2  66 /  21 = 3.1428571429 ~0.0402499435
3  88 /  28 = 3.1428571429 ~0.0402499435
4  91 /  29 = 3.1379310345 ~0.1165529561
5  69 /  22 = 3.1363636364 ~0.1664447878
6  85 /  27 = 3.1481481481 ~0.2086678727
7  47 /  15 = 3.1333333333 ~0.2629023291
8  94 /  30 = 3.1333333333 ~0.2629023291
9  63 /  20 = 3.1500000000 ~0.2676141479
10  72 /  23 = 3.1304347826 ~0.3551660642
```

So 22/7 is the best value of Pi for two-digit numbers.

How does it do with three-digit numbers? A simple modification (`for numerator in range(1,999):`) to the program suffices to tell us: it ranks 107th.

```Rank Num / Den = Approx ~Diff
0 355 / 113 = 3.1415929204 ~0.0000084914
1 710 / 226 = 3.1415929204 ~0.0000084914
2 732 / 233 = 3.1416309013 ~0.0012174620
3 688 / 219 = 3.1415525114 ~0.0012777651
4 377 / 120 = 3.1416666667 ~0.0023559094
5 754 / 240 = 3.1416666667 ~0.0023559094
6 333 / 106 = 3.1415094340 ~0.0026489630
7 666 / 212 = 3.1415094340 ~0.0026489630
8 776 / 247 = 3.1417004049 ~0.0034298294
9 977 / 311 = 3.1414790997 ~0.0036145333
10 644 / 205 = 3.1414634146 ~0.0041138037
11 399 / 127 = 3.1417322835 ~0.0044445570
12 798 / 254 = 3.1417322835 ~0.0044445570
13 955 / 304 = 3.1414473684 ~0.0046245706
14 820 / 261 = 3.1417624521 ~0.0054048547
15 311 /  99 = 3.1414141414 ~0.0056822190
16 622 / 198 = 3.1414141414 ~0.0056822190
17 933 / 297 = 3.1414141414 ~0.0056822190
18 421 / 134 = 3.1417910448 ~0.0063149876
19 842 / 268 = 3.1417910448 ~0.0063149876
20 911 / 290 = 3.1413793103 ~0.0067909264
21 864 / 275 = 3.1418181818 ~0.0071787865
22 600 / 191 = 3.1413612565 ~0.0073655967
23 889 / 283 = 3.1413427562 ~0.0079544815
24 443 / 141 = 3.1418439716 ~0.0079997017
25 886 / 282 = 3.1418439716 ~0.0079997017
26 908 / 289 = 3.1418685121 ~0.0087808494
27 289 /  92 = 3.1413043478 ~0.0091770575
28 578 / 184 = 3.1413043478 ~0.0091770575
29 867 / 276 = 3.1413043478 ~0.0091770575
30 465 / 148 = 3.1418918919 ~0.0095250510
31 930 / 296 = 3.1418918919 ~0.0095250510
32 952 / 303 = 3.1419141914 ~0.0102348670
33 845 / 269 = 3.1412639405 ~0.0104632620
34 487 / 155 = 3.1419354839 ~0.0109126268
35 974 / 310 = 3.1419354839 ~0.0109126268
36 556 / 177 = 3.1412429379 ~0.0111317976
37 996 / 317 = 3.1419558360 ~0.0115604540
38 823 / 262 = 3.1412213740 ~0.0118181949
39 509 / 162 = 3.1419753086 ~0.0121802886
40 267 /  85 = 3.1411764706 ~0.0132475164
41 534 / 170 = 3.1411764706 ~0.0132475164
42 801 / 255 = 3.1411764706 ~0.0132475164
43 531 / 169 = 3.1420118343 ~0.0133429370
44 553 / 176 = 3.1420454545 ~0.0144131021
45 779 / 248 = 3.1411290323 ~0.0147575253
46 575 / 183 = 3.1420765027 ~0.0154013965
47 512 / 163 = 3.1411042945 ~0.0155449533
48 597 / 190 = 3.1421052632 ~0.0163168693
49 757 / 241 = 3.1410788382 ~0.0163552526
50 619 / 197 = 3.1421319797 ~0.0171672831
51 641 / 204 = 3.1421568627 ~0.0179593352
52 245 /  78 = 3.1410256410 ~0.0180485705
53 490 / 156 = 3.1410256410 ~0.0180485705
54 735 / 234 = 3.1410256410 ~0.0180485705
55 980 / 312 = 3.1410256410 ~0.0180485705
56 663 / 211 = 3.1421800948 ~0.0186988341
57 958 / 305 = 3.1409836066 ~0.0193865692
58 685 / 218 = 3.1422018349 ~0.0193908422
59 713 / 227 = 3.1409691630 ~0.0198463220
60 707 / 225 = 3.1422222222 ~0.0200397920
61 729 / 232 = 3.1422413793 ~0.0206495810
62 468 / 149 = 3.1409395973 ~0.0207874268
63 936 / 298 = 3.1409395973 ~0.0207874268
64 751 / 239 = 3.1422594142 ~0.0212236502
65 691 / 220 = 3.1409090909 ~0.0217584759
66 773 / 246 = 3.1422764228 ~0.0217650488
67 914 / 291 = 3.1408934708 ~0.0222556797
68 795 / 253 = 3.1422924901 ~0.0222764886
69 817 / 260 = 3.1423076923 ~0.0227603893
70 839 / 267 = 3.1423220974 ~0.0232189169
71 861 / 274 = 3.1423357664 ~0.0236540161
72 223 /  71 = 3.1408450704 ~0.0237963113
73 446 / 142 = 3.1408450704 ~0.0237963113
74 669 / 213 = 3.1408450704 ~0.0237963113
75 892 / 284 = 3.1408450704 ~0.0237963113
76 883 / 281 = 3.1423487544 ~0.0240674378
77 905 / 288 = 3.1423611111 ~0.0244607626
78 927 / 295 = 3.1423728814 ~0.0248354211
79 949 / 302 = 3.1423841060 ~0.0251927114
80 870 / 277 = 3.1407942238 ~0.0254148087
81 971 / 309 = 3.1423948220 ~0.0255338137
82 993 / 316 = 3.1424050633 ~0.0258598040
83 647 / 206 = 3.1407766990 ~0.0259726403
84 424 / 135 = 3.1407407407 ~0.0271172282
85 848 / 270 = 3.1407407407 ~0.0271172282
86 625 / 199 = 3.1407035176 ~0.0283020780
87 826 / 263 = 3.1406844106 ~0.0289102708
88 201 /  64 = 3.1406250000 ~0.0308013704
89 402 / 128 = 3.1406250000 ~0.0308013704
90 603 / 192 = 3.1406250000 ~0.0308013704
91 804 / 256 = 3.1406250000 ~0.0308013704
92 983 / 313 = 3.1405750799 ~0.0323903774
93 782 / 249 = 3.1405622490 ~0.0327987969
94 581 / 185 = 3.1405405405 ~0.0334897985
95 961 / 306 = 3.1405228758 ~0.0340520841
96 380 / 121 = 3.1404958678 ~0.0349117770
97 760 / 242 = 3.1404958678 ~0.0349117770
98 939 / 299 = 3.1404682274 ~0.0357915965
99 559 / 178 = 3.1404494382 ~0.0363896760
100 738 / 235 = 3.1404255319 ~0.0371506367
101 917 / 292 = 3.1404109589 ~0.0376145101
102 179 /  57 = 3.1403508772 ~0.0395269704
103 358 / 114 = 3.1403508772 ~0.0395269704
104 537 / 171 = 3.1403508772 ~0.0395269704
105 716 / 228 = 3.1403508772 ~0.0395269704
106 895 / 285 = 3.1403508772 ~0.0395269704
107  22 /   7 = 3.1428571429 ~0.0402499435
```

\$22/7\$ ranks 107th on this list.

Are any of the values above easy to remember? The following strike me as candidates:

``` 0 355 / 113 = 3.1415929204 ~0.0000084914
7 666 / 212 = 3.1415094340 ~0.0026489630
15 311 /  99 = 3.1414141414 ~0.0056822190
20 911 / 290 = 3.1413793103 ~0.0067909264
```

None of them seem as easy to remember as 22/7, though they are more accurate.

One use of 22/7 is for doing mental math. Therefore, it's worth asking whether any of these easily-memorable three-digit candidates have convenient mathematical properties for mental math. Let's factor them:

```355 / 113 = (5*71)   / (113)
666 / 212 = (3*3*37) / (2*53)
311 / 99  = (311)    / (3*3*11)
911 / 290 = (911)    / (2*5*29)
```

None of those factors seem conducive to mental math.

In summary, it seems as though 22/7 is the best fractional estimate of Pi in terms of ease of memorization and use for mental math.

What if you don't use a fractional estimate and prefer, instead, a decimal estimate? The Pi button's your best bet, but, barring that, how do various decimal approximations of Pi stack up?

We add the following chunk to the program after the first candidate-generating chunk:

```for dec_approx in [3,3.1,3.14,3.141,3.1415,3.14159,3.141592,3.1415926]:
diff        = abs((dec_approx-math.pi)/math.pi)*100
candidates += [(dec_approx,0,0,diff)]
```

The results are as follows:

```Rank Num / Den = Approx ~Diff
0   0 /   0 = 3.1415926000 ~0.0000017058
1 355 / 113 = 3.1415929204 ~0.0000084914
2 710 / 226 = 3.1415929204 ~0.0000084914
3   0 /   0 = 3.1415920000 ~0.0000208044
4   0 /   0 = 3.1415900000 ~0.0000844664
5 732 / 233 = 3.1416309013 ~0.0012174620
6 688 / 219 = 3.1415525114 ~0.0012777651
7 377 / 120 = 3.1416666667 ~0.0023559094
8 754 / 240 = 3.1416666667 ~0.0023559094
9 333 / 106 = 3.1415094340 ~0.0026489630
10 666 / 212 = 3.1415094340 ~0.0026489630
11   0 /   0 = 3.1415000000 ~0.0029492554
12 776 / 247 = 3.1417004049 ~0.0034298294
13 977 / 311 = 3.1414790997 ~0.0036145333
14 644 / 205 = 3.1414634146 ~0.0041138037
15 399 / 127 = 3.1417322835 ~0.0044445570
16 798 / 254 = 3.1417322835 ~0.0044445570
17 955 / 304 = 3.1414473684 ~0.0046245706
18 820 / 261 = 3.1417624521 ~0.0054048547
19 311 /  99 = 3.1414141414 ~0.0056822190
20 622 / 198 = 3.1414141414 ~0.0056822190
21 933 / 297 = 3.1414141414 ~0.0056822190
22 421 / 134 = 3.1417910448 ~0.0063149876
23 842 / 268 = 3.1417910448 ~0.0063149876
24 911 / 290 = 3.1413793103 ~0.0067909264
25 864 / 275 = 3.1418181818 ~0.0071787865
26 600 / 191 = 3.1413612565 ~0.0073655967
27 889 / 283 = 3.1413427562 ~0.0079544815
28 443 / 141 = 3.1418439716 ~0.0079997017
29 886 / 282 = 3.1418439716 ~0.0079997017
30 908 / 289 = 3.1418685121 ~0.0087808494
31 289 /  92 = 3.1413043478 ~0.0091770575
32 578 / 184 = 3.1413043478 ~0.0091770575
33 867 / 276 = 3.1413043478 ~0.0091770575
34 465 / 148 = 3.1418918919 ~0.0095250510
35 930 / 296 = 3.1418918919 ~0.0095250510
36 952 / 303 = 3.1419141914 ~0.0102348670
37 845 / 269 = 3.1412639405 ~0.0104632620
38 487 / 155 = 3.1419354839 ~0.0109126268
39 974 / 310 = 3.1419354839 ~0.0109126268
40 556 / 177 = 3.1412429379 ~0.0111317976
41 996 / 317 = 3.1419558360 ~0.0115604540
42 823 / 262 = 3.1412213740 ~0.0118181949
43 509 / 162 = 3.1419753086 ~0.0121802886
44 267 /  85 = 3.1411764706 ~0.0132475164
45 534 / 170 = 3.1411764706 ~0.0132475164
46 801 / 255 = 3.1411764706 ~0.0132475164
47 531 / 169 = 3.1420118343 ~0.0133429370
48 553 / 176 = 3.1420454545 ~0.0144131021
49 779 / 248 = 3.1411290323 ~0.0147575253
50 575 / 183 = 3.1420765027 ~0.0154013965
51 512 / 163 = 3.1411042945 ~0.0155449533
52 597 / 190 = 3.1421052632 ~0.0163168693
53 757 / 241 = 3.1410788382 ~0.0163552526
54 619 / 197 = 3.1421319797 ~0.0171672831
55 641 / 204 = 3.1421568627 ~0.0179593352
56 245 /  78 = 3.1410256410 ~0.0180485705
57 490 / 156 = 3.1410256410 ~0.0180485705
58 735 / 234 = 3.1410256410 ~0.0180485705
59 980 / 312 = 3.1410256410 ~0.0180485705
60 663 / 211 = 3.1421800948 ~0.0186988341
61   0 /   0 = 3.1410000000 ~0.0188647497
62 958 / 305 = 3.1409836066 ~0.0193865692
63 685 / 218 = 3.1422018349 ~0.0193908422
64 713 / 227 = 3.1409691630 ~0.0198463220
65 707 / 225 = 3.1422222222 ~0.0200397920
66 729 / 232 = 3.1422413793 ~0.0206495810
67 468 / 149 = 3.1409395973 ~0.0207874268
68 936 / 298 = 3.1409395973 ~0.0207874268
69 751 / 239 = 3.1422594142 ~0.0212236502
70 691 / 220 = 3.1409090909 ~0.0217584759
71 773 / 246 = 3.1422764228 ~0.0217650488
72 914 / 291 = 3.1408934708 ~0.0222556797
73 795 / 253 = 3.1422924901 ~0.0222764886
74 817 / 260 = 3.1423076923 ~0.0227603893
75 839 / 267 = 3.1423220974 ~0.0232189169
76 861 / 274 = 3.1423357664 ~0.0236540161
77 223 /  71 = 3.1408450704 ~0.0237963113
78 446 / 142 = 3.1408450704 ~0.0237963113
79 669 / 213 = 3.1408450704 ~0.0237963113
80 892 / 284 = 3.1408450704 ~0.0237963113
81 883 / 281 = 3.1423487544 ~0.0240674378
82 905 / 288 = 3.1423611111 ~0.0244607626
83 927 / 295 = 3.1423728814 ~0.0248354211
84 949 / 302 = 3.1423841060 ~0.0251927114
85 870 / 277 = 3.1407942238 ~0.0254148087
86 971 / 309 = 3.1423948220 ~0.0255338137
87 993 / 316 = 3.1424050633 ~0.0258598040
88 647 / 206 = 3.1407766990 ~0.0259726403
89 424 / 135 = 3.1407407407 ~0.0271172282
90 848 / 270 = 3.1407407407 ~0.0271172282
91 625 / 199 = 3.1407035176 ~0.0283020780
92 826 / 263 = 3.1406844106 ~0.0289102708
93 201 /  64 = 3.1406250000 ~0.0308013704
94 402 / 128 = 3.1406250000 ~0.0308013704
95 603 / 192 = 3.1406250000 ~0.0308013704
96 804 / 256 = 3.1406250000 ~0.0308013704
97 983 / 313 = 3.1405750799 ~0.0323903774
98 782 / 249 = 3.1405622490 ~0.0327987969
99 581 / 185 = 3.1405405405 ~0.0334897985
100 961 / 306 = 3.1405228758 ~0.0340520841
101 380 / 121 = 3.1404958678 ~0.0349117770
102 760 / 242 = 3.1404958678 ~0.0349117770
103 939 / 299 = 3.1404682274 ~0.0357915965
104 559 / 178 = 3.1404494382 ~0.0363896760
105 738 / 235 = 3.1404255319 ~0.0371506367
106 917 / 292 = 3.1404109589 ~0.0376145101
107 179 /  57 = 3.1403508772 ~0.0395269704
108 358 / 114 = 3.1403508772 ~0.0395269704
109 537 / 171 = 3.1403508772 ~0.0395269704
110 716 / 228 = 3.1403508772 ~0.0395269704
111 895 / 285 = 3.1403508772 ~0.0395269704
112  22 /   7 = 3.1428571429 ~0.0402499435
```

22/7 ranks 112th on this list.

Let's pull out the interesting values:

```Rank Num / Den = Approx ~Diff
0           = 3.1415926000 ~0.0000017058
1 355 / 113 = 3.1415929204 ~0.0000084914
3           = 3.1415920000 ~0.0000208044
4           = 3.1415900000 ~0.0000844664
10 666 / 212 = 3.1415094340 ~0.0026489630
11           = 3.1415000000 ~0.0029492554
19 311 /  99 = 3.1414141414 ~0.0056822190
24 911 / 290 = 3.1413793103 ~0.0067909264
61           = 3.1410000000 ~0.0188647497
112  22 /   7 = 3.1428571429 ~0.0402499435
```

So, remembering 3.141 is about twice as good as remembering 22/7. This number seems pretty memorable, so 22/7 is only good if you are using it because you like playing with fractions.