円周率の計算(円に内接する多角形の周囲の長さを使う)

半径が1の円に内接する正1万角形の周囲の長さを使って求める。
それが2πに近くなるので、2で割る。

Python

import math

#半径が1の円に内接する正n角形の1辺の長さを求める
#頂角がth、等しい2辺の長さが1の二等辺三角形の底辺の長さを求める
def length_of_bottom_of_nearbye_triangle(th):
    cos_th = math.cos(math.radians(th))
    return math.sqrt(2 - 2 * cos_th)

#半径が1の円に内接する正n角形の周囲の長さを求める
def length_of_n_square(n):
    th = 360 / n
    return n * length_of_bottom_of_nearbye_triangle(th)

print(length_of_n_square(10000) / 2)

結果

3.1415926022400824