Write a program to calculate pi
I listened to the following idea in some probability session… So reserve your credits, if any, for a phantom person. Consider a circle of radius 1 unit with its centre in the middle of the square. The circle will be perfectly inside the square Circumscribed?? Consider throwing a dot randomly inside this square. It could be anywhere inside the square. The probability that the dot falls inside the circle is:. Considering the centre of square circle as [0,0], this randomly generated point will surely fall inside the square.
However the assignment still calls for two loops, one that increments i and the inner that computes the series from 1 to i. Well, write that by taking the snippets I linked, and wrap them in an extra loop that makes the series longer. However, you would only need 1 loop and print it for each term you add. That would have the same result, but a lot better performance.
So I finally got the program to run properly. Add a comment. Active Oldest Votes. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.
The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.
I know y'all frown on homework help that's this lost so if you could just point me in the right direction I'll do my best. Someone already did the calculus for you by giving that series sum. You need a function; something like this:. Taylor series isn't very accurate: 10, terms and the approximation is 3. A million terms and the approximation is 3. A hundred million terms and the approximation is 3. To calculate up to the nth term, you should then loop from 0 to n, adding up the final result to your current approximation of pi at the end of each loop.
The idea of a series is that the bigger the n, the more accurate the value is. You can either recursive, or simple. I'll give it the simple way. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?
Collectives on Stack Overflow.