6 Replies - 809 Views - Last Post: 12 November 2018 - 08:36 AM Rate Topic: -----

#1 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 488
  • View blog
  • Posts: 1,530
  • Joined: 27-December 13

Christmas challenge: Calculate Pi with 1000 decimals

Post icon  Posted 09 November 2018 - 01:55 AM

*
POPULAR

This week I helped my teenage son with some mathematics involving pi.

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 S1 = 1.

Calculation of S2, the side length in a 12-sided polygon inscribed in a unit circle, is easily derived from S1 using only the Pythagorean theorem: a˛ + b˛ = c˛.

S3 can be derived from S2 and so on.

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

Pi (approximation) = n * 3 * Sn

To make a calculation of Sn in python should not require more than 10-15 lines of code. If you do this, you will notice that after 24 iterations Sn 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.

Is This A Good Question/Topic? 6
  • +

Replies To: Christmas challenge: Calculate Pi with 1000 decimals

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11325
  • View blog
  • Posts: 19,369
  • Joined: 19-March 11

Re: Christmas challenge: Calculate Pi with 1000 decimals

Posted 09 November 2018 - 09:16 AM

Christmas is a good time for pi, it's true.
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1554
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: Christmas challenge: Calculate Pi with 1000 decimals

Posted 10 November 2018 - 11:05 AM

Ok ,I will have a go, having researched Archimedes for this project I can only come to the conclusion that he was 'aff his heid' so, in keeping with his original idea for solving PI, my solution is also insane !!

It is going to be also the slowest solution but in keeping with Archimedes it solves the riddle of PI by using inscribed regular polygons on the inside of a circle, each polygon is drawn with a Turtle using regular Isosceles Triangles, I know it's mental but keep with me as I have said Archimedes was a nutcase so this solution is well Nutty, It is exactly though, the way Archimedes would have solved this problem.

All his pupils would have been like:
"Archimedes ya Mad Man, just use the Gregory-Leibniz series, like everyone else."

And Archimedes would have been like:
"Naw I'm using regular Polygons of ever-increasing sides made up of regular Isosceles Triangles of ever decreasing size, I've got a Turtle outback drawing it as we speak."

And his pupils would have been like:
"Aye, Archimedes course it's a Turtle drawing it, he is definitely on the magic mushrooms I'm telling ye!!"

The Solution.
Spoiler


I've got a cheek putting it in spoiler tags as nobody is stealing this code for their shot, but it is a very pretty and fascinating demonstration of the problem and as luck would have it it is accurate until the 100th place where it shows an 8 instead of a 9 but it corrects itself on the 104th iteration, which just goes to prove not only was Archimedes crazy, he was also slightly inaccurate!!

Mental.
Was This Post Helpful? 2
  • +
  • -

#4 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 488
  • View blog
  • Posts: 1,530
  • Joined: 27-December 13

Re: Christmas challenge: Calculate Pi with 1000 decimals

Posted 10 November 2018 - 01:14 PM

Thank you snoopy11, marvelous contribution..!

Archimedes was working on uniting mathematics and physics; just like you have now united comics and coding.

I'm afraid I'm gonna steal your idea for educational purposes - I will, however, remember to reference the source.

snoopy11 only did 100 decimals, but I checked the algorithm at 500 decimals and it works fine. Probably 1000 decimals will also work, but it is slow; it is running now in the background...

Let me raise the bar here: Can anyone do the calculation without the Decimal module...?
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1554
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: Christmas challenge: Calculate Pi with 1000 decimals

Posted 10 November 2018 - 02:58 PM

Yeah, I can do it with strings DK,

forgive my weird sense of humour, you have been a good sport!!

Anyway, steal away for educational purposes for sure, man!!

And you sat through 500 iterations jeez, I was losing the will to live at 92 Iterations, well done!!
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11325
  • View blog
  • Posts: 19,369
  • Joined: 19-March 11

Re: Christmas challenge: Calculate Pi with 1000 decimals

Posted 12 November 2018 - 08:14 AM

First thing I tried was the classic Monte Carlo that I learned from Mr. Drake in seventh grade.

Spoiler



The latter shows some of the advantages and limitations of naively applying numpy to a problem - it speeds things up pretty well, up to a point, and then it runs out of room and dies.

Sadly, there's not a lot to indicate that Monte Carlo converges fast enough to be worth further investigation:

Quote

In [2]: numpy_monte_carlo_calc_py()
numpy_monte_carlo_calc_py()
Out[8]: 3.156

In [9]:
numpy_monte_carlo_calc_py(10000)
Out[9]: 3.1312

In [10]: numpy_monte_carlo_calc_py(100000)
numpy_monte_carlo_calc_py(100000)
Out[10]: 3.14212

In [11]: numpy_monte_carlo_calc_py(1000000)
numpy_monte_carlo_calc_py(1000000)
Out[11]: 3.14188

In [12]: numpy_monte_carlo_calc_py(10000000)
numpy_monte_carlo_calc_py(10000000)
Out[12]: 3.1419936

In [13]: numpy_monte_carlo_calc_py(100000000)
numpy_monte_carlo_calc_py(100000000)
Out[13]: 3.14164028


So, off to the next experiment.
Was This Post Helpful? 2
  • +
  • -

#7 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 488
  • View blog
  • Posts: 1,530
  • Joined: 27-December 13

Re: Christmas challenge: Calculate Pi with 1000 decimals

Posted 12 November 2018 - 08:36 AM

I appreciate you take an nontraditional approach here.
Just what I hoped for.

Actually, the Monte Carlo method is very well suited for paralles processing.
I did it a few years back on my 4-core machine.
As I remember it, using 4 cores increased the speed by a factor of 3 or so.
Not bad, but still way too slow for the number crunching required here...

Also, this method is influenced by the (true) randomness of the random module.
I'm sure the random module is good, but probably not uniform to 1000 digits level.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1