[PYTHON] About the accuracy of Archimedean circle calculation method

Introduction

Archimedes calculated the approximate values of the circumference ratio by calculating the circumferences of a regular hexagon, a regular dodecagon, a regular 24 polygon, a regular 48 square, and a regular 96 polygon circumscribing a circle in that order. tsujimotter's notebook "Archimedean and Pi" At that time, the decimal point had not been invented yet, and it was expressed as a ratio of integers. Even with these restrictions, Archimedes wanted to be less than 3 and 1/7 and greater than 3 and 10/71. (In decimal terms, between 3.1428571428571429 and 3.1408450704225352) With the help of programming, I tried to find out how accurately the pi could be calculated by proceeding with Archimedes' method as it is.

Archimedes method

The Archimedes method itself is accurately described in tujimotter's notebook, so if you are interested, please have a look there. Here, only the proof necessary for creating the program is described. If the circumference of the regular n-sided polygon is L and the radius of the circumscribed circle is 1, the circumference is 2π, so the approximate value of the circumference ratio based on the regular n-sided polygon is L / 2. 証明.JPG

[For regular hexagons]

L=F(0)B×2×6 F (0) B = 1 / √3 * ∠F (0) OB is 30 ° (△ OF (0) C is an equilateral triangle) The approximate value of pi based on a regular hexagon is L ÷ 2 = 1 / √3 × 2 × 6 ÷ 2 = 2 × √3 ≒ 3.464.

[For regular dodecagons]

L=F(1)B×2×12 F(1)B = OB× F(0)B ÷(OB+F(0)O)= F(0)B ÷(1+√(1+F(0)B× F(0)B)) The approximate value of pi based on a regular dodecagon is L ÷ 2 = F (1) B × 2 × 12 ÷ 2 ≒ 3.215. ** Proof ** Draw a straight line passing through F (0) parallel to F (1) O, and let A be the intersection with the extension line of OB. Since ΔOBF (1) and ΔABF (0) are similar, the following holds. F(1)B:OB = F(0)B:(OB+OA) Furthermore, since ΔOF (0) A is an isosceles triangle, F (0) O = OA. F (0) O is obtained from √ (OB × OB + F (0) B × F (0) B) using Pythagorean theorem.

[For regular n-sided polygons]

Similarly, the approximate value of the pi based on the regular n-sided polygon is as follows. L÷2=F(n)B×2×n÷2 = F(n-1)B÷(1+√(1+F(n-1)B× F(n-1)B))×n

Implementation by Python

The above result is implemented in Python as follows.

import math
import itertools
def f(n):
  if n == 0: return math.sqrt(3)/3
  return f(n-1)/(1+math.sqrt(1+f(n-1)**2))

N = int(input())
for n in range(N):
  print(f(n)*6*2**n)

However, since it is the Mendou that accurately inputs a regular n-sided polygon, the input value N is as follows. 0… Calculate a regular hexagon 1… Calculate up to a regular dodecagon 2… Calculate up to a regular 24 square The same applies below. Since the above algorithm requires the amount of calculation of O (2 ** n), I rewrote it with DP as follows. In addition, since it is not possible to accurately represent the decimal point with a binary number (*), we made the number about 15 digits larger and made it a program to format it when outputting.

import math
import itertools

def pi2s(pi):
  return str(pi)[0] + "." + str(pi)[1:]

def regN(n):
  return format(n, '10d') + ": "

R = 1732050807568877
B = 1000000000000000
pi = "3.14159265358979323846264338327950288"

N = int(input())
F = [0.0 for n in range(N)]
F[0] = B
for n in range(1, N):
  F[n] = F[n-1]*R/(R+math.sqrt(F[n-1]**2+R**2))

for n, v in enumerate(F):
  print(regN(6*2**n) + pi2s(int(v*6*2**n*B/R)))
print(format("pi: ", '>12') + pi[:17])

It is the result of calculation up to ** regular 3145728 square **. 実行結果.JPG It outputs π on the last line. You can see that the values are fairly accurate.

at the end

I tried to trace the method of Archimedes by programming. Contrary to my own expectations, I was surprised that it was already close to 3.14 at a fairly early stage (48-sided). Archimedes, who calculated this by hand and by the ratio of integers (fractions), just took off his hat.

Recommended Posts

About the accuracy of Archimedean circle calculation method
Calculation of the shortest path using the Monte Carlo method
About the ease of Python
About cost calculation of MeCab
About the components of Luigi
About the features of Python
Numerical approximation method when the calculation of the derivative is troublesome
Calculation of the number of Klamer correlations
About the return value of pthread_mutex_init ()
About the return value of the histogram.
About the basic type of Go
About the upper limit of threads-max
About the behavior of yield_per of SqlAlchemy
About the size of matplotlib points
About the basics list of Python basics
Count / verify the number of method calls.
10 methods to improve the accuracy of BERT
About the behavior of enable_backprop of Chainer v2
About the virtual environment of python version 3.7
Roughly think about the gradient descent method
About the arguments of the setup function of PyCaret
About the Normal Equation of Linear Regression
Approximation by the least squares method of a circle with two fixed points
About the behavior of copy, deepcopy and numpy.copy
About the X-axis notation of Matplotlib bar graphs
About the processing speed of SVM (SVC) of scikit-learn
A note about the python version of python virtualenv
Consider improving the accuracy of VAE abnormality detection
About the development contents of machine learning (Example)
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
About the behavior of Queue during parallel processing
About the * (asterisk) argument of python (and itertools.starmap)
[Python] Seriously think about the M-1 winning method.
Clustering of clustering method
About the test
About the queue
Numerical calculation of compressible fluid by finite volume method
Experience the good calculation efficiency of vectorization in Python
A memorandum about the warning of the pylint output result
Calculation of the minimum required number of votes from turnout
Destroy the intermediate expression of the sweep method with Python
Think about the next generation of Rack and WSGI
About testing in the implementation of machine learning models
About the inefficiency of data transfer in luigi on-memory
What is the true identity of Python's sort method "sort"? ??
About the uncluttered arrangement in the import order of flake8
[Summary of 27 languages] My number check digit calculation method
Calculation of homography matrix by least squares method (DLT method)
A story about changing the master name of BlueZ
Personal notes about the integration of vscode and anaconda
A reminder about the implementation of recommendations in Python
I tried the simplest method of multi-label document classification
The copy method of pandas.DataFrame is deep copy by default