Introduction: How to Find the Value of Pi to a Great Level of Accuracy.

I have found a series that can calculate PI to a great deal of accuracy with less terms.
I'll do that using JavaScript.

Step 1: See the Formula We Use

The day was boring and I so to cut of the boredom, I searched for some formula to calculate PI. Then I found this formula and used it in the JavaScript. Just take a look at the formula:

Step 2: Translating the Formula to JavaScript

Then I used a while loop ( for-loop would have been better, but I forgot that) to use the formula up to accuracy you want. I have the formula:
                                       3 + 4 / ( 2 * 3 * 4) + 4 / (4 * 5 * 6) + .....
The numbers at the denominator increases by 2 and we have used x+1 is 3 and x+2 is 4. SO we get 3 + 4/(2*(2+1)*(2+2))+.....
We use x instead of  2 because we need to add 2 to all of them every time. And now look.
Just use it if you want.

<!DOCTYPE html>
<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script src="latextit.js"></script>
  <script type="text/javascript">
   $(document).ready(function(){
       $('#button').click(function(){
           var value = $('input[name=accuracyPercent]').val();

           if(parseInt(value) < 2 ){
            alert("It should be more than " + parseInt(value) + " ");
           }else if(parseInt(value) > 80 ){
            alert("It should be less than " + parseInt(value) + " ");
           }else{
            //first lower
      var x = 2;
      // the variable to collect the estimation of pi
      var n = 3;
      //initial term
      var i = 2;
      var accuracy = parseInt(value) * 2000;//while loop
      while(i <= accuracy){
          //if the  i is even, then do this
          if(i % 2 === 0){
              // the formula
              n += (4 / ( x * ( x + 1) * ( x + 2)));
          }else{ // else you do this
              // the formula
              n -= (4 / ( x * ( x + 1) * ( x + 2)));
          }
          // add 2 to x
          x += 2;
          // i
          i++;
      }
      $('#item').replaceWith('<div id = "item">' + n + '</div>');
     }
       });
    $('#button').mouseover(function(){
     $(this).css('color', '#fff');
    });
    $('#button').mouseleave(function(){
     $(this).css('color', '#000')
    });
   });
  </script>
  <style type="text/css">
   body{
    background: -webkit-linear-gradient(-45deg, #0ff, blue); /* For Safari */
    background: -o-linear-gradient(-45deg, #0ff, blue); /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(-45deg, #0ff, blue); /* For Firefox 3.6 to 15 */
    background: linear-gradient(-45deg, #0ff, blue); /* Standard syntax */
   }
   form{
    display: inline-block;
    background-color: blue;
   }
   #button{
       display: inline-block;
       height:20px;
    width:70px;
    background-color:#cc0000;
    font-family:arial;
    font-weight:bold;
    color:#000;
    border-radius: 5px;
    text-align:center;
    margin-top:2px;
   }
   #item{
    text-align: center;
    font-size: 40px;
    color: #000;
   }
   .list{
    font-size: 20px;
   } 
  </style>
</head>
<body>
  <form name="accuracyPercent">
   <input type="text" name="accuracyPercent"/>
  </form>
  <div id="button">Get!</div>
  <p class = "list">
   Let us find the value of <span lang="latex">$\pi$</span> to your accuracy point. (Remember, your value should be greater than 2, less than 1000 and an integer):
  </p>
  <div id = "item">
   3.14159 26535 89793 23846
  </div>
</body>
</html>

Comments

author
rimar2000 (author)2014-02-12

YES, it works! Thanks for sharing.

I did an easy KEX macro (I love Kedit) and it brings correct values. The convergence is a bit slow, but it works well.

* 13/02/14 00:03 Calcula pi según una fórmula que bajé de un instructable de ruhanhabib39
* pi = 3 + 4/2x3x4 - 4/4x5x6 + 4/6x7x8 - 4/8x9x10 + ...
* pi = 3,1415926535897932384626433832795 según Cal.exe de Microsoft.
numeric digits 31;
pi = 3;
piant = 3;
signo = 1;
lf = d2c(10);
MSpi = 3.1415926535897932384626433832795;
do ii=1 to 9999 by 2;
jj=ii+1;
kk=ii+2;
ll=ii+3;
pi = pi + signo * 4 / (jj * kk * ll);
'dialog}MSpi = 'MSpi lf'pi valía 'piant lf'y ahora vale ' pi'}okcancel';
if dialog.2 = "CANCEL" then exit;
signo = 0 - signo;
piant = pi;
end;

About This Instructable

1,451views

6favorites

License:

More by ruhanhabib39:How to find the value of pi to a great level of accuracy.
Add instructable to: