We came to talk about how Archimedes 3000 years ago calculated pi by inscribed polygons with ever more sides: 6, 12, 24, 48, …

The side length in a 6-sided regular polygon inscribed in a unit circle is 1.

I denote this S

_{1}= 1.

Calculation of S

_{2}, the side length in a 12-sided polygon inscribed in a unit circle, is easily derived from S

_{1}using only the Pythagorean theorem: a˛ + b˛ = c˛.

S

_{3}can be derived from S

_{2}and so on.

As pi is the ratio of perimeter to diameter, you get:

Pi (approximation) = n * 3 * S

_{n}

To make a calculation of S

_{n}in python should not require more than 10-15 lines of code. If you do this, you will notice that after 24 iterations S

_{n}doesn’t change as floats in python is given with only 15 decimals.

But how can we calculate pi with 100 or 1000 decimals…? This is the challenge here.

• Make a program that will calculate pi to minimum 1000 correct digits.

• Use an algorithm of you own choice.

• Python is the preferred language, but the challenge is open to all.

• When submitting solutions, please use the spoiler protection, see below.

• Please comment the code/algorithm.

• Execution time is not a primary parameter; fast execution, however, is desirable.

• There are no prizes, - it’s purely a friendly competition; hopefully with some learnings…

• Multiple solutions (building on different algorithms) are welcome.

Challenges involving pi is are not new. Some of the more experienced guys may have made this before. If so, please hold back your finished solutions a few weeks; or look your solutions over and check for possible improvements.

Happy coding.

Spoiler protection:

The spoiler tags are found in the 'Other styles' menu, 4th from top. I find it easiest to first make the code (with code tags), then marking the code (including tags) and apply the spoiler tags.