GithubHelp home page GithubHelp logo

calculate-pi-archimedes's Introduction

calculate-pi-archimedes

Quick python script to calculate Pi as Archimedes did it manually

What is Pi?

The constant ratio between the circumference and diameter of every circle in this universe

how did Archimedes calculate this ratio in 200 BC?

By approximation, starting with a hexagon (6 sides) within a circle

Screenshot 2021-07-19 at 11 59 55 PM

Assuming the hexagon having side length of 1

Circumference of hexagon = 1 * 6 = 6

Diamater of hexagon/circle = 1 * 2 = 2

Hence ratio ฯ€ > 6/2 = 3. (why greater? as hexagon hasn't completely covered the whole area in the circle)

Calculating value of SIDE for 12 side polygon within a circle

Note : The 12 sided polygon covers more area of the circumference and hence Pi gets more accurate as we keep increasing the number of sides of the polygon

We need to calculate the line segment DE to get closer to the circumference of the circle

Screenshot 2021-08-18 at 1 56 57 AM

Naming the FD line segment can be called as S

Naming MN line segment as d

Using pythagoras, we get length of d to be

But what we need is the length of a

which can be described as

Finally now X can be described as

calculate PI ratio - 6 side

N - Number of Sides = 6

S - Length of Side = 1

d = () = 0.87

a = = = 0.13

New S (which is X basically) = = 0.52

New length of side when polygon sides are doubled

Perimeter = 6

Diameter = = 3

calculate PI ratio - 12 side now

N = 12

S = 0.52 (From previous calculation)

and rest values follow the same above approach

Pythonic way to calculate PI ratio

import math
N = float(6)
S = float(1)

#Iterating uptil N is 96 sides 
for i in range(2,7):
    d = math.sqrt(1 - pow(S/2,2))
    a = 1 - d
    NewS = math.sqrt(pow(a,2) + pow(S/2,2))
    Perimeter = float(N * S)
    Pi = Perimeter/2
    print(Pi)
    S = NewS
    N = N*2.0

The results we get are as follows

3.0
3.10582854123
3.13262861328
3.13935020305
3.14103195089

calculate-pi-archimedes's People

Contributors

shivankar-madaan avatar

Stargazers

 avatar

Forkers

zentrocdot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.