3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Calculating Pi using simple JavaScript

One day I was bored and wanted to calculate Pi for myself. After finding out the Leibniz formula for Pi. When it is solved for Pi one gets:
Pi=4/1-4/3+4/5-4/7...
And so on since it's an infinite series. Since one can see a pattern that can easily be defined I concluded that a simple program using a loop could be made. I decided on using JavaScript for its ease of use and use a for loop. The program I made at first was mainly:

var Pi=0;
var n=1;
for (i=0;i<=1000;i++)
{
Pi=Pi+(4/n)
n=n+2
Pi=Pi-(4/n)
n=n+2
}

Where the variable "Pi" is the value of Pi, "i" is the number of times the loop is repeated, and "n" is just a variable to help in the formula. This I however found could further simplified to:

var Pi=0;
var n=1;
for (i=0;i<=1000;i++)
{
Pi=Pi+(4/n)-(4/(n+2))
n=n+4
}

Of course the more times the loop is repeated, the more accurate your value of Pi gets, and while 1000 may seem like a large number, the value of Pi which it calculated is wrong after the 3rd decimal point. I've had it repeat over 100,000 times and it gets more accurate, however, remember that making the number of repeats can slow down and even crash your browser (I speak from experience). If you want some ready made scripting, then here's some scripting I've made which lets you easily control the amount of loop repeats. It is based as an HTML document:

<html>
<body>
<input type="button" onclick="Calculate()" value="Calculate Pi" />
<script type="text/javascript">
//Made by Masterdude
var c=0;
function Calculate()
{
var c=prompt("Enter number of cycles:","0");
if (c>0)
{
var Pi=0;
var n=1;
for (i=0;i<=c;i++)
{
Pi=Pi+(4/n)-(4/(n+2))
n=n+4
}
alert(Pi);
}
else
{
alert("Canceled or Error in input: Input must be positive.");
}
}
</script>
</body>
</html>

6 comments
sort by: active | newest | oldest
Apr 20, 2011. 7:11 PMNachoMahma says:
.  Neat "trick". There's an extra set of braces in the code (after var c=prompt...), but that won't hurt anything.
Apr 21, 2011. 11:14 PMLithium Rain says:
Ha, I just read this comment and the one below and...combed the code for the extra braces. xD
Apr 20, 2011. 10:18 PMlemonie says:

You are aware that the Leibniz formula for Pi converges rather slowly aren't you?
http://en.wikipedia.org/wiki/Leibniz_formula_for_pi

L
Apr 21, 2011. 10:15 PMlemonie says:
I didn't know whether you were aware of the things that people have done to address accuracy with fewer terms - I found it interesting myself.

L

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!